大意:判断输入的字符串是否为回文串和“镜像串”(不知道怎么翻译,一个字母的补由题目给出,如下)
A list of all valid characters and their reverses is as follows.
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |
题目并不难。分别写判断回文串和补串的函数就简单了。值得注意的是:A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string.这句话的重点是如果某个字母没有reverse,那么这个串就不符号,而不是某个字母没有就忽略这个字母,我一开始就是这样理解错了。
#include<iostream>
#include<cstring>
using namespace std;
char Reverse(char c)//表
{
switch(c)
{
case 'A':return 'A';
case 'E':return '3';
case 'H':return 'H';
case 'I':return 'I';
case 'J':return 'L';
case 'M':return 'M';
case 'L':return 'J';
case 'O':return 'O';
case 'S':return '2';
case 'T':return 'T';
case 'U':return 'U';
case 'V':return 'V';
case 'W':return 'W';
case 'X':return 'X';
case 'Y':return 'Y';
case 'Z':return '5';
case '2':return 'S';
case '3':return 'E';
case '5':return 'Z';
case '8':return '8';
}
return 0;
}
bool Palindrome(char s[])//判断回文串
{
int i,n;
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]!=s[n-i-1])
return false;
}
return true;
}
bool Mirrored(char s[])//判断镜像串
{
int i,n;
n=strlen(s);
for(i=0;i<n;i++)
{
if(Reverse(s[i])!=s[n-i-1])
return false;
}
return true;
}
const int MAX=20 + 5;
int main()
{
char s[MAX];
bool fM,fP;
while(cin>>s)
{
fM=Mirrored(s);
fP=Palindrome(s);
if(fP)//四种情况
{
if(fM)
cout<<s<<" -- is a mirrored palindrome."<<endl;
else
cout<<s<<" -- is a regular palindrome."<<endl;
}
else
{
if(fM)
cout<<s<<" -- is a mirrored string."<<endl;
else
cout<<s<<" -- is not a palindrome."<<endl;
}
cout<<endl;
}
return 0;
}