1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include <iostream> #include <cstdio> #include <cstring> #include <string> using namespace std; struct node{ bool end; node *nxt[2]; node() { end=false; for(int i=0;i<2;i++) nxt[i]=NULL; } }*root; int main() { string str,s; int id; node *cur,*tmp; int kase=1; while(cin>>str) { bool decodable=1; root=new node(); cur=root; int i; for( i=0;i<str.size()-1;i++) { id=str[i]-'0'; if(cur->nxt[id]==NULL) { cur->nxt[id]=new node(); } cur=cur->nxt[id]; } id=str[i]-'0'; if(cur->nxt[id]==NULL) { cur->nxt[id]=new node(); } cur=cur->nxt[id]; cur->end=1; while(cin>>s) { if(s=="9") break; if(decodable==0) continue; tmp=root; int j; for( j=0;j<s.size()-1;j++) { id=s[j]-'0'; if(tmp->nxt[id]==NULL) tmp->nxt[id]=new node(); tmp=tmp->nxt[id]; if(tmp->end==1) { decodable=0; j=s.size(); } } id=s[j]-'0'; if(tmp->nxt[id]==NULL) tmp->nxt[id]=new node(); else decodable=0; tmp=tmp->nxt[id]; tmp->end=1; } if(decodable) cout<<"Set "<<kase++<<" is immediately decodable\n"; else cout<<"Set "<<kase++<<" is not immediately decodable\n"; } return 0; } |