Following Orders(拓扑+dfs)
AC_Code:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn = 25; 5 const int inf = 0x3f3f3f3f; 6 const int mod = 998244353; 7 #define rep(i,first,second) for(ll i=first;i<=second;i++) 8 #define dep(i,first,second) for(ll i=first;i>=second;i--) 9 10 bool g[maxn][maxn]; 11 char str[maxn],ans[maxn]; 12 int indegree[maxn]; 13 int res; 14 15 map<char,int>num; 16 17 void toposort_dfs(int depth){ 18 if( depth==res ){ 19 printf("%s\n",ans); 20 return ; 21 } 22 rep(u,0,res-1){ 23 if( indegree[u]==0 ){ 24 indegree[u]--; 25 ans[depth]=str[u]; 26 27 rep(v,0,res-1){ 28 if( g[u][v] ){ 29 indegree[v]--; 30 } 31 } 32 33 toposort_dfs(depth+1); 34 35 indegree[u]++; 36 rep(v,0,res){ 37 if( g[u][v] ){ 38 indegree[v]++; 39 } 40 } 41 } 42 } 43 } 44 45 int main() 46 { 47 char temp[55]; 48 bool first=true; 49 while( gets(temp) ){ 50 memset(g,false,sizeof(g)); 51 memset(indegree,0,sizeof(indegree)); 52 53 int len=strlen(temp); 54 res=0; 55 rep(i,0,len-1){ 56 if( isalpha(temp[i]) ) str[res++]=temp[i]; 57 } 58 sort(str,str+res); 59 rep(i,0,res-1) num[str[i]]=i; 60 gets(temp); 61 len = strlen(temp); 62 int flag=0; 63 int c1,c2; 64 rep(i,0,len-1){ 65 if( isalpha(temp[i])){ 66 if(flag==0){ 67 c1=num[temp[i]]; 68 flag=1; 69 } 70 else{ 71 c2=num[temp[i]]; 72 g[c1][c2]=true; 73 indegree[c2]++; 74 flag=0; 75 } 76 } 77 } 78 memset(ans,0,sizeof(ans)); 79 if( first==false ) printf("\n"); 80 else first=false; 81 toposort_dfs(0); 82 } 83 return 0; 84 }