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
  
  这一题是判断字符串是回·镜、回·非镜、非回·镜、非回·非镜四种情况的哪一种。我的办法是先判断是否是回文串,是的话那么就只有回·镜、回·非镜两种可能,否则就是非回·非镜的其中一种。然后再分别判断。
  这里我使用了4个常量数组,一个是储存了可以和自己成镜像的符号;第二个是存储了所有不能和自己成镜像,但有其他与其对应成镜像的符号;第三个与第二个数组的元素分别成镜像;剩下的符号都储存进第四个数组。
  这样,当字符串是回文串的时候,将其元素逐个与第二个数组和第四个数组的元素做比较,如果发现有相同的话就一定是回·非镜。否则比较完字符串所有的都没有发现相同的就是回·镜。
  当字符串不是回文串的时候,那么将其中的元素以及这个元素在字符串上面处于对称位置的元素先与第一个数组里面的元素比较,如果没发现相同的再与这两个元素在分别对应着第二第三个数组检查。最后判断出是非回文串的哪一种情况。
  这里常量数组里面只需要有0,不需要有O,因为这里选了0作为合法符号,而非O。
  输出按照题目要求就可以。
 
 
 
  上代码:
#include <stdio.h>
#include <string.h>
char morrior0[]="AHIMTUVWXY180 ",morrior1[]="EJLSZ235",morrior2[]="3LJ25SEZ",palindrome[]="BCDFGKNPQR4679";

int ispalindrome(char s[],int l)
{
    int i,j;
    for(i=0;i<l;i++)
    {
        for(j=0;palindrome[j]!='\0';j++)
        {
            if(palindrome[j]==s[i]) return 1;
        }
        for(j=0;morrior1[j]!='\0';j++)
        {
            if(morrior1[j]==s[i]) return 1;
        }
    }
    return 0;
}

int ismorrior(char s[],int l)
{
    int i,j,n,m;
    n=l-1;
    for(i=0;i<=n;i++,n--)
    {
        m=0;
        for(j=0;j<16;j++)
        {
            if(s[i]==morrior0[j] && s[n]==morrior0[j]) {m=1;break;}
        }
        if(!m)
        {
            for(j=0;j<8;j++)
          {
            if(s[i]==morrior1[j] && s[n]==morrior2[j]) break;
          }
          if(j==8) return 0;
        }
    }
    return 1;
}

int main()
{
    int i,n,l,m,t;
    char s[30];
    while(gets(s))
    {
        m=1;
        t=0;
        l=strlen(s);
        n=l-1;
        for(i=0;i<=n;i++,n--)
        {
            if(s[i]!=s[n]) {m=0;break;}
        }
        if(m)
        {
            t=ispalindrome(s,(l+1)/2);
            if(t) printf("%s -- is a regular palindrome.",s);
            else printf("%s -- is a mirrored palindrome.",s);
        }
        else
        {
            t=ismorrior(s,l);
            if(t) printf("%s -- is a mirrored string.",s);
            else printf("%s -- is not a palindrome.",s);
        }
        printf("\n\n");
        memset(s,0,sizeof(s));
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sineatos/p/2908273.html