- 字符串还原
- 2017-07-13 18:00:48 @
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int n;
bool judge(string s1,string s2,string s3)
{
reverse(s1.begin(),s1.end());
bool flag=true;
for(int i=0;i<n;i++){
if(((s2[i]-'a'+s3[i]-'a')-2*(s1[i]-'a'))%26!=0){
flag=false;
break;
}
}
return flag;
}
int main()
{
string s1,s2,s3;
cin>>n;
cin>>s1>>s2>>s3;
if(judge(s1,s2,s3)) reverse(s1.begin(),s1.end()),cout<<s1<<endl;
else if(judge(s2,s1,s3)) reverse(s2.begin(),s2.end()),cout<<s2<<endl;
else if(judge(s3,s2,s1))reverse(s3.begin(),s3.end()),cout<<s3<<endl;
return 0;
}
下面是自己写的,感觉虽然思路一样,但是写的就丑了许多
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
string s[4];
cin>>n>>s[1]>>s[2]>>s[3];
//cout<<s[1]<<s[2]<<s[3];
for(int i=1;i<=3;i++){
string sub(n,'0');
for(int k=0;k<n;k++){
sub[k]=s[i][n-1-k];
}
//cout<<sub<<endl;
if(i==1){
bool flag = true;
for(int k=0;k<n;k++){
if(abs((s[2][k]-'a')-(sub[k]-'a'))%26!=abs((s[3][k]-'a')-(sub[k]-'a'))%26&&
(abs((s[2][k]-'a')-(sub[k]-'a'))%26+abs((s[3][k]-'a')-(sub[k]-'a'))%26)!=26) flag = false;
}
if(flag==true){
cout<<sub<<endl;
break;
}
}
else if(i==2){
bool flag = true;
for(int k=0;k<n;k++){
if(abs((s[1][k]-'a')-(sub[k]-'a'))%26!=abs((s[3][k]-'a')-(sub[k]-'a'))%26&&
(abs((s[1][k]-'a')-(sub[k]-'a'))%26+abs((s[3][k]-'a')-(sub[k]-'a'))%26)!=26) flag = false;
}
if(flag==true){
cout<<sub<<endl;
break;
}
}
else if(i==3){
bool flag = true;
for(int k=0;k<n;k++){
if(abs((s[2][k]-'a')-(sub[k]-'a'))%26!=abs((s[1][k]-'a')-(sub[k]-'a'))%26&&
(abs((s[2][k]-'a')-(sub[k]-'a'))%26+abs((s[1][k]-'a')-(sub[k]-'a'))%26)!=26) flag = false;
}
if(flag==true){
cout<<sub<<endl;
break;
}
}
}
return 0;
}
1 条评论
-
RoderickQiu LV 6 @ 2018-07-03 21:20:21
本质上是一样的
只不过他把你大量重复代码封装成了一个可以重复使用的函数而已
- 1