回文词
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld
& %llu
Submit Status Practice UVA
401
Appoint description:
acmparand (2013-07-07)Luke (2014-01-20)
System Crawler (2015-05-10)
Description
A regular palindrome is a string of numbers or letters that is the same forward
as backward. For example, the string "ABCDEDCBA" is a palindrome because
it is the same when the string is read from left to right as when the string is read
from right to left.
A mirrored string is a string for which when each of the elements of the string
is changed to its reverse (if it has a reverse) and the string is read backwards the
result is the same as the original string. For example, the string "3AIAE" is a
mirrored string because "A" and "I"are their own reverses, and "3" and "E"
are each others‘ reverses.
A mirrored palindrome is a string that meets the criteria of a regular palindrome and
the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because
if the string is read backwards, the string is the same as the original and because if each
of the characters is replaced by its reverse and the result is read backwards, the result is the
same as the original string.
Of course,"A","T", "O",
and "Y" are all their own reverses.
A list of all valid characters and their reverses is as follows.
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 |
Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the
letter "0" is a valid character.
Input
Input consists of strings (one per line) each of which will consist of one to twenty valid characters.
There will be no invalid characters in any of the strings. Your program should read to the end of file.
Output
For each input string, you should print the string starting in column 1 immediately followed by exactly one of
the following strings.
STRING | CRITERIA |
" -- is not a palindrome." | if the string is not a palindrome and is not a mirrored string |
" -- is a regular palindrome." | if the string is a palindrome and is not a mirrored string |
" -- is a mirrored string." | if the string is not a palindrome and is a mirrored string |
" -- is a mirrored palindrome." | if the string is a palindrome and is a mirrored string |
Note that the output line is to include the -‘s and spacing exactly as shown in the table above and
demonstrated in the Sample Output below.
In addition, after each output line, you must print an empty line.
Sample Input
NOTAPALINDROME ISAPALINILAPASI 2A3MEAS ATOYOTA
Sample Output
NOTAPALINDROME -- is not a palindrome. ISAPALINILAPASI -- is a regular palindrome. 2A3MEAS -- is a mirrored string. ATOYOTA -- is a mirrored palindrome.
Hint
use the C++‘s class of string will be convenient, but not a must
代码:
#include <iostream> #include <cstdio> #include <algorithm> #include <string> using namespace std; int main() { string s; while(cin >> s) { int len = s.size(); for(int i = 0; i < len; i++) { if(s[i] == '0') s[i] = 'O'; } int sin_dou = len%2; //表示字符串的奇偶 int half_len = len/2; int i, node = 0; for(i = 0; i < half_len; i++) {//判断是否是回文串 if(s[i] == s[len-i-1]) continue; else break; } if(i == half_len) {//如果是回文串的话 for(i = 0; i < half_len; i++) {//用node来统计镜像字符 if(s[i] == 'A' || s[i] == 'H' || s[i] == 'I' || s[i] == 'M' || s[i] == 'O' || s[i] == 'T' || s[i] == 'U' || s[i] == 'V' || s[i] == 'W' || s[i] == 'X' || s[i] == 'Y' || s[i] == '1' || s[i] == '8') node++; } int f = 0; //如果是偶数 if(sin_dou == 0) f = 1; else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' || s[half_len] == '1' || s[half_len] == '8') f = 1; else f = 0; //如果node == half_len && f == 1说明,不仅是回文串,而且是镜像回文串,也就是回文串中的每个字母都有对应的镜像字母 if(node == half_len && f == 1) cout << s << " -- is a mirrored palindrome." << endl << endl; //否则,就是一个普通的回文串 else cout << s << " -- is a regular palindrome." << endl << endl; } else//如果不是回文串的话 { for(i = 0; i < half_len; i++) {//判断是否是镜像字符串 if(s[i] == s[len-i-1]) { if(s[i] == 'B' || s[i] == 'C' || s[i] == 'D' || s[i] == 'F' || s[i] == 'G' || s[i] == 'K' || s[i] == 'N' || s[i] == 'P' || s[i] == 'Q' || s[i] == 'R' || s[i] == '4' || s[i] == '6' || s[i] =='7' || s[i] == '9') break; else continue; } else if((s[i] == 'E' && s[len-i-1] == '3') || (s[i] == '3' && s[len-i-1] == 'E') || (s[i] == '2' && s[len-i-1] == 'S') || (s[i] == 'S' && s[len-i-1] == '2') || (s[i] == 'Z' && s[len-i-1] == '5') || (s[i] == '5' && s[len-i-1] == 'Z') || (s[i] == 'J' && s[len-i-1] == 'L') || s[i] == 'L' && s[len-i-1] == 'J') continue; else break; } int f = 0; if(sin_dou == 0) f = 1; else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' || s[half_len] == '1' || s[half_len] == '8') f = 1; else f = 0; //如果i == half_len 且 f == 1的话,说明是一个镜像字符串 if(i == half_len && f == 1) { cout << s << " -- is a mirrored string." << endl << endl; } //否则,字符串s是一个普通的字符串 else cout << s << " -- is not a palindrome." << endl << endl; } } return 0; }