1:判斷迴文串,倒着念跟順着念相同即可,不同直接返回false.
2:判斷鏡像串,只要有逆(進行映射,我處理方法比較複雜,可以用更簡單的方法)則將其逆求出,如果沒有逆元則不是鏡像串。轉換之後從右到左跟原串從左到右比較,看是否相同,如果不相同,則返回不是鏡像。
3:attention please : In addition, after each output line, you must print an empty line. (因爲這個錯了4遍)
// Accepted C++11 0.016
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 1000+10;
char vdata[maxn];
bool is_palo(char *data){
int len = strlen(data);
for(int i = 0; i < len/2; i ++){
if(data[i] != data[len-i-1]) return false;
}
return true;
}
char reverse(char ch){
switch(ch){
case 'A' : return 'A';
case 'E' : return '3';
case 'H' : return 'H';
case 'I' : return 'I';
case 'J' : return 'L';
case 'L' : return 'J';
case 'M' : return 'M';
case 'O' : return 'O';
case 'S' : return '2';
case 'T' : return 'T';
case 'U' : return 'U';
case 'V' : return 'V';
case 'W' : return 'W';
case 'X' : return 'X';
case 'Y' : return 'Y';
case 'Z' : return '5';
case '1' : return '1';
case '2' : return 'S';
case '3' : return 'E';
case '5' : return 'Z';
case '8' : return '8';
default : return '0';
}
}
bool is_mirror(char *data){
int len = strlen(data);
int flag = 1;
memset(vdata, 0, sizeof(vdata));
for(int i = 0; i < len; i ++){
char ch = reverse(data[i]);
//printf("%c\n", ch);
if(ch == '0') { flag = 0; return false; }
vdata[i] = ch;
}
if(flag == 0) return false;
else {
//printf("%s\n", vdata);
for(int i = 0; i < len; i ++){
if(data[i] != vdata[len-i-1]) return false;
}
return true;
}
}
int main()
{
char data[maxn];
while(scanf("%s", data) != EOF){
//printf("%d\n", is_prime(data));
//printf("%d\n", is_mirror(data));
if(!is_palo(data)) {
if(!is_mirror(data)) printf("%s -- is not a palindrome.\n\n", data);
else printf("%s -- is a mirrored string.\n\n", data);
}
else {
if(!is_mirror(data)) printf("%s -- is a regular palindrome.\n\n", data);
else printf("%s -- is a mirrored palindrome.\n\n", data);
}
}
return 0;
}