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/archive/2012/08/29/2661099.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值