题目链接:http://acm.pku.edu.cn/JudgeOnline/problem?id=1590
这个题有两个学点:
1,对于这种映射关系应该如何处理?
处理方法:
如果有规律可以通过循环来设置;如果没有规律只能手动一个一个设置。
存储方式:
通过直接的哈希表,如本题的解决方法;用map数据结构实现映射;用两个不同的容器在对应位置存储映射的值。
如可以
char source[22] = {"AEHIJLMOSTUVWXYZ12358"};
char reverse[22] ={"A3HILJMO2TUVWXY51SEZ8"};
来存储字符,但是不便于mirror的判定
2,mirro的判定中由于没有考虑单个字符(由此波及的奇数字符)情况WA了一次。
#include <iostream>
using namespace std;
#define MAX 21
char mirr['Z'];
char text[MAX];
bool first;
int len;
bool IsReg()
{
int i,j;
i = 0,j = len - 1;
while(i < j)
{
if(text[i] != text[j])
return false;
++i;
--j;
}
return true;
}
bool IsMir()
{
int i,j;
i = 0,j = len - 1;
while(i < j)
{
if(text[i] != mirr[text[j]])
return false;
++i;
--j;
}
if(i == j)
if(text[i] != mirr[text[j]])
return false;
return true;
}
int main()
{
mirr['E'] = '3';
mirr['J'] = 'L';
mirr['L'] = 'J';
mirr['S'] = '2';
mirr['Z'] = '5';
mirr['2'] = 'S';
mirr['5'] = 'Z';
mirr['3'] = 'E';
mirr['A'] = 'A';
mirr['H'] = 'H';
mirr['I'] = 'I';
mirr['M'] = 'M';
mirr['O'] = 'O';
mirr['T'] = 'T';
mirr['U'] = 'U';
mirr['V'] = 'V';
mirr['W'] = 'W';
mirr['X'] = 'X';
mirr['Y'] = 'Y';
mirr['8'] = '8';
mirr['1'] = '1';
freopen("in.txt","r",stdin);
first = true;
while(cin >> text)
{
if(!first)
cout << endl;
first = false;
len = strlen(text);
if(IsReg() && IsMir())
cout << text << " -- is a mirrored palindrome." << endl;
else if(IsReg())
cout << text << " -- is a regular palindrome." << endl;
else if(IsMir())
cout << text << " -- is a mirrored string." << endl;
else
cout << text << " -- is not a palindrome." << endl;
}
return 0;
}