-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1092.cpp
76 lines (76 loc) · 1.54 KB
/
1092.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 30
int a[maxn],b[maxn],c[maxn];
int num[maxn],Next[maxn],n,cnt;
char s1[maxn],s2[maxn],s3[maxn];
bool book[maxn];
bool Judge() {
for(int i=n-1,x=0;i>=0;i--) {
int A=num[a[i]],B=num[b[i]],C=num[c[i]];
if((A+B+x)%n!=C) return false;
x=(A+B+x)/n;
}
return true;
}
bool judge() {
if(num[a[0]]+num[b[0]]>=n)
return true;
for(int i=n-1;i>=0;i--) {
int A=num[a[i]],B=num[b[i]],C=num[c[i]];
if(A==-1||B==-1||C==-1) continue;
if((A+B)%n!=C&&(A+B+1)%n!=C)
return true;
}
return false;
}
void Print() {
for(int i=0;i<n;i++)
printf("%d ",num[i]);
exit(0);
}
void dfs(int x) {
if(judge()==true) return;
if(x==n) {
if(Judge()==true) Print();
return;
}
for(int i=n-1;i>=0;i--)
if(book[i]==false) {
num[Next[x]]=i;
book[i]=true;
dfs(x+1);
num[Next[x]]=-1;
book[i]=false;
}
return;
}
inline int id(char c) {
return c-'A';
}
void GetNext(int x) {
if(book[x]==false) {
book[x]=true;
Next[cnt++]=x;
}
return;
}
int main() {
scanf("%d",&n);
scanf("%s%s%s",s1,s2,s3);
for(int i=0;i<n;i++) {
a[i]=id(s1[i]);
b[i]=id(s2[i]);
c[i]=id(s3[i]);
num[i]=-1;
}
for(int i=n-1;i>=0;i--) {
GetNext(a[i]);
GetNext(b[i]);
GetNext(c[i]);
}
for(int i=0;i<n;i++) book[i]=false;
dfs(0);
return 0;
}