Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10837 Accepted Submission(s): 3735
1 /*hdu 1671 ×ÖµäÊ÷*/ 2 #define LOCAL 3 #include<cstdio> 4 #include<cstring> 5 #include<iostream> 6 using namespace std; 7 struct Trie 8 { 9 struct Trie *next[10]; 10 bool tail; 11 }; 12 char str[10005][12]; 13 void in_Trie(char *s,Trie *root) 14 { 15 Trie *cur=root,*newcur; 16 for(int i=0;s[i]!=‘\0‘;i++) 17 { 18 if(cur->next[s[i]-‘0‘]==NULL) 19 { 20 newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie))); 21 for(int j=0;j<=9;j++) 22 newcur->next[j]=NULL; 23 newcur->tail=0; 24 cur->next[s[i]-‘0‘]=newcur; 25 } 26 cur=cur->next[s[i]-‘0‘]; 27 } 28 cur->tail=1; 29 } 30 bool query(char *s,Trie *root) 31 { 32 int i=0; 33 Trie *cur=root; 34 for(i=0;s[i];i++){ 35 if(cur->tail==0&&cur->next[s[i]-‘0‘]!=NULL) 36 cur=cur->next[s[i]-‘0‘]; 37 else break; 38 } 39 if(s[i]!=‘\0‘) return 1; 40 return 0; 41 } 42 void dele(Trie *root) 43 { 44 for(int i=0 ; i<=9 ; i++ ) 45 if(root->next[i]!=NULL) 46 dele(root->next[i]); 47 // free(root); 48 delete root; 49 } 50 int main() 51 { 52 #ifdef LOCAL 53 freopen("test.in","r",stdin); 54 #endif 55 int t,i,n; 56 Trie *root; 57 scanf("%d",&t); 58 while(t--) 59 { 60 root = new Trie ; 61 for(int j=0;j<=9;j++) 62 root->next[j]=NULL; 63 root->tail=0; 64 65 scanf("%d",&n); 66 for(i=0;i<n;i++){ 67 scanf("%s",str[i]); 68 in_Trie(str[i],root); 69 } 70 71 for(i=0;i<n;i++) 72 if(query(str[i],root)!=0) 73 break; 74 dele(root); 75 if(i>=n) 76 printf("YES\n"); 77 else 78 printf("NO\n"); 79 } 80 return 0; 81 }
hdu----(1671)Phone List(Trie带标签)
原文:http://www.cnblogs.com/gongxijun/p/4000770.html