#include<cstdio> #include<cstring> #include<iostream> using namespace std; void dfs(bool path[][26],int indegre[],bool used[],char ans[],int numofcharacter,int position) { if(numofcharacter==position) { //printf("%d\n",position); for(int i=0;i<numofcharacter;i++) printf("%c",ans[i]+'a'); printf("\n"); return; } for(int i=0;i<26;i++) { if(used[i]&&!indegre[i]) { used[i]=false; for(int j=0;j<26;j++) { if(used[j]&&path[i][j]) { indegre[j]--; } } ans[position]=i; dfs(path,indegre,used,ans,numofcharacter,position+1); used[i]=true; for(int j=0;j<26;j++) if(used[j]&&path[i][j]) indegre[j]++; } } } int main() { // freopen("input.txt","r",stdin); char a[10000]; int y=0; while(gets(a)!=NULL) { if(y>1)printf("\n"); y=10; bool path[26][26]; bool used[26]; char ans[26],*p1,*p2; int indegre[26],c=0; for(int i=0;i<26;i++) { used[i]=false; indegre[i]=0; for(int j=0;j<26;j++) { path[i][j]=0; } } p1=strtok(a," \n"); while(p1) { char ch=p1[0]; used[ch-'a']=true; p1=strtok(NULL," \n"); c++; } gets(a); p1=strtok(a," \n"); while(p1) { char ch,ph; ch=p1[0]; p1=strtok(NULL," \n"); ph=p1[0]; p1=strtok(NULL," \n"); path[ch-'a'][ph-'a']=true; indegre[ph-'a']++; } dfs(path,indegre,used,ans,c,0); } return 0; }
Sunday, August 17, 2014
uva :124 - Following Orders
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment