【UVA – 401】Palindromes【紫书】

【UVA – 401】Palindromes【紫书】

问题链接:https://vjudge.net/problem/UVA-401

udebug数据链接:https://www.udebug.com/UVa/401

C++ solution

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

char alpha[] = "A   3  HIL JM O   2TUVWXY5";
char digit[] = "O1SE Z  8 ";

const int LIM = 1000;
char s[LIM];

int main(void) {
    while (gets(s)) {
        //reverse
        int len = strlen(s);
        bool pf = true, mf = true;
        for (int i = 0; i < len; i++) {
            if (!pf && !mf) break;
            //mirror check
            if (isdigit(s[i])) {
                if (s[len - i - 1] != digit[s[i] - '0']) mf = false;
            } else if (isalpha(s[i])) {
                if (s[len - i - 1] != alpha[s[i] - 'A']) mf = false;
            }
            //palindrome check
            if (s[i] != s[len - i - 1]) pf = false;
        }
        printf("%s", s);
        if (pf && mf) printf(" -- is a mirrored palindrome.");
        else if (pf) printf(" -- is a regular palindrome.");
        else if (mf) printf(" -- is a mirrored string.");
        else printf(" -- is not a palindrome.");
        printf("\n\n");
    }
    return 0;
}

 

相关阅读:

《算法竞赛入门经典》(紫书)全AC代码