UVa401 回文词

Palindromes

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.

 

输入样例:

 

NOTAPALINDROME 
ISAPALINILAPASI 
2A3MEAS 
ATOYOTA

 

输出样例:

NOTAPALINDROME -- is not a palindrome.
 
ISAPALINILAPASI -- is a regular palindrome.
 
2A3MEAS -- is a mirrored string.
 
ATOYOTA -- is a mirrored palindrome.
大意就是判断一个输入的字符串是否为回文和镜像的,因此我写了两个函数。而对于题中表格的处理,本来开始打算用数组的,后来觉得不方便,改用switch语句了。
题目本来不难,注意特殊情况的处理,就是当字符串长度为1的时候。当n==1时,是回文的,当且仅当改字符和本身是镜像的时候,这个字符串是镜像的。
对了,还有很重要的一点就是每次输出后都要再输出一个空行,对于我们这种英语渣渣是怎么会注意到这种东西的。。
  1 #include <iostream>
  2 #include <cstring>
  3 using namespace std;
  4 
  5 const char* res[4] = {" -- is not a palindrome.", " -- is a regular palindrome.",
  6                       " -- is a mirrored string.", " -- is a mirrored palindrome."};
  7 
  8 int main(void)
  9 {
 10     char Reverse(char c);
 11     bool Palindrome(char s[], int n);
 12     bool Mirrored(char s[], int n);
 13     char s[25];
 14     int len;
 15     while(cin >> s)
 16     {
 17          len = strlen(s);
 18          bool flag1 = Palindrome(s, len);
 19          bool flag2 = Mirrored(s, len);
 20          cout << s;
 21          if(!flag1 && !flag2)
 22              cout << res[0] << endl << endl;
 23          if(flag1 && !flag2)
 24              cout << res[1] << endl << endl;
 25          if(!flag1 && flag2)
 26              cout << res[2] << endl << endl;
 27          if(flag1 && flag2)
 28              cout << res[3] << endl << endl;
 29     }
 30     return 0;
 31 }
 32 char Reverse(char c)
 33 {
 34     switch (c)
 35     {
 36         case 'A':
 37         case 'H':
 38         case 'I':
 39         case 'M':
 40         case 'O':
 41         case 'T':
 42         case 'U':
 43         case 'V':
 44         case 'W':
 45         case 'X':
 46         case 'Y':
 47         case '1':
 48         case '8':
 49             return c;
 50         case 'E':
 51             return '3';
 52         case 'J':
 53             return 'L';
 54         case 'L':
 55             return 'J';
 56         case 'S':
 57             return '2';
 58         case 'Z':
 59             return '5';
 60         case '2':
 61             return 'S';
 62         case '3':
 63             return 'E';
 64         case '5':
 65             return 'Z';
 66         default:
 67             return 0;
 68     }
 69 }
 70 //判断是否是回文字符串
 71 bool Palindrome(char s[], int n)
 72 {
 73     int i;
 74     if(n == 1)
 75         return true;
 76     for(i = 0; i < n / 2; ++i)
 77     {
 78         if(s[i] != s[n - 1 - i])
 79             return false;
 80     }
 81     return true;
 82 }
 83 //判断是否是镜像字符串
 84 bool Mirrored(char s[], int n)
 85 {
 86     int i;
 87     if(n == 1)
 88     {
 89         if(Reverse(s[0]) == s[0])
 90             return true;
 91         else
 92             return false;
 93     }
 94     for(i = 0; i < n / 2; ++i)
 95     {
 96         if(Reverse(s[i]) == 0)
 97             return false;
 98         if(Reverse(s[i]) != s[n - 1 - i])
 99             return false;
100     }
101     return true;
102 }
代码君

 

posted @ 2014-07-01 09:10  AOQNRMGYXLMV  阅读(274)  评论(0编辑  收藏  举报