UVa 401 - Palindromes
题意
输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后和原串相同,如abba和madam。所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。
思路
水题
用字符串记录一下镜像表即可
但是要注意几个细节:
1. 当已经判断某串为镜像/镜像回文时 , 如果串长为奇数, 要特判最中间元素是否镜像, 如果最中间元素不镜像那么这就是一个普通回文串
2. 特判串长为1的 , 如果该元素镜像则是镜像回文, 否则只是回文
AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 30;
char s[maxn];
char ms[] = " SE Z 3 L J 2 5";
char mp[] = "1 8 A HI M O TUVWXY ";
int main()
{
int len;
while( ~scanf("%s",s) )
{
len = strlen(s);
bool MS = false, MP = false, OR = false;
if( len == 1 ){
if( mp[s[0]-'1'] != ' ' ) MP = true;
else OR = true;
}
for( int i = 0; i < len/2; i++ ){
if( s[i] == s[len-i-1]){
if( mp[s[i]-'1'] != ' ' ){
MP = true;
if(MS) MP = false;
if(OR) MP = false;
}
else{
OR = true;
MP = false;
}
}
else{
if( ms[s[i]-'1'] == s[len-i-1] ){
MS = true;
if(MP) MP = false;
if(OR) MS = false;
}
else{
OR = false;
break;
}
}
//printf("%d & %d\n",i,len-i-1);
//printf("MP:%d MS:%d OR:%d\n",MP,MS,OR);
}
if( len % 2 != 0 ){
if( MP && mp[s[len/2]-'1'] == ' ' ){
MP = false;
OR = true;
}
else if( MS && mp[s[len/2]-'1'] == ' ' ){
MS = false;
OR = false;
}
}
if( MS ) cout << s << " -- is a mirrored string." << endl << endl;
else if( MP ) cout << s << " -- is a mirrored palindrome." << endl << endl;
else if( OR ) cout << s << " -- is a regular palindrome." << endl << endl;
else cout << s << " -- is not a palindrome." << endl << endl;
}
return 0;
}