Trie树练手题,然而空间不够用最后一个点MLE,90
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include<iostream> #include<algorithm> #include<string> using namespace std; const int MaxN = 5000000; int trie[MaxN][26]; int cnts[MaxN]; int tot; int trieInsert(string s) { int node = 0; for (int i = 0; i < s.size(); i++) { if (!trie[node][s[i] - 'a'])node = trie[node][s[i] - 'a'] = ++tot; else node = trie[node][s[i] - 'a']; } return node; } int main() { int n,m; string s; cin >> n; while (n--) { cin >> s; int node=trieInsert(s); cnts[node]=1; } cin >> m; while (m--) { cin >> s; int node = trieInsert(s); if (cnts[node] == 1){ cout << "OK" << endl; cnts[node]=2; } else if (cnts[node] == 0)cout << "WRONG" << endl; else if (cnts[node] == 2)cout << "REPEAT" << endl; } } |