潜伏者这道题比较简单,只需要一个一个去比较,然后保存已经有配对的,并用一个变量来保存配对个数,然后如果在配对时看一下是否已经配对过即可,下面是代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
char yuan[105],mi[105],zimu[30],wan[105];
int isu;
bool is[30];
int main()
{
// freopen("spy.in","r",stdin);
// freopen("spy.out","w",stdout);
cin>>mi>>yuan;
int len=strlen(mi);
for(int i=0;i<len;i++){
if(!is[mi[i]-'A']){
is[mi[i]-'A']=1;
isu++;
zimu[mi[i]-'A']=yuan[i];
}
else{
if(zimu[mi[i]-'A']!=yuan[i]){
cout<<"Failed";
return 0;
}
}
}
if(isu!=26) cout<<"Failed";
else {
cin>>wan;
for(int i=0;i<strlen(wan);i++) cout<<zimu[wan[i]-'A'];
}
return 0;
}
提交了吗?发现是90分的心情怎样,我就是这样啊,最初只考虑的由密文到原文的配对,未考虑原文到密文,那下面是正解(没骗你):
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;
char yuan[105],mi[105],zimu1[30],zimu2[30],wan[105];
int isu1,isu2;
bool is1[30],is2[30];
int main()
{
// freopen("spy.in","r",stdin);
// freopen("spy.out","w",stdout);
cin>>mi>>yuan;
int len=strlen(mi);
for(int i=0;i<len;i++){
if(!is1[mi[i]-'A']){
is1[mi[i]-'A']=1;
isu1++;
zimu1[mi[i]-'A']=yuan[i];
} else{
if(zimu1[mi[i]-'A']!=yuan[i]){
cout<<"Failed";
return 0;
}
}
if(!is2[yuan[i]-'A']){
is2[yuan[i]-'A']=1;
isu2++;
zimu2[yuan[i]-'A']=mi[i];
} else{
if(zimu2[yuan[i]-'A']!=mi[i]){
cout<<"Failed";
return 0;
}
}
}
if(isu1!=26) cout<<"Failed";
else if(isu2!=26) cout<<"Failed";
else {
cin>>wan;
for(int i=0;i<strlen(wan);i++) cout<<zimu1[wan[i]-'A'];
}
return 0;
}