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

 



题目大意:

回文词(palindrome)就是顺序逆序都相同的词句,例如“ABCDEDCBA”。

镜像词语,就是两个词成镜像相反,例如“A”、:"I"都是自己是自己的镜像,“3”和“E”互成镜像,题目中也列出了大写字母以及数字的镜像。

题目要求就是判断一个字符串是否是回文串、镜像串,根据判断结果有四种解答,分别是:


字符串 标准
" -- is not a palindrome."
既不是回文串,也不是镜像串        
" -- is a regular palindrome." 是回文串,但不是镜像串
" -- is a mirrored string." 是镜像串,但不是回文串
" -- is a mirrored palindrome." 既是回文串,又是镜像串

 

 

解析: 首先用一个字符串数组将所有的镜像单词存储下来char mirror[]={"1SE Z  8        A   3  HIL JM O   2TUVWXY5"};(从1~Z,根据ASCII码,数字后留七个空格,才到大写字母),然后进行镜像的选择。

注意: 1)回文的时候,可能有人使用strrev函数,但它不属于ANSI C标准,所以会出现错误(Compilation error),所以可以自己写回文函数。
    2)不知道为什么,写了一个回文函数,但是一直RE,也没发现有错误,所以我就进行直接首尾比较,判断回文串。
    3)为了避免镜像串也要进行一次回文,所以在进行镜像翻译的时候做了一些改变!


CODE:

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str[1000], mir[1000];
char mirror[]={"1SE Z  8        A   3  HIL JM O   2TUVWXY5"};

int main(){
    int len;
    int i, flag1, flag2;
    while(scanf("%s", str)!=EOF){

        len = strlen(str);
        for(i = 0; i<len; i++){         /*镜像选择*/
            mir[len-i-1] = mirror[str[i]-'1'];
        }
        mir[len] = '\0';                /*最后要有结束的符号*/

        flag1=flag2=0;                  /*标记的初始化,0代表是,非0代表不是*/
        for(i = 0; i<=(len/2); i++){    /*判断是不是回文串*/
            if(str[i]!=str[len-i-1]){
                flag1 = 1;
                break;
            }
        }

        flag2 = strcmp(mir, str);       /*判断是不是镜像串*/

        printf("%s -- ", str);
        if(flag1!=0&&flag2!=0){
            printf("is not a palindrome.");
        }
        else if(flag1==0&&flag2!=0){
            printf("is a regular palindrome.");
        }
        else if(flag1!=0&&flag2==0){
            printf("is a mirrored string.");
        }
        else if(flag1==0&&flag2==0){
            printf("is a mirrored palindrome.");
        }
        printf("\n\n");
    }
}
posted @ 2013-08-08 21:53  pangbangb  阅读(361)  评论(0编辑  收藏  举报