拓扑
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
#define MAXA 25005
#define MAXN 50005
#define LL long long
#define MOD 10000000000
int head[200];
int to[200];
int next[200];
int tot;
void addedge(int u,int v)
{
to[tot]=v;
next[tot]=head[u];
head[u]=tot++;
}
char ch[200];
char s[200];
char ans[200];
int a[200];
int b[200];
int cnt;
void dfs(int t)
{
if(t==cnt)
{
ans[t]='\0';
puts(ans);
return ;
}
for(int i=0;i<cnt;i++)
{
if(b[i]==0)
{
b[i]--;
for(int j=head[i];j!=-1;j=next[j])
{
b[to[j]]--;
}
ans[t]=s[i];
dfs(t+1);
b[i]++;
for(int j=head[i];j!=-1;j=next[j])
{
b[to[j]]++;
}
}
}
}
int main()
{
while(gets(ch))
{
cnt=0;
for(int i=0;ch[i];i++)
{
if(i%2==1)continue;
s[cnt++]=ch[i];
}
sort(s,s+cnt);
for(int i=0;i<cnt;i++)
{
a[s[i]]=i;
}
memset(head,-1,sizeof(head));
tot=0;
gets(ch);
int len=strlen(ch);
memset(b,0,sizeof(b));
for(int i=0;i<len;i+=4)
{
int x=a[ch[i]];
int y=a[ch[i+2]];
b[y]++;
addedge(x,y);
}
dfs(0);
printf("\n");
}
return 0;
}