`
SunnyYoona
  • 浏览: 361502 次
社区版块
存档分类
最新评论

UVA 之401 - 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


Notethat O (zero) and 0 (the letter) are considered the same character and thereforeONLYthe 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

Notethat 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.

【注意】:

题目意思:

判断输入的字符串是否是回文串还是镜面字符串。

回文串:从左到右读和从右到左读是一样的。

镜面字符串:如果某个字符有镜面字符,转换为相应的镜面字符。转换后从左到右读和从右到左读是一样的。

数字0和字母O是等价的。

【代码】:

  1. /*********************************
  2. *日期:2013-4-22
  3. *作者:SJF0115
  4. *题号:题目401-Palindromes
  5. *来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=342
  6. *结果:AC
  7. *来源:UVA
  8. *总结:
  9. **********************************/
  10. #include<stdio.h>
  11. #include<string.h>
  12. charMirroredStr[]={'A','','','','3','','','H',
  13. 'I','L','','J','M','','O','','','','2','T','U','V','W','X','Y','5'};
  14. charMirroredNum[]={'1','S','E','','Z','','','8',''};
  15. //判断是否是回文
  16. intIsPalindromes(charStr[]){
  17. inti,j;
  18. for(i=0,j=strlen(Str)-1;i<j;i++,j--){
  19. //0和O等价
  20. if((Str[i]=='0'&&Str[j]=='O')||(Str[i]=='O'&&Str[j]=='0')){
  21. continue;
  22. }
  23. if(Str[i]!=Str[j]){
  24. return0;
  25. }
  26. }
  27. return1;
  28. }
  29. //判断是否是镜像字符串
  30. intIsMirrored(charStr[]){
  31. chartemp[21];
  32. inti,j,index;
  33. intlen=strlen(Str);
  34. for(i=0,j=len-1;i<len;i++,j--){
  35. if(Str[i]>='A'&&Str[i]<='Z'){
  36. index=Str[i]-'A';
  37. temp[i]=MirroredStr[index];
  38. }
  39. else{
  40. index=Str[i]-'1';
  41. temp[i]=MirroredNum[index];
  42. }
  43. //0和O等价
  44. if((temp[i]=='0'&&Str[j]=='O')||(temp[i]=='O'&&Str[j]=='0')){
  45. continue;
  46. }
  47. if(temp[i]!=Str[j]){
  48. return0;
  49. }
  50. }
  51. return1;
  52. }
  53. intmain(){
  54. charStr[21];
  55. inti,len,IsP,IsM;
  56. //freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);
  57. while(scanf("%s",Str)!=EOF){
  58. len=strlen(Str);
  59. printf("%s--",Str);
  60. //回文字符串
  61. IsP=IsPalindromes(Str);
  62. //镜面字符串
  63. IsM=IsMirrored(Str);
  64. //输出
  65. if(IsP==0&&IsM==0){
  66. printf("isnotapalindrome.\n");
  67. }
  68. elseif(IsP==1&&IsM==1){
  69. printf("isamirroredpalindrome.\n");
  70. }
  71. elseif(IsP==1&&IsM==0){
  72. printf("isaregularpalindrome.\n");
  73. }
  74. elseif(IsP==0&&IsM==1){
  75. printf("isamirroredstring.\n");
  76. }
  77. printf("\n");
  78. }
  79. return0;
  80. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics