UVA401 回文词 Palindromes
程序员文章站
2022-03-04 22:54:04
...
题目描述
输入格式
输出格式
题意翻译
输入是一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字00 。所谓回文串,就是反转以后和原串相同,如abbaabba 和madammadam 。所谓镜像串,就是左右镜像之后和原串相同,如2S2S 和3AIAE3AIAE 。注意,并不是每个字符在镜像之后都能得到一个合法字符。在本题中,每个合法字符的镜像如下表所示:
Character Reverse
A A
E 3
H H
I I
J L
L J
M M
O O
S 2
T T
U U
V V
W W
X X
Y Y
Z 5
1 1
2 S
5 Z
8 8
输入输出样例
输入 #1
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
输出 #1
NOTAPALINDROME -- is not a palindrome.
ISAPALINILAPASI -- is a regular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<cstdio>
using namespace std;
#pragma warning(disable:4996)
int n, l, i, a[10000];
const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg[] = {"not a palindrome","a regular palindrome","a mirrored string","a mirrored palindrome"};
char r(char ch) {
if (isalpha(ch))return rev[ch-'A'];//是字母,返回反转的字母/数字
return rev[ch-'0'+25];//如果是数字,返回字母(字母26+数字9)
}
int main()
{
char s[30];
while (scanf("%s",s)==1)
{
int len = strlen(s);//计算长度,二分,左边右边相比较
int p = 1,m = 1;//标志
for (int i=0;i<(len+1)/2;i++)//左右相比
{
if (s[i] != s[len - 1 - i])p = 0;//不是回文串
if (r(s[i]) != s[len - 1 - i])m = 0;//不是镜像串
}
printf("%s -- is %s.\n\n",s,msg[m*2+p]);//按格式打印
}
return 0;
}