-
Notifications
You must be signed in to change notification settings - Fork 0
/
00124.cpp
57 lines (52 loc) · 991 Bytes
/
00124.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
vector<int> graph[26];
int w[26],ans[30],n;
void dfs(int idx)
{
if(idx == n)
{
for(int i = 0;i < n;i++) printf("%c",ans[i] + 'a');
printf("\n");
return;
}
for(int i = 0;i < 26;i++)
{
if(w[i] == 0)
{
ans[idx] = i;
w[i]--;
for(int j = 0;j < graph[i].size();j++) w[graph[i][j]]--;
dfs(idx + 1);
w[i]++;
for(int j = 0;j < graph[i].size();j++) w[graph[i][j]]++;
}
}
}
int main()
{
int len,f,t,d = 0;
char inp[1000],*p;
p = gets(inp);
while(1)
{
if(!p) break;
len = strlen(inp);
n = (len / 2) + 1;
for(int i = 0;i < 26;i++) w[i] = -1,graph[i].clear();
for(int i = 0;i < len;i++) w[inp[i] - 'a'] = 0;
len = strlen(gets(inp));
for(int i = 0;i < len;i += 4)
{
f = inp[i] - 'a',t = inp[i + 2] - 'a';
graph[f].push_back(t);
w[t]++;
}
dfs(0);
p = gets(inp);
if(p) printf("\n");
}
return 0;
}