-
Notifications
You must be signed in to change notification settings - Fork 1
/
Shuffle'm Up.cpp
57 lines (53 loc) · 980 Bytes
/
Shuffle'm Up.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
这个题就是利用一个map来存之前出现过的字符串,如果在变换中遇到了之前的字符串则自动退出失败否则一直搜索下去
#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <cstring>
using namespace std;
char a[110], b[110], c[220], s[220];
int main(){
int i, j, ans, sum, n, flag, t, m;
cin >> t;
m = 1;
while (t--) {
flag = 0;
ans = 0;
cin >> n;
cin >> a;
cin >> b;
cin >> c;
map<string, int> q;
//cout << n << endl;
while (1) {
ans++;
sum = 0;
for (i = 0; i < n; i++){
s[sum++] = b[i];
s[sum++] = a[i];
}
s[sum] = '\0';
if (strcmp(s, c) == 0) {
break;
}
if (q[s] == 1) {
flag = 1;
break;
}
q[s] = 1;
for (i = 0; i < n; i++){
a[i] = s[i];
b[i] = s[i + n];
}
a[i] = b[i] = '\0';
}
cout << m << " ";
m++;
if (!flag) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}