AC:Time(29ms) C++ 4.8.2
#include<stdio.h> #include<string.h> char * mirstr = "AAE3EHHIIJLJMM0OS2STTUUVVWWXXYYZ5Z88"; int dispose(char *); int main(void) { char str[25]; int state; while(scanf("%s", str) == 1) { state = dispose(str); switch(state) { case 1: printf("%s -- is not a palindrome.\n\n", str); break; case 2: printf("%s -- is a regular palindrome.\n\n", str); break; case 3: printf("%s -- is a mirrored string.\n\n", str); break; case 4: printf("%s -- is a mirrored palindrome.\n\n", str); break; } } return 0; } int dispose(char * str) { int i; bool palin = true; bool mirr = true; int len = strlen(str); int half_len = len / 2; // 判断回文 for(i = 0; i < half_len; i++) if(str[i] != str[len-1-i]) palin = false; char * p; // 判断镜像 for(i = 0; i < len; i++) { p = strchr(mirstr, str[i]); if(!p) { mirr = false; break; } if(p[0] == ‘O‘ || p[0] == ‘0‘) // 字母O和数字0单独处理 { // 一开始这个花括号忘记了,导致最后一个测试数据失败,情况四变成了情况二,通过调试检查出了错误。 if(str[len-1-i] != ‘O‘ && str[len-1-i] != ‘0‘) { mirr = false; break; } } else if(p[1] != str[len-1-i]) { mirr = false; break; } } if(palin == false && mirr == false) return 1; else if(palin == true && mirr == false) return 2; else if(palin == false && mirr == true) return 3; else return 4; }
时间: 2024-10-02 02:45:31