UVA401 Palindromes
题目:
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.
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 =_=||| 发现用多维数组存数据很好用啊。第一次做的时候千万不要进行一次行替换,因为没办法保证原来的字符是对称的。还有注意输出空行。题目里面强调的'O'和'0'好像不影响AC。
代码:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 char s[1000]; 6 char s1[1010]; 7 char map[250]; 8 int main() 9 { 10 map['A']='A'; 11 map['E']='3'; 12 map['H']='H'; 13 map['I']='I'; 14 map['J']='L'; 15 map['L']='J'; 16 map['M']='M'; 17 map['O']='O'; 18 map['S']='2'; 19 map['T']='T'; 20 map['U']='U'; 21 map['V']='V'; 22 map['W']='W'; 23 map['X']='X'; 24 map['Y']='Y'; 25 map['Z']='5'; 26 map['1']='1'; 27 map['2']='S'; 28 map['3']='E'; 29 map['5']='Z'; 30 map['8']='8'; 31 int ispalin(char[],int); 32 int ismirro(char[],int); 33 while(cin>>s) 34 { 35 int len=strlen(s); 36 for(int i=0;i<len;i++) 37 { 38 if(s[i]=='0') 39 s[i]='O'; 40 } 41 memcpy(s1,s,len*sizeof(char)); 42 int p=ispalin(s,len); 43 int m=ismirro(s1,len); 44 if(p==0&&m==0) 45 printf("%s -- is not a palindrome.",s); 46 else if(p==1&&m==0) 47 printf("%s -- is a regular palindrome.",s); 48 else if(p==0&&m==1) 49 printf("%s -- is a mirrored string.",s); 50 else if(p==1&&m==1) 51 printf("%s -- is a mirrored palindrome.",s); 52 cout<<endl; 53 cout<<endl; 54 memset(s,0,sizeof(s)); 55 memset(s1,0,sizeof(s1)); 56 } 57 } 58 //ispalin 59 int ispalin(char s[],int len) 60 { 61 int i,j; 62 for(i=0,j=len-1;j>=len/2;i++,j--) 63 { 64 if(s[i]!=s[j]) return 0; 65 } 66 return 1; 67 } 68 //ismirro 69 int ismirro(char s[],int len) 70 { 71 int i,j; 72 for(i=0,j=len-1;j>=len/2;i++,j--) 73 { 74 if(s[i]!=map[s[j]]) return 0; 75 } 76 return 1; 77 }