Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19109 Accepted Submission(s): 6748
You are to find all the hat’s words in a dictionary.
Only one case.
#include <iostream> #include <cstdio> #include <cstring> #define MAX 600005 using namespace std; int trie[MAX][26]; bool flag[MAX]; int pos,num; bool may[50000]; void Insert(char *s,int snum) { int i = 0,c = 0; while(s[i]) { int d = s[i] - 'a'; if(!trie[c][d]) trie[c][d] = ++ pos; c = trie[c][d]; if(flag[c]) { may[snum] = true; } i ++; } flag[c] = true; } bool Query(char *s,int k,int t) { if(t == 2) { if(!s[k])return true; return false; } int i = k,c = 0; bool ok = false; while(s[i]) { int d = s[i] - 'a'; if(!trie[c][d]) trie[c][d] = ++ pos; c = trie[c][d]; i ++; if(flag[c]) { ok |= Query(s,i,t + 1); } } return ok; } char str[50000][30]; int main() { while(~scanf("%s",str[num])) { Insert(str[num],num); num ++; } for(int i = 0;i < num;i ++) { if(may[i] && Query(str[i],0,0)) { printf("%s\n",str[i]); } } }