基础题,使用 <algorithm> 的库函数 reverse_copy,将原始字符串逆序存储,然后依次比较是否为回文和镜像。
#include <iostream>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXL = 25;
char before[MAXL], after[MAXL]; //原始字符串,逆序后的字符串
map<char, char> mp; //原字符-镜像
//初始化对应镜像
void mirroredInit()
{
mp['A'] = 'A'; mp['E'] = '3'; mp['H'] = 'H'; mp['I'] = 'I';
mp['J'] = 'L'; mp['L'] = 'J'; mp['M'] = 'M'; mp['O'] = 'O';
mp['S'] = '2'; mp['T'] = 'T'; mp['U'] = 'U'; mp['V'] = 'V';
mp['W'] = 'W'; mp['X'] = 'X'; mp['Y'] = 'Y'; mp['Z'] = '5';
mp['1'] = '1'; mp['2'] = 'S'; mp['3'] = 'E'; mp['5'] = 'Z'; mp['8'] = '8';
}
int main()
{
mirroredInit();
while (cin >> before)
{
int len = strlen(before);
reverse_copy(before, before + strlen(before), after);
bool flagP = true, flagM = true; //是否回文,是否镜像
for (int i = 0; i < len; i++) //判断是否回文
{
if (before[i] != after[i])
flagP = false;
}
for (int i = 0; i < len; i++) //判断是否镜像
{
if (mp.find(after[i]) == mp.end())
flagM = false;
else if (before[i] != mp[after[i]])
flagM = false;
}
cout << before;
if (!flagP && !flagM)
cout << " -- is not a palindrome." << endl;
else if (flagP && !flagM)
cout << " -- is a regular palindrome." << endl;
else if (!flagP && flagM)
cout << " -- is a mirrored string." << endl;
else
cout << " -- is a mirrored palindrome." << endl;
cout << endl;
memset(before, 0, sizeof(before));
memset(after, 0, sizeof(after));
}
return 0;
}
继续加油。