这个题目本身不难,但是个人觉得题目本身存在两个表述不清楚的地方,导致解题时出现多次答案错误的提示:
1.数字'0'和字母'O'。题述这两个字符时相等的,并且只有字母'O'是valid character,数字'0'是invalid character。那么按照题目的表述,输入中应该不会出现数字'0'。很多人都对这点有争论,我对此进行了验证,WA的原因基本于此无关,我的代码没有对数字'0'和字母'O'进行任何处理和判断,一样可以得到 AC;
2.关于mirrored string的判断。这一点是造成WA的最重要的原因。题目只告诉我们什么样的字符串是 mirrored string,但是没有告诉我什么样的字符串不是。这就很容易忽略掉一个条件:如果字符串中含有不能翻转的字母,那么这个字符串就不能成为mirrored string。
#include <stdio.h> #include <string.h> char alpha[]={'A','E','H','I','J','L','M','O','S','T','U','V','W','X','Y','Z','1','2','3','5','8'}; int mirror(char *src) { int i, flag = 1; for(i = 0;i < strlen(src);i++) { if(strchr(alpha,src[i])==NULL) flag = 0; if(src[i] == 'E') { src[i] = '3'; continue; } if(src[i] == 'J') { src[i] = 'L'; continue; } if(src[i] == 'L') { src[i] = 'J'; continue; } if(src[i] == 'S') { src[i] = '2'; continue; } if(src[i] == 'Z') { src[i] = '5'; continue; } if(src[i] == '2') { src[i] = 'S'; continue; } if(src[i] == '3') { src[i] = 'E'; continue; } if(src[i] == '5') { src[i] = 'Z'; continue; } } return flag; } int main() { char src[25], dest[25]; while(scanf("%s",src) != EOF) { int i = 0, j = 0, a = 0, b = 0, flag = 0; printf("%s", src); for(i = strlen(src)-1; i >= 0; i--) dest[j++] = src[i]; dest[j] = '\0'; if(!strcmp(src,dest)) a = 1; flag = mirror(dest); if(!strcmp(src,dest) && flag == 1) b = 1; if(a == 0 && b == 0) printf(" -- is not a palindrome.\n\n"); else if(a == 1 && b == 0) printf(" -- is a regular palindrome.\n\n"); else if(a == 0 && b == 1) printf(" -- is a mirrored string.\n\n"); else printf(" -- is a mirrored palindrome.\n\n"); memset(src,'0',sizeof(src)); memset(dest,'0',sizeof(dest)); } return 0; }