题目:UVa401
地址:https://vjudge.net/problem/UVA-401
题解:
给你一个字符串,让你判断它是否具有回文性质、镜像性质。
思路:
根据题意,直接首尾判断就行。
编程技巧:
① 可以用常量字符数组读入镜像表
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
char table[] = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; ///用常量数组储存对应的关系
string s;
int main(){
while(cin >> s){
int len = s.length();
int flag1 = 1; ///标记是否满足回文性质
int flag2 = 1; ///标记是否满足镜像性质
for(int i = 0;i <= len/2;++i){
if(s[i]!=s[len-1-i]) flag1 = 0; ///判断回文性质
char ch;
if(isdigit(s[i])) ///获取对应的镜像字符
ch = table[s[i]-'0'+25];
else ch = table[s[i]-'A'];
if(ch!=s[len-1-i]) flag2 = 0; ///判断镜像性质
if(!flag1&&!flag2) break;
}
cout << s; ///输出
printf(" -- ");
if(!flag1&&!flag2)
printf("is not a palindrome.\n");
if(flag1&&!flag2)
printf("is a regular palindrome.\n");
if(!flag1&&flag2)
printf("is a mirrored string.\n");
if(flag1&&flag2)
printf("is a mirrored palindrome.\n");
cout << endl;
}
return 0;
}