这个题的核心就是判断镜像字符串,镜像字符串是不能包含 没有reverse的字符的,例如B C
代码:
#include <cstdio>
#include <cstring>
char s[100];
bool is_h()
{
int len = strlen(s);
if(len == 1)
return 1;
int i = 0,j = len - 1;
while(s[i] == s[j] && i < j)
{
++i;
--j;
}
return i >= j;
}
struct letter
{
char re;
int flag;
} a[50];
struct digit
{
char re;
int flag;
} b[50];
void init()
{
for(int i = 0; i < 26; ++i)
{
if('A' + i == 'A')
{
a[i].re = 'A';
a[i].flag = 1;
}
else if('A' + i == 'E')
{
a[i].re = '3';
a[i].flag = 1;
}
else if('A' + i == 'H')
{
a[i].re = 'H';
a[i].flag = 1;
}
else if('A' + i == 'I')
{
a[i].re = 'I';
a[i].flag = 1;
}
else if('A' + i == 'J')
{
a[i].re = 'L';
a[i].flag = 1;
}
else if('A' + i == 'L')
{
a[i].re = 'J';
a[i].flag = 1;
}
else if('A' + i == 'M')
{
a[i].re = 'M';
a[i].flag = 1;
}
else if('A' + i == 'O')
{
a[i].re = 'O';
a[i].flag = 1;
}
else if('A' + i == 'S')
{
a[i].re = '2';
a[i].flag = 1;
}
else if('A' + i == 'T')
{
a[i].re = 'T';
a[i].flag = 1;
}
else if('A' + i == 'U')
{
a[i].re = 'U';
a[i].flag = 1;
}
else if('A' + i == 'V')
{
a[i].re = 'V';
a[i].flag = 1;
}
else if('A' + i == 'W')
{
a[i].re = 'W';
a[i].flag = 1;
}
else if('A' + i == 'X')
{
a[i].re = 'X';
a[i].flag = 1;
}
else if('A' + i == 'Y')
{
a[i].re = 'Y';
a[i].flag = 1;
}
else if('A' + i == 'Z')
{
a[i].re = '5';
a[i].flag = 1;
}
}
for(int i = 0; i < 9; ++i)
{
if('1' + i == '1')
{
b[i].re = '1';
b[i].flag = 1;
}
else if('1' + i == '2')
{
b[i].re = 'S';
b[i].flag = 1;
}
else if('1' + i == '3')
{
b[i].re = 'E';
b[i].flag = 1;
}
else if('1' + i == '5')
{
b[i].re = 'Z';
b[i].flag = 1;
}
else if('1' + i == '8')
{
b[i].re = '8';
b[i].flag = 1;
}
}
}
bool is_j()
{
int len = strlen(s);
int i,j;
for(i = 0,j = len - 1; i <= j; ++i,--j)
{
if(s[j] >= 'A' && s[j] <= 'Z')
{
if(a[s[j] - 'A'].flag)
{
if(a[s[j] - 'A'].re != s[i])
break;
}
else
{
return 0;
}
}
else if(s[j] >= '1' && s[j] <= '9')
{
if(b[s[j] - '1'].flag)
{
if(b[s[j] - '1'].re != s[i])
break;
}
else
{
return 0;
}
}
}
return i > j;
}
int main()
{
init();
while(~scanf("%s",s))
{
int f1 = is_h();
int f2 = is_j();
if(!f1 && !f2)
printf("%s -- is not a palindrome.\n\n",s);
else if(f1 && !f2)
printf("%s -- is a regular palindrome.\n\n",s);
else if(!f1 && f2)
printf("%s -- is a mirrored string.\n\n",s);
else
printf("%s -- is a mirrored palindrome.\n\n",s);
//printf("%d %d\n",f1,f2);
}
}