

注解
1、回文串的定义。
2、镜像串的定义。此题给了映射关系,也就相当于是个map了。
代码
#include <iostream>
using namespace std;
int isPar(string s){
int len = s.length();
for(int i=0; i<len; i++){
if(s.at(i)!=s.at(len-i-1)){
return 0;
}
}
return 1;
}
int isMirror(string s){
string s1 = "A 3 HIL JM O 2TUVWXY5";
string s2 = "1SE Z 8 ";
int len = s.length();
string smir = "";
for(int i=0; i<len; i++){
if(s.at(i)>='A' && s.at(i)<='Z'){
smir += s1.at(s.at(i)-'A');
}
else{
smir += s2.at(s.at(i)-'1');
}
}
for(int i=0; i<len; i++){
if(s.at(i)!=smir.at(len-i-1)){
return 0;
}
}
return 1;
}
int main(){
string s;
while(cin>>s){
if(isPar(s) && isMirror(s)){
cout<<s<<" -- "<<"is a mirrored palindrome."<<endl<<endl;
}
else if(isPar(s)){
cout<<s<<" -- "<<"is a regular palindrome."<<endl<<endl;
}
else if(isMirror(s)){
cout<<s<<" -- "<<"is a mirrored string."<<endl<<endl;
}
else{
cout<<s<<" -- "<<"is not a palindrome."<<endl<<endl;
}
}
return 0;
}
结果

本文介绍了一种判断字符串是否为回文串和镜像串的方法,通过C++实现,详细解释了代码逻辑,包括如何使用映射关系判断镜像串,以及如何判断回文串。
685

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



