| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 3800 | Accepted: 1502 |
Description
Input
Output
Sample Input
a b f g a b b f v w x y z v y x v z v w v
Sample Output
abfg abgf agbf gabf wxzvy wzxvy xwzvy xzwvy zwxvy zxwvy
Source
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
const int maxn=26+2;
char str[maxn],ans[maxn],apa[maxn];
int in[maxn],gra[maxn][maxn],res;
map<char,int>mp;
void topo(int depth)
{
if(depth==res)
{
printf("%s\n",ans);
return;
}
for(int i=0;i<res;i++)
{
if(in[i]==0)
{
--in[i];
ans[depth]=apa[i];
for(int j=0;j<res;j++)
{
if(gra[i][j])
--in[j];
}
topo(depth+1);
++in[i];
for(int j=0;j<res;j++)
{
if(gra[i][j])
++in[j];
}
}
}
}
int main()
{
int flag,k,len;
char temp1,temp2;
while(gets(str))
{
k=0;
memset(ans,0,sizeof(ans));
memset(in,0,sizeof(in));
memset(gra,0,sizeof(gra));
len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]>='a'&&str[i]<='z')
apa[k++]=str[i];
sort(apa,apa+k);
for(int i=0;i<k;i++)
mp[apa[i]]=i;
res=k;
gets(str);
len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]>='a'&&str[i]<='z')
{
if(flag)
{
temp1=str[i];
flag=0;
}
else
{
temp2=str[i];
gra[mp[temp1]][mp[temp2]]=1;
in[mp[temp2]]++;
flag=1;
}
}
topo(0);
printf("\n");
}
return 0;
}poj1270Following Orders(拓扑排序+dfs回溯),布布扣,bubuko.com
poj1270Following Orders(拓扑排序+dfs回溯)
原文:http://blog.csdn.net/u014303647/article/details/38454955