您的当前位置:首页正文

UVa 401 - Palindromes(回文词)

2024-11-18 来源:个人技术集锦
#include <iostream>
#include <cstdio>
#include <cstring>

char c[] = "AEHIJLMOSTUVWXYZ12358";
char r[] = "A3HILJMO2TUVWXY51SEZ8";
char out[4][50] =
{" -- is not a palindrome.",
" -- is a regular palindrome.",
" -- is a mirrored string.",
" -- is a mirrored palindrome."};
char buff[200];

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif

    char *p;
    int n, i, pos;
    //1:not a palindrome 2:palindrome 3:mirrored string 4:mirrored palindrome
    int status;
    //equ:对应位置是否相等; inbuff:是否在buff中,且满足对应关系
    bool equ, inbuff;
    while(scanf("%s", buff) != EOF)
    {
        n = strlen(buff);
        equ = inbuff = true;

        for(i=0; i<=n/2; i++) {
            if(buff[i] != buff[n-1-i]) equ = false;
            if((p = strchr(c, buff[i])) == NULL) inbuff = false;
            else{
                pos = p - c;
                if(r[pos] != buff[n-1-i]) inbuff = false;
            }
        }
        if(equ) {
            if(inbuff) status = 4;
            else status = 2;
        } else {
            if(inbuff) status = 3;
            else status = 1;
        }
        printf("%s%s\n\n", buff, out[status-1]);
    }

    return 0;
}

显示全文