#include <iostream> #include <cstdio> #include <algorithm> #include <string> using namespace std; int main() { string s; while(cin >> s) { int len = s.size(); for(int i = 0; i < len; i++) { if(s[i] == '0') s[i] = 'O'; } int sin_dou = len%2; //表示字符串的奇偶 int half_len = len/2; int i, node = 0; for(i = 0; i < half_len; i++) {//判断是否是回文串 if(s[i] == s[len-i-1]) continue; else break; } if(i == half_len) {//如果是回文串的话 for(i = 0; i < half_len; i++) {//用node来统计镜像字符 if(s[i] == 'A' || s[i] == 'H' || s[i] == 'I' || s[i] == 'M' || s[i] == 'O' || s[i] == 'T' || s[i] == 'U' || s[i] == 'V' || s[i] == 'W' || s[i] == 'X' || s[i] == 'Y' || s[i] == '1' || s[i] == '8') node++; } int f = 0; //如果是偶数 if(sin_dou == 0) f = 1; else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' || s[half_len] == '1' || s[half_len] == '8') f = 1; else f = 0; //如果node == half_len && f == 1说明,不仅是回文串,而且是镜像回文串,也就是回文串中的每个字母都有对应的镜像字母 if(node == half_len && f == 1) cout << s << " -- is a mirrored palindrome." << endl << endl; //否则,就是一个普通的回文串 else cout << s << " -- is a regular palindrome." << endl << endl; } else//如果不是回文串的话 { for(i = 0; i < half_len; i++) {//判断是否是镜像字符串 if(s[i] == s[len-i-1]) { if(s[i] == 'B' || s[i] == 'C' || s[i] == 'D' || s[i] == 'F' || s[i] == 'G' || s[i] == 'K' || s[i] == 'N' || s[i] == 'P' || s[i] == 'Q' || s[i] == 'R' || s[i] == '4' || s[i] == '6' || s[i] =='7' || s[i] == '9') break; else continue; } else if((s[i] == 'E' && s[len-i-1] == '3') || (s[i] == '3' && s[len-i-1] == 'E') || (s[i] == '2' && s[len-i-1] == 'S') || (s[i] == 'S' && s[len-i-1] == '2') || (s[i] == 'Z' && s[len-i-1] == '5') || (s[i] == '5' && s[len-i-1] == 'Z') || (s[i] == 'J' && s[len-i-1] == 'L') || s[i] == 'L' && s[len-i-1] == 'J') continue; else break; } int f = 0; if(sin_dou == 0) f = 1; else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' || s[half_len] == '1' || s[half_len] == '8') f = 1; else f = 0; //如果i == half_len 且 f == 1的话,说明是一个镜像字符串 if(i == half_len && f == 1) { cout << s << " -- is a mirrored string." << endl << endl; } //否则,字符串s是一个普通的字符串 else cout << s << " -- is not a palindrome." << endl << endl; } } return 0; }
原文:http://blog.csdn.net/bao_libra/article/details/45749277