Palindromes
UVA - 401
题目传送门
题目大意:给你一个字符串,判断其是回文串还是镜像串。
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
char s1[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"};
char s2[]={"A 3 HIL JM O 2TUVWXY51SE Z 8 "};
char ss1[maxn],ss2[maxn];
char str[maxn];
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);map<char,char>m;rep(i,0,35) {m[s1[i]]=s2[i];}while(scanf("%s",str)!=EOF){ms(ss1);ms(ss2);bool ju1=true,ju2=true; //ju1表示它是回文串,ju2表示它是镜像串int len=strlen(str);rep(i,0,len/2-1) {if(str[i]!=str[len-i-1]) //判断其不是回文串ju1=false;}rep(i,0,len-1) {if(m[str[i]]==' ') //其镜像后无法得到一个有效字符,说明其不是镜像串{ju2=false;break;}else{ss1[len-i-1]=m[str[i]]; //左镜像串ss2[i]=m[str[len-i-1]]; //右镜像串}}ss1[len]='\0';ss2[len]='\0';if(ju2==true){if(strcmp(ss1,str)!=0||strcmp(ss2,str)!=0) //若左右镜像与原字符串不等,这其不是镜像串ju2=false;}if(ju1==false&&ju2==false) //根据结果输出printf("%s -- is not a palindrome.\n\n",str);else if(ju1==true&&ju2==false)printf("%s -- is a regular palindrome.\n\n",str);else if(ju1==false&&ju2==true)printf("%s -- is a mirrored string.\n\n",str);elseprintf("%s -- is a mirrored palindrome.\n\n",str);}return 0;
}