杭师校赛I题-Little Sub and Enigma AC代码

不难 听了出题人讲思路马上就会 但我就是要搞题解 纪念我的赛场自闭四小时

最坑的是25对推第26对
双向映射一一对应
直接数组存
感觉也许是输在英语不好?

题目:Little Sub and Enigma

Description
Little Sub builds a naive Enigma machine of his own. It can only be used to encrypt/decrypt lower-case letters by giving each letter a unique corresponding lower-case letter. In order to ensure the accuracy, no contradiction or controversy is allowed in both the decryption and the encryption, which means all lower-case letters can only be decrypted/encrypted into a distinct lower-case letter.
Now we give you a string and its encrypted version. Please calculate all existing corresponding relationship which can be observed or deducted through the given information.
Input
The first line contains a string S, indicating the original message.
The second line contains a string T , indicating the encrypted version.
The length of S and T will be the same and not exceed 1000000.
Output
we use a string like ’x->y’ to indicate that letter x will be encrypted to letter y.
Please output all possible relationships in the given format in the alphabet order.
However, if there exists any contradiction in the given information, please just output Impossible in one line.
Author
YE, Zicheng

放一下出题人,zjunb

AC代码

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
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[27], b[27];
string s1, s2;
int main() {
while (cin >> s1 >> s2) {
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
int flag = 0;
for (int i = 0; i < s1.length(); i++) {
int t1, t2;
t1 = s1[i] - 'a' + 1;
t2 = s2[i] - 'a' + 1;
if (!a[t1]) a[t1] = t2;
else if (a[t1] != t2) {
cout << "Impossible" << endl;
flag = 1;
break;
}
if (!b[t2]) b[t2] = t1;
else if (b[t2] != t1) {
cout << "Impossible" << endl;
flag = 1;
break;
}
//复读,双向存
}
if (flag) continue;
//上面break的是for,还需要一个continue,晕了
int ai, bi = 0;
int cnt = 0;
for (int i = 1; i <= 26; i++) {
if (a[i] == 0) cnt++, ai = i;
if (cnt >= 2) continue;
}
if (cnt == 1)
for (int i = 1; i <= 26; i++)
if (b[i] == 0) bi = i;
if (bi) a[ai] = bi;//25推26
for (int i = 1; i <= 26; i++) {
if (a[i] ) {
char tmp1, tmp2;
tmp1 = i - 1 + 'a';
tmp2 = a[i] - 1 + 'a';
cout << tmp1 << "->" << tmp2 << endl;
}
}
}
return 0;
}

其他题莫得了 太菜了