题目: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;
}
本文详细解析了洛谷P1071题目的解题思路,通过使用map和数组来检查字符串映射的有效性,确保每个字母只对应一个唯一的“密字”。文章分享了AC代码,包括输入输出、字母映射检查和重复标记的实现。
&spm=1001.2101.3001.5002&articleId=104038567&d=1&t=3&u=35ddf6249e5d49b997b7146195abef95)
1万+

被折叠的 条评论
为什么被折叠?



