题目:https://www.luogu.com.cn/problem/P1071
错题笔记: 原代码下面样例没过
ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYA
ABCD
正确答案:Failed
错误答案:ABCD
这是因为第二个字符串中A对应了两次,
题目要求每个字母只对应一个唯一的“密字”,不同的字母对应不同的“密字”。“密字”可以和原字母相同。
所以我开了个book数组标记第二个字符串中字母是否重复对应。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 7;
string s1, s2, s3;
map<int, char> Map;
int book[30];
int main()
{
Map.clear();
cin >> s1 >> s2 >> s3;
for(int i = 0; i < s1.length(); i++)
{
int x = s1[i] - 65;
if(!Map[x] || (Map[x] == s2[i]))//如果这个字母以前没有出现过,或者这个字母出现过且此时对应的字母跟先前出现的那一次对应的一样
{
if(!book[s2[i] - 'A']) //如果这个字母以前没有对应过
{
Map[x] = s2[i];
book[s2[i] - 'A'] = 1;
}
}
else
{
cout << "Failed" << endl;
return 0;
}
}
for(int i = 0; i < 26; i++)
{
if(!Map[i])
{
cout << "Failed" << endl;
return 0;
}
}
int flag = 1;
for(int i = 0; i < s3.length(); i++)
{
int x = s3[i] - 65;
cout << Map[x];
}
if(flag == 1) cout << endl;
return 0;
}