标签:test cst type lowbit fine overview algo trie put
https://vjudge.net/contest/311647#overview
给定n个串,求出每个串的“唯一最短前缀”
用bo[i]表示从字典树根节点0到节点i连成的字符串为前缀的单词个数,那么在查询一个单词的时候,在该路径的某个节点i出现了bo[i]=1的情况,就说明这个字符串是该单词的唯一前缀。
1 #include<iostream> 2 #include<sstream> 3 #include<fstream> 4 #include<algorithm> 5 #include<cstring> 6 #include<iomanip> 7 #include<cstdlib> 8 #include<cctype> 9 #include<vector> 10 #include<string> 11 #include<cmath> 12 #include<ctime> 13 #include<stack> 14 #include<queue> 15 #include<map> 16 #include<set> 17 #define mem(a,b) memset(a,b,sizeof(a)) 18 #define random(a,b) (rand()%(b-a+1)+a) 19 #define ll long long 20 #define ull unsigned long long 21 #define e 2.71828182 22 #define Pi acos(-1.0) 23 #define ls(rt) (rt<<1) 24 #define rs(rt) (rt<<1|1) 25 #define lowbit(x) (x&(-x)) 26 using namespace std; 27 const int MAXN=2e4+5; 28 const int Z=26; 29 int ch[MAXN][Z]; 30 int bo[MAXN]; 31 char str[1001][21]; 32 int tot=1; 33 void insert(char *s) 34 { 35 int l=strlen(s),u=1; 36 for(int i=0;i<l;++i) 37 { 38 int c=s[i]-‘a‘; 39 if(!ch[u][c]) ch[u][c]=++tot; 40 u=ch[u][c]; 41 bo[u]++; 42 } 43 } 44 void query(char *s) 45 { 46 int l=strlen(s),u=1; 47 for(int i=0;i<l;++i) 48 { 49 int c=s[i]-‘a‘; 50 if(bo[u]==1) break; 51 putchar(s[i]); 52 u=ch[u][c]; 53 } 54 putchar(‘\n‘); 55 } 56 int main() 57 { 58 int cnt=0; 59 while(scanf("%s",str[++cnt])!=EOF) 60 insert(str[cnt]); 61 62 for(int i=1;i<=cnt;++i) 63 { 64 cout<<str[i]<<‘ ‘; 65 query(str[i]); 66 } 67 }
标签:test cst type lowbit fine overview algo trie put
原文地址:https://www.cnblogs.com/wangzhebufangqi/p/11527683.html