hdu1671 字典树

 1 #include<stdio.h>
 2 #include<string.h>
 3 int sz,ch[100005][30],val[100005];
 4 void insert(char *s)
 5 {
 6     int u=0,i,len=strlen(s);
 7     for (i=0;i<len;i++)
 8     {
 9         if (ch[u][s[i]-'0']==0)
10         {
11             memset(ch[sz],0,sizeof(ch[sz]));
12             val[sz]=0;
13             ch[u][s[i]-'0']=sz;
14             sz++;
15         }
16         u=ch[u][s[i]-'0'];
17     }
18     val[u]=1;
19 }
20 int main()
21 {
22     int t,i,j,n,judge,u,d;
23     char s[10005][15];
24     while (~scanf("%d",&t))
25     while (t--)
26     {
27         scanf("%d",&n);
28         getchar(); sz=1;
29         memset(ch[0],0,sizeof(ch[0]));
30         for (i=1;i<=n;i++) {gets(s[i]); insert(s[i]); }
31         judge=1;
32         for (i=1;i<=n;i++)
33         {
34             u=0; d=0;
35             while (ch[u][s[i][d]-'0']!=0&&d<strlen(s[i])) 
36             {
37                 u=ch[u][s[i][d]-'0'];
38                 d++;
39             }
40             if (d==strlen(s[i]))
41             {
42                 for (j=0;j<=9;j++)
43                  if (ch[u][j]!=0) judge=0; 
44             }
45         }
46         if (judge==0) printf("NO\n");
47         else printf("YES\n");
48     }
49     return(0);
50 } 

http://acm.hdu.edu.cn/showproblem.php?pid=1671

posted on 2014-07-16 17:53  xiao_xin  阅读(77)  评论(0编辑  收藏  举报

导航