poj 1056 IMMEDIATE DECODABILITY

字典树问题,判断一个串是不是另一个的前缀。

 1 #include <stdio.h>
 2 #include <string.h>
 3 int next[1000][2],cnt=0;
 4 bool f=1,end[1000],vis[1000];
 5 void inst(char *s)
 6 {
 7     int u=0,p;
 8     for(;*s;s++)
 9     {
10         p = *s - '0';
11         if(!next[u][p])
12         {
13             cnt++;
14             next[cnt][0] = next[cnt][1] = 0;
15             next[u][p] = cnt;
16             vis[u] = 1;
17         }
18         if(end[u])
19         {f = 0; return ;}
20         u = next[u][p];
21     }
22     end[u] = 1;
23     if(vis[u]) f = 0;
24 }
25 int main()
26 {
27     char t[10];
28     int m=1;
29     while(~scanf("%s",t))
30     {
31         if(t[0] != '9')
32         {
33             if(f) inst(t);
34         }
35         else
36         {
37             printf("Set %d is%s immediately decodable\n",m++,f ?"" :" not");
38             f = 1; cnt = 0;
39             next[0][0] = next[0][1] = 0;
40             memset(end,0,sizeof end);
41             memset(vis,0,sizeof end);
42         }
43     }
44     return 0;
45 }
原文地址:https://www.cnblogs.com/lzxskjo/p/2661099.html