紫书上的例题,看了书之后做的,发现自己做还是有些地方想不好。题目Vjudge地址,大意是判断一个词是不是回文词和镜像词,回文词很好判断,镜像词书上是创建常量数组。需要注意判镜像和判回文的一点区别。
贴AC代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
char* msg[]={"is not a palindrome.", "is a regular palindrome.",
"is a mirrored string.", "is a mirrored palindrome."};
char rev[]="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
char r(char c){
if(isalpha(c))return (rev[c-'A']);
else return rev[c-'0' + 25];
}
int main()
{
char s[25];
while(~scanf("%s", s)){
int len = strlen(s);
int m = 1;
int p = 1;
for(int i = 0; i < (len+1) / 2; i++){
//这里wa过,因为len是奇数时回文词不用管正中间的字母,而镜像词需要判断正中间的字母是否对称
if(s[i] != s[len-1-i]) p = 0;
if(r(s[i]) != s[len-1-i]) m = 0;
}
printf("%s -- %s\n\n", s, msg[p + m*2]);
}
return 0;
}