2014 04 02 Uva 401 Palindrome

POSTS

问题简介

给定一段由字符与数字组成的字符串,判断它是否是回文字。

如果组成字符串的全部都是自反:如A,T这样的字符,则为mirrored palindrome

或者由E3等mirror字符组成的,则为mirrored string

普通情况下为reqular palindrome

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <vector>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

#define fe(i, a, b) for (int i = int(a); i <= int(b); i++)
#define fu(i, a, b) for (int i = int(a); i < int(b); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<int, int> pii;
#define pb push_back
#define mp make_pair

string chars   = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
string c_revers= "A   3  HIL JM O    TUVWXY51SE Z  8 ";

int solve(string s)
{
    int l = 0, r = s.size() - 1;
    int index = -1;
    char cl, cr;
    bool ismirror = true;
    bool ismirrorp= true;
    for (; l <= r; l++, r--) {
        cl = s[l]; cr = s[r];
        index = chars.find(cl);
        if (cr == c_revers[index] && ismirror) {
            if (cr == cl && ismirrorp) {
                //cout << cr << " " << cl << endl;
                continue;
            } else {
                ismirrorp = false;
                continue;
            }
        } else if (cr != c_revers[index] && ismirror) {
            ismirrorp = false;
            index = c_revers.find(cl);
            if (index == -1) {
                return 0;
            } else {
                if (cr == chars[index] && ismirror) {
                    continue;
                } else {
                    ismirror = false;
                    continue;
                }
            }
        } else if (cl == cr) {
            ismirror = false;
            continue;
        } else {
            return 0;
        }
    }
    if (ismirrorp) return 3;
    if (ismirror)  return 2;
    return 1;
}

int main()
{

    string s;
    string msgs[] = {
        "not a palindrome.",
        "a regular palindrome.",
        "a mirrored string.",
        "a mirrored palindrome."
    };

    while (cin >> s) {
        cout << s << " -- is " << msgs[solve(s)] << endl;
    }

    return 0;
}