代码:
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 70001;
struct node{
string data;
int fa;
};
node hash[SIZE];
int i,t;
string s,father,child;
int h(string s){
int i,ans = 0;
for (i = 0;i < s.length();i++) ans = ans + s[i] * s[i];
ans = ans % SIZE;
return ans;
}
int search(string s){
int t = h(s);
while (hash[t].data != s && hash[t].data != "") t++;
return t;
}
void insert(string father,string child){
int temp = search(father),p;
if (hash[temp].data != father) hash[temp].data = father,hash[temp].fa = 0;
p = search(child),hash[p].data = child,hash[p].fa = temp;
}
int main(){
for (int i = 0;i <= SIZE - 1;i++) hash[i].data = "",hash[i].fa = 0;
cin >> s;
do{
father = s.substr(1,6);
do{
cin >> s;
if (s[0] != '+') break;
child = s.substr(1,6);
insert(father,child);
}while (s[0] == '+');
}while (s[0] != '?');
while (s[0] != '$'){
s = s.substr(1,6),t = search(s);
while (hash[t].fa != 0) t = hash[t].fa;
cout << s << ' ' << hash[t].data << endl;
cin >> s;
}
fclose(stdin);
fclose(stdout);
return 0;
}
为什么编译不通过?在自己的编译器上试后,结果:
