「UVA644」 Immediate Decodability(Trie

题意翻译

本题有多组数据.每组数据给出一列以"9"结尾的仅包含'0'和'1'的字符串,如果里面有一个是另一个的子串,输出"Set &case is not immediately decodable",否则输出"Set &case is immediately decodable".换行. case从1开始计数.

感谢@Fuko_Ibuki 提供的翻译

题目描述

PDF

输入输出格式

输入格式:

输出格式:

输入输出样例

输入样例#1: 复制
01
10
0010
0000
9
01
10
010
0000
9
输出样例#1: 复制
Set 1 is immediately decodable
Set 2 is not immediately decodable

题解

算是比较简单的Trie题了吧。

首先因为懒得特殊处理,就先按长度排个序,然后从小到大枚举每个串,并且在结尾打标记。

如果经过了一个打过标记的点,说明之前有串是它的前缀。

 1 /*
 2     qwerta
 3     UVA644 Immediate Decodability
 4     Accepted
 5     代码 C++,0.99KB
 6     提交时间 2018-10-19 15:04:58
 7     耗时/内存
 8     50ms, 0KB
 9 */
10 #include<algorithm>
11 #include<iostream>
12 #include<cstring>
13 #include<cstdio>
14 using namespace std;
15 string s[11];
16 bool cmp(string qaq,string qwq){
17     return qaq.length()<qwq.length();
18 }
19 struct emm{
20     int nxt[2],tag;
21 }a[83];
22 int main()
23 {
24     //freopen("a.in","r",stdin);
25     int t=0;
26     while(++t&&cin>>s[1])
27     {
28         int n=1;
29         do{if(s[n][0]=='9')break;
30         }while(cin>>s[++n]);
31         n--;
32         sort(s+1,s+n+1,cmp);
33         int cnt=0;
34         memset(a,0,sizeof(a));
35         int flag=0;
36         for(int c=1;c<=n&&!flag;++c)
37         {
38             int k=0;
39             for(int i=0;i<s[c].length();++i)
40             {
41                 if(!a[k].nxt[s[c][i]-'0'])
42                   a[k].nxt[s[c][i]-'0']=++cnt;
43                 k=a[k].nxt[s[c][i]-'0'];
44                 if(a[k].tag)flag++;
45             }
46             a[k].tag=1;
47         }
48         if(!flag)
49           printf("Set %d is immediately decodable
",t);
50         else
51           printf("Set %d is not immediately decodable
",t);
52     }
53     return 0;
54 }
原文地址:https://www.cnblogs.com/qwerta/p/9819291.html