【Trie】Immediate Decodability

【题目链接】:

https://loj.ac/problem/10052

【题意】:

就是给一些串,是否存在两个串是相同前缀的。

【题解】

模板题,不想解释了。

【代码】:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std ;
 5 typedef long long ll;
 6 const int N = 1e4;
 7 int Son[N][2];
 8 char str[N][20];
 9 int n,idx;
10 bool f,cnt[N];
11 void Insert(char s[]){
12     int p = 0 ;
13     for(int i=0;s[i];i++){
14         int t = s[i] - '0';
15         if( !Son[p][t] ) Son[p][t] = ++idx;
16         p = Son[p][t] ;
17     }
18     cnt[p] = true;
19 }
20 void Query(char s[]){
21     int p = 0;
22     for(int i=0;s[i+1];i++){
23         int t = s[i] - '0' ;
24         if( !Son[p][t] ) break;
25         p = Son[p][t];
26         if( cnt[p] ) f = true;
27     }
28 }
29 void Init(){
30     f = false;
31     idx = 0 ;
32     memset(Son,0,sizeof Son);
33     memset(cnt,false,sizeof cnt);
34 }
35 int main()
36 {
37     int kase = 0 ;
38     while(~scanf("%s",str[n])){
39         if( !strcmp( str[n],"9") ){
40             //printf("######### %d ######### 
",n);
41             Init();
42             for(int i=0;i<n;i++){
43                 Insert(str[i]);
44             }
45             for(int i=0;i<n;i++){
46                 Query(str[i]);
47             }
48             n = 0;
49             printf("Set %d is",++kase);
50             puts(!f?" immediately decodable":" not immediately decodable");
51         }else{
52             n ++ ;
53         }
54     }
55     return 0;
56 }
View Code
原文地址:https://www.cnblogs.com/Osea/p/11361508.html