//这题需要考虑的情况也比较多,有点烦,一定需要用清晰的思路,要不很容易出错,尤其是对四种情况的讨论!
//这题WA了两次,一次是:输出的英文写错了,是自己手打的,没有进行对样例的复制粘贴,下次要谨记!
//第二次是没有讲‘O’和‘0’考虑进去,粗心了!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i, j, len;
string str, tmp;
bool is_palindrome, is_mirrored, is_pm;
while (cin >> str){
is_palindrome = false;
is_mirrored = false;
is_pm = false;
len = str.length();
tmp = str;
//判断是否为palindrome
for (i = 0, j = len-1; i < len/2; i++, j--){
if ((str[i] == 'O' && str[j] == '0') || (str[i] == '0' || str[j] == 'O'))
continue;
else if (str[i] != str[j]){
is_palindrome = true;
break;
}
}
//判断是否为mirrored
for (i = 0; i < len; i++){
if (str[i] != 'A' && str[i] != 'M' && str[i] != 'Y' &&
str[i] != 'Z' && str[i] != 'O' && str[i] != '1' &&
str[i] != '2' && str[i] != 'E' && str[i] != '3' &&
str[i] != 'S' && str[i] != '5' && str[i] != 'H' &&
str[i] != 'T' && str[i] != 'I' && str[i] != 'U' &&
str[i] != 'J' && str[i] != 'V' && str[i] != '8' &&
str[i] != 'W' && str[i] != 'L' && str[i] != 'X'){
is_mirrored = true;
break;
}
else{
if (str[i] == '2')
str[i] = 'S';
else if (str[i] == '3')
str[i] = 'E';
else if (str[i] == '5')
str[i] = 'Z';
}
}
//在是mirrored的情况之下判断是否为palindrome,注意:这里用于判断回文的字符串应该是用最原始的字符串,并不是用用mirrored变换过来的字符串!
if (!is_mirrored){
for (i = 0, j = len-1; i < len/2; i++, j--){
if ((str[i] == 'I' && str[j] == 'J') || (str[i] == 'J' && str[j] == 'I'))
continue;
else if (tmp[i] != tmp[j]){
is_pm = true;
break;
}
}
}
//输出的时候需要注意,样例之间需要有一个空行!
if (is_palindrome && is_mirrored)
cout << tmp << " -- is not a palindrome." << endl << endl;
else if (!is_mirrored && is_palindrome && is_pm)
cout << tmp << " -- is a mirrored string." << endl << endl;
else if (!is_palindrome && is_mirrored)
cout << tmp << " -- is a regular palindrome." << endl << endl;
else if (!is_pm)
cout << tmp << " -- is a mirrored palindrome." << endl << endl;
}
system("pause");
}
poj 1590 Palindromes
最新推荐文章于 2017-06-02 21:57:55 发布