UVA Online Judge 题目401 Palindromes 回文串
问题描述:
回文串(Palindromes)就是正着读和反着读完全一样的字符串,例如"ABCDEDCBA"。
镜像串(Mirrored string)有些类似回文串,字符'3'的镜像可以看成是'E',字符'A'本身就是对称的所以它的镜像字符还是'A'。我们把像"3AIAE"这样的字符串看做镜像串。
镜像回文串(Mirrored palindrome)是符合上面两个条件的字符串,比如"ATOYOTA"。‘A’、‘T’、‘O’、‘Y’几个字符的镜像字符都是其自身。
详细的字符与镜像字符对应表如下:
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |
注意:数字0被看做和字母O相同,并且输入中只包含有字母O。
输入格式:
每行为一个需要判断的字符串,读取到文件结束符后结束。
输出格式:
针对每一行输入,你需要判断其字符串类型并输出原字符串加上语句,见下表:
STRING | CRITERIA |
" -- is not a palindrome." | 既不是回文也不是镜像串 |
" -- is a regular palindrome." | 普通回文串 |
" -- is a mirrored string." | 镜像串 |
" -- is a mirrored palindrome." | 镜像回文串 |
注意:每行语句之间有一个空行,也就是说需要在字符串后面写上“ ”,这个地方WA了一次。。太坑爹了。
示例输入:
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
示例输出:
NOTAPALINDROME -- is not a palindrome. ISAPALINILAPASI -- is a regular palindrome. 2A3MEAS -- is a mirrored string. ATOYOTA -- is a mirrored palindrome.
代码:(没注意输出格式WA一次。。)
1 /* 2 Problem : UVA Online Judge - 401 Palindromes 3 Date:2014-04-03 4 Author:Leroy 5 */ 6 7 #include <stdio.h> 8 #include <string.h> 9 10 char ch[22] = "AEHIJLMOSTUVWXYZ12358"; 11 char chRe[22] = "A3HILJMO2TUVWXY51SEZ8"; 12 13 int hasRe(char c) 14 { 15 int has = 0; 16 for (int i = 0; i < 22; i++) 17 { 18 if (ch[i] == c) 19 has = 1; 20 } 21 return has; 22 } 23 24 char getRe(char c) 25 { 26 for (int i = 0; i < 22; i++) 27 { 28 if (ch[i] == c) 29 return chRe[i]; 30 } 31 } 32 33 int is_P(char* str, int n) 34 { 35 int i; 36 for (i = 0; i < n / 2; i++) 37 { 38 if (str[i] != str[n - i - 1]) 39 return 0; 40 } 41 return 1; 42 } 43 44 int is_M(char* str, int n) 45 { 46 if (n == 1) 47 { 48 if (hasRe(str[0])) 49 { 50 return 1; 51 } 52 else 53 { 54 return 0; 55 } 56 } 57 58 for (int i = 0; i < n / 2; i++) 59 { 60 if (hasRe(str[i])) 61 { 62 char t = getRe(str[i]); 63 if (t != str[n - i - 1]) 64 { 65 return 0; 66 } 67 } 68 else 69 { 70 return 0; 71 } 72 } 73 74 if (n % 2 != 0) 75 { 76 int x = n / 2; 77 if (hasRe(str[x])) 78 { 79 return 1; 80 } 81 else 82 { 83 return 0; 84 } 85 } 86 87 return 1; 88 } 89 90 int main(){ 91 char str[100000]; 92 while (gets(str) != NULL) 93 { 94 int len = strlen(str); 95 if (len == 0) 96 break; 97 int isMirro = 1, isPalin = 1; 98 int i = 0; 99 100 isPalin = is_P(str, len); 101 isMirro = is_M(str, len); 102 103 if (isPalin == 0){ 104 if (isMirro == 0) 105 printf("%s -- is not a palindrome. ", str); 106 else 107 printf("%s -- is a mirrored string. ", str); 108 } 109 else 110 { 111 if (isMirro == 0) 112 printf("%s -- is a regular palindrome. ", str); 113 else 114 printf("%s -- is a mirrored palindrome. ", str); 115 } 116 117 } 118 }