Links : http://acm.hdu.edu.cn/showproblem.php?pid=1671
Meaning of the questions : Given n strings ask whether there is a prefix string of other strings
Ideas : Dictionary bare tree problem, I set the board
Code :
1 //#include<bits/stdc++.h> 2 #include<stdio.h> 3 #include<iostream> 4 #include<string.h> 5 #define inf 0x3f3f3f3f 6 using namespace std; 7 typedef long long ll; 8 typedef long double ld; 9 const int M = int(1e5)*2 + 5; 10 const int mod = 10056; 11 inline int lowbit(int x) { 12 return x & (-x); 13 } 14 15 char s[M]; 16 int trie[M][15],cnt[M]; 17 bool f; 18 int tot; 19 void insert(){ 20 int root=0; 21 for(int i=0;s[i];i++){ 22 int id=s[i]-'0'; 23 if(Trie [the root] [ID] == 0 ) { 24 Trie [the root] [ID] = TOT ++ ; 25 } 26 is the root = Trie [the root] [ID]; 27 } 28 CNT [the root] ++ ; 29 } 30 void Search () 31 is { 32 int the root = 0 ; // from the beginning to find the root 33 is for ( int I = 0 ; S [I]; I ++ ) 34 is { 35 int X = S [I] - ' 0 ' ; // 36 IF (Trie [root] [x] == 0 ) return ; // as the root node for the first letter x does not exist, returns 0 37 [ IF (! CNT [Trie [root] [x]] = 0 ) { 38 is F = . 1 ; 39 return ; 40 } 41 is the root = Trie [the root] [X]; // prepare for the next query letter, go down 42 is } 43 is F = . 1 ; // found 44 is } 45 46 is int main () 47 { 48 int n-; CIN >> n-; 49 while(n--){ 50 memset(trie,0,sizeof(trie)); 51 memset(cnt,0,sizeof(cnt)); 52 tot=1;f=0; 53 int m;cin>>m; 54 for(int i=0;i<m;i++){ 55 scanf("%s",s); 56 if(f==1) continue; 57 search(); 58 insert(); 59 } 60 61 if(f==0){ 62 printf("YES\n"); 63 } 64 else{ 65 printf("NO\n"); 66 } 67 } 68 return 0; 69 }
Note : In fact, the dictionary tree I will not write it