算法之路二:刘汝佳算法竞赛入门经典 3.7回文词 UVa401

时间:2022-04-17 20:02:29
#include<stdio.h>
#include<string.h>
#include<ctype.h>
const char*rev="A 3 HIL JM O 2TUVWXY51SE Z 8 ";//镜像串字符常量 
const char*msg[]={"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};//字符常量 

char r(char ch)
{
    if(isalpha(ch)) return rev[ch-'A'];
    return rev[ch-'0'+25];
}//用来之后判断是否为镜像串 

int main()
{
    char s[30];
    while(scanf("%s",s)==1)
    {
        int len=strlen(s);
        int p=1,m=1;
        for(int i=0;i<(len+1)/2;i++)
        {
            if(s[i]!=s[len-1-i]) p=0;//不是回文串 
            if(r(s[i])!=s[len-1-i]) m=0;//不是镜像串 
        }
        printf("%s -- is %s.\n\n",s,msg[m*2+p]);//amazing!!!!! 
    }
    return 0;
}