hdoj 1318(Palindromes)

来源:互联网 发布:淘宝拖动滑块完成验证 编辑:程序博客网 时间:2024/06/05 19:33

回文字符串配对问题

#include<iostream>#include<cctype>#include<string.h>using namespace std;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'];// ch-'A' 意在定位到“表“rev中的相应位置    else return rev[ch - '0' + 25];  // ch-'0'+25 意在定位到“表“rev中的相应位置}int main(void){    freopen("input.txt", "r", stdin);    char s[30];    while (cin >> s)    {        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;        }        cout << s << " -- is " << msg[p + 2 * m] << endl << endl;    }    return 0;}


原创粉丝点击