UVa401

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=342

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
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);

/*
回文串 (regular palindrome):字串從左到右讀跟從右到左讀一樣。舉例「ABCDEDCBA」。

鏡像字串 (mirrored string):先將字串中每個字元(如果有定義「鏡像字符」的話)替換為其鏡像(例如某些字元自身鏡像、某些字元互為鏡像),
然後將替換後的字串從右到左讀,結果如果跟原字串從左到右讀的一樣,那就是鏡像字串。
如果有任意字元沒有鏡像字符,那他就不是鏡像字串。

鏡像迴文 (mirrored palindrome):即同時滿足「回文」與「鏡像」兩個條件。舉例「ATOYOTA」就是鏡像迴文。
*/

map<char, char> m = {{'A', 'A'}, {'E', '3'}, {'H', 'H'}, {'I', 'I'}, {'J', 'L'}, {'L', 'J'}, {'M', 'M'}, {'O', 'O'},
{'S', '2'}, {'T', 'T'}, {'U', 'U'}, {'V', 'V'}, {'W', 'W'}, {'X', 'X'}, {'Y', 'Y'}, {'Z', '5'}, {'1', '1'},
{'2', 'S'}, {'3', 'E'}, {'5', 'Z'}, {'8', '8'}};

string text;
while(getline(cin, text)){
bool isPalin = false, isMirror = false;
string palin = text;
reverse(palin.begin(), palin.end());
if(palin == text) isPalin = true;
string mirror = text;
for(int i=0; i<mirror.length(); ++i){
if(m.count(mirror[i])) mirror[i] = m[mirror[i]];
else mirror[i] = '?'; // 若無法被替換,則主動替換成非法字元,導致他不可能是mirrored string
}
reverse(mirror.begin(), mirror.end());
if(mirror == text) isMirror = true;

cout << text;
if(isPalin && isMirror) cout << " -- is a mirrored palindrome.";
else if(isPalin) cout << " -- is a regular palindrome.";
else if(isMirror) cout << " -- is a mirrored string.";
else cout << " -- is not a palindrome.";
cout << "\n\n";
}

return 0;
}