不明白为什么会超出时间限制,可能跟string输入有关,请大佬调错
查看原帖
不明白为什么会超出时间限制,可能跟string输入有关,请大佬调错
726777
wdAndLzl楼主2022/8/15 14:43
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MOD = 1000000007;
const int P = 10000019;

int N;
string str;
vector<ll> ans;

ll hashFunc(string str){
    ll H = 0;
    
    for(int i=0;i<str.length();i++){
        H = (H * P + str[i] - 'a') % MOD;
    }

    return H;
}

int main(){
    cin>>N;
    for(int i=0;i<N;i++){
        cin>>str;
        ll id = hashFunc(str);
        ans.push_back(id);
    }

    sort(ans.begin(),ans.end());

    int count = 0;
    for(int i=0;i<N;i++){
        if(i = 0 || ans[i] != ans[i-1]){
            count++;
        }
    }

    cout<<count;
}

2022/8/15 14:43
加载中...