map
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int flag = 0;
map<string,int>mp;
string ans;
while(n--){
string s;
cin>>s;
if(mp[s]){
flag=1;
ans = s;
}
mp[s]++;
}
if(!flag) cout<<"NO"<<endl;
cout<<ans<<endl;
}
哈希
#include <iostream>
#include <stack>
using namespace std;
const int h = 1e4+5;
string Value[h];
string UpValue[h];
int UpValueCount = 0;
int Hx(string s){
int n = s.size();
int sum1 = 0;
for (int i = 0; i < n; i++){
sum1 = sum1 * 131 % h + (s[i] - 'a' + 1) % h;
}
return (sum1 + h) % h;
}
bool isAt(string s)
{
int n = Hx(s);
if (Value[n] == "") return false;
else if (Value[n] == s) return true;
else{
for (int i = 0; i < UpValueCount; i++)
if (UpValue[n] == s)
return true;
return false;
}
}
bool in(string s){
int n = Hx(s);
if (Value[n] == ""){
Value[n] = s;
return true;
}
else if (Value[n] == s) return false;
else {
for (int i = 0; i < UpValueCount; i++)
if (UpValue[n] == s)
return false;
UpValue[UpValueCount++] = s;
return true;
}
}
int main(){
int n;
string ans = "NO";
cin >> n;
for (int i = 0; i < n; i++){
string word;
cin >> word;
if (!in(word)){
cout << word << endl;
return 0;
}
}
cout << ans << endl;
}