标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12907 | Accepted: 6188 |
Description
Input
Output
Sample Input
01 10 0010 0000 9 01 10 010 0000 9
Sample Output
Set 1 is immediately decodable Set 2 is not immediately decodable
思路:很裸的Trie吧,check给定的字符串有无某一子串是另一子串的前缀
1.sort 短的字符串在前 ps.刚开始慌着套模板,忘了排序了
2.套模板 ps.‘\0‘表示的val[u] = 1,u为下一个节点
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <stack> #include <string> #include <queue> #include <vector> #include <algorithm> #include <ctime> using namespace std; #define EdsonLin #ifdef EdsonLin #define debug(...) fprintf(stderr,__VA_ARGS__) #else #define debug(...) #endif //EdsonLin typedef long long ll; typedef double db; const int inf = 0x3f3f3f; const int MAXN = 1e3; const int MAXNN = 2e6+100; //const int MAXM = 1e6; //const int MAXM = 3e4+100; const int MOD = 1000000007; const db eps = 1e-3; #define PB push_back int readint(){int x;scanf("%d",&x);return x;} inline int code(char x){return x-‘0‘;}; struct Tire{ int ch[MAXN][26]; int val[MAXN]; int sz; bool sg; /* Tire(){ sz = 1; sg = true; sizeof(ch[0],0,sizeof(ch[0][0])); }*/ void Insert(string &s){ int n = s.length(); int u = 0; for(int i=0;i<n;i++){ int c = code(s[i]); if(!ch[u][c]){ memset(ch[sz],0,sizeof(ch[sz])); val[u] = 0; ch[u][c] = sz++; }else if(val[ch[u][c]]==1){ sg = false; } u = ch[u][c]; } val[u] = 1; } void init(){ sz = 1; sg = true; memset(ch[0],0,sizeof(ch[0])); val[0] = 0; } }solver; using namespace std; int main() { string s[MAXN],ts; int mc = 0,x; while(cin>>ts){ solver.init(); x = 0; while(ts.compare("9")!=0){ s[x++] = ts; cin>>ts; } sort(s,s+x); for(int i=0;i<x;i++) solver.Insert(s[i]); if(!solver.sg){ printf("Set %d is not immediately decodable\n",++mc); continue; }else{ printf("Set %d is immediately decodable\n",++mc); } } //cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5643700.html