UVA401 - Palindromes

1、题目名称

Palindromes

2、题目地址

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342

3、题目内容

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.

晚上没事干,做了一个水题,问题很大。提交了好几次都是wa。
这道题的思想很简单,写两个函数,分别判断是不是回文和镜像,在写镜像的时候有点蒙蔽,不过还是用string.find 来对应关系,如果用if判断的话会很累。 思想要灵活。
wa的原因主要是没有在镜像函数中重视o和0的关系,例如输入AO0A是回文而不是回文镜像,以及APA,输出的是回文镜像但是实际答案是回文,(原因在于,P不在定义的两个数组中,结果find返回的都是-1,发生错误,所以又加了一句判断,字符是否在str1中)

 

 1 #include"iostream"
 2 #include"string"
 3 using namespace std;
 4 bool palindromes(string str){
 5     int n=str.length();
 6     for(int i=0;i<=n/2;i++){
 7         if((str[i]=='O'&&str[n-i-1]=='0')||(str[i]=='0'&&str[n-i-1]=='O'))
 8             continue;
 9         if(str[i]!=str[n-i-1])
10             return false;
11     }
12     return true;
13 }
14 bool mirrored(string str){
15     int n=str.length();
16     string str1="AEHIJLMOSTUVWXYZ12358";
17     string str2="A3HILJMO2TUVWXY51SEZ8";
18     for(int i=0;i<=n/2;i++){
19         if((str[i]=='O'&&str[n-i-1]=='0')||(str[i]=='0'&&str[n-i-1]=='O'))
20             continue;
21         if(str1.find(str[i],0)==-1)
22             return false;
23         if(str1.find(str[i],0)!=str2.find(str[n-i-1],0))
24             return false;
25     }
26     return true;    
27 }
28     
29 int main(){
30     string s;
31     while(cin>>s){
32         if(palindromes(s)&&mirrored(s))
33             cout<<s<<" -- is a mirrored palindrome."<<endl;
34         else if(palindromes(s))
35             cout<<s<<" -- is a regular palindrome."<<endl;
36         else if(mirrored(s))
37             cout<<s<<" -- is a mirrored string."<<endl;
38         else 
39             cout<<s<<" -- is not a palindrome." <<endl;
40         cout<<endl;
41     }
42     return 0;
43 }

 

posted @ 2016-04-14 23:59  Hutonm  阅读(200)  评论(0编辑  收藏  举报