uva_644暴力加字典树解法
暴力
#include<iostream> #include<string.h> #include<cstdio> using namespace std; int main() { int t=0,i=0; char s[10][12]; bool a[12][12]; memset(s,'\0',sizeof(s)); while(scanf("%s",s[i])!=EOF) { if(s[i][0]=='9') { t++; int state=1; memset(a,false,sizeof(a)); for(int j=0;j<i;j++) for(int k=j+1;k<=i;k++) for(int l=0;s[j][l]!='\0'&&s[k][l]!='\0';l++) {if(s[j][l]!=s[k][l]) {a[j][k]=true; //cout<<j<<k<<l<<endl; } //cout<<j<<k<<l<<endl; //cout<<l<<s[k][l]<<endl; } for(int j=0;j<i;j++) for(int k=j+1;k<=i;k++) if(a[j][k]==false) state=0; if(!state) printf("Set %d is not immediately decodable\n",t); if(state==1) printf("Set %d is immediately decodable\n",t); memset(s,'\0',sizeof(s)); i=0; } else i++; } return 0; }字典树
#include<iostream> #include<string.h> #include<cstdio> using namespace std; int tree[5000][2]; int w[5000]; int numv=0; char s[10][12]; void insert(char s[]) { int u=0; w[0]++; int len=strlen(s); for(int i=0; i<len; i++) { if(tree[u][s[i]-'0']==0) { tree[u][s[i]-'0']=++numv; } u=tree[u][s[i]-'0']; w[u]++; } //cout<<w[1]<<w[2]<<endl; } bool find(char s[]) { int u=0; int len=strlen(s); for(int i=0; i<len; i++) { u=tree[u][s[i]-'0']; //cout<<w[u]<<" "<<u<<s[i]<<endl; if(w[u]==1) { return true; break; } } return false; } int main() { int t=0,i=0; bool a[12]; memset(s,'\0',sizeof(s)); while(scanf("%s",s[i])!=EOF) { if(s[i][0]=='9') { numv=0; memset(tree,0,sizeof(tree)); memset(w,0,sizeof(w)); memset(a,false,sizeof(a)); t++; int state=1; for(int j=0; j<i; j++) insert(s[j]); for(int j=0; j<i; j++) if(find(s[j])==true) { a[j]=true; } for(int j=0; j<i; j++) if(a[j]==false) {state=0; //cout<<j<<"as"<<endl; } if(!state) cout<<"Set "<<t<<" is not immediately decodable"<<endl; else cout<<"Set "<<t<<" is immediately decodable"<<endl; memset(s,'\0',sizeof(s)); i=0; } else i++; } return 0; }