UVa 401 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.

 

 


Miguel Revilla 2001-04-16

 

回文串判断题,其实代码是一年前写的了,刚学C语言刚接触ACM的时候,结果这个题一直RE怎么都过不了,今天以把代码翻出来,改了一下rev数组的下标和输出格式就过了

不得不说UVa的输出格式要求直挺严的,换行格式什么的,一定要注意,而且没有PE,格式错了都是WA

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 
 4 int change(int len,char a[],char b[])
 5 {
 6     char rev[50]={"A000300HIL0JM0O0002TUVWXY51SE0Z0080"};
 7     int i,j;
 8     if(len%2)
 9     {
10         int t=len/2;
11         if(a[t]>='A'&&a[t]<='Z')
12         {
13             if(a[t]!=rev[a[t]-'A'])
14                 return 0;
15         }
16         else
17             if(a[t]!=rev[a[t]-'1'+26])
18                 return 0;
19     }
20     j=(len%2==0?len/2-1:(len-1)/2);
21     for(i=0;i<=j;i++)
22         if(a[i]>='A'&&a[i]<='Z')
23             if(rev[a[i]-'A']!=0)
24                 b[i]=rev[a[i]-'A'];
25             else
26                 return 0;
27         else
28             if(rev[a[i]-'0']!=0)
29                 b[i]=rev[a[i]-'1'+26];
30             else
31                 return 0;
32     for(i=j+1;i<=len;i++)
33         b[i]=a[i];
34     return 1;
35 }
36 
37 int main()
38 {
39     char x[25],a[25],b[25];
40 
41     while(scanf("%s",x)!=EOF)
42     {
43         int len=strlen(x),isPal=1,isMir=1,i;
44         strcpy(a,x);
45 
46         for(i=0;i<len;i++)
47             if(a[i]=='0')
48                 a[i]='O';
49         for(i=0;(i<len/2)&&isPal;i++)
50         {
51             if(a[i]!=a[len-i-1])
52                 isPal=0;
53         }
54         isMir=change(len,a,b);
55         for(i=0;(i<len/2)&&isMir;i++)
56         {
57             if(b[i]!=b[len-i-1])
58                 isMir=0;
59         }
60         if(isPal&&isMir)
61             printf("%s -- is a mirrored palindrome.\n",x);
62         else if(isPal&&!isMir)
63             printf("%s -- is a regular palindrome.\n",x);
64         else if(!isPal&&isMir)
65             printf("%s -- is a mirrored string.\n",x);
66         else
67             printf("%s -- is not a palindrome.\n",x);
68         printf("\n");
69     }
70 
71     return 0;
72 }
[C]

 

posted @ 2014-01-05 10:44  ~~Snail~~  阅读(212)  评论(0编辑  收藏  举报