这道题按照题意处理就可以了。似乎一开始就想得和题目要求比较一致(比如字符串末端的空格不算,奇数长度的字符串中间的那个字符如果不能reverse的话字符串不算mirrored)。bug之处在于构建那个reverse表的时候少输入了个Y。还好样例中有Y。。。
代码如下:
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<char, char> reverses;
reverses['A'] = 'A';
reverses['E'] = '3';
reverses['H'] = 'H';
reverses['I'] = 'I';
reverses['J'] = 'L';
reverses['L'] = 'J';
reverses['M'] = 'M';
reverses['O'] = 'O';
reverses['S'] = '2';
reverses['T'] = 'T';
reverses['U'] = 'U';
reverses['V'] = 'V';
reverses['W'] = 'W';
reverses['X'] = 'X';
reverses['Y'] = 'Y';
reverses['Z'] = '5';
reverses['1'] = '1';
reverses['2'] = 'S';
reverses['3'] = 'E';
reverses['5'] = 'Z';
reverses['8'] = '8';
string str;
while(true)
{
cin>>str;
if(cin.eof())
break;
bool ispalindrome = true, ismirrored = true;
for(int i = 0; i <= str.size() / 2; ++i)
{
if(!ispalindrome && !ismirrored)
{
break;
}
if(i == str.size() / 2)
{
if(ismirrored && (str.size() % 2) && reverses.find(str[i]) == reverses.end())
ismirrored = false;
}
else
{
if(ispalindrome && str[i] != str[str.size() - 1 - i])
{
ispalindrome = false;
}
if(ismirrored && (reverses.find(str[i]) == reverses.end()
|| reverses[str[i]] != str[str.size() - 1 - i]))
{
ismirrored = false;
}
}
}
if(ispalindrome && ismirrored)
cout<<str<<" -- is a mirrored palindrome."<<endl<<endl;
else if(ispalindrome)
cout<<str<<" -- is a regular palindrome."<<endl<<endl;
else if(ismirrored)
cout<<str<<" -- is a mirrored string."<<endl<<endl;
else
cout<<str<<" -- is not a palindrome."<<endl<<endl;
}
return 0;
}