uva 401.Palindromes

本文提供了一段使用C++编写的代码,用于解决UVA在线评测平台上的问题,即判断给定字符串是否为回文串、镜像串或回文镜像串。代码通过字符映射表和循环比较实现判断,并在循环外进行最终判断结果的输出。

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342

题目意思:给出一段字符串(大写字母+数字组成)。判断是否为回文串 or 镜像串 or 回文镜像串 or 什么都不是。每个字母的镜像表格如下

 

CharacterReverseCharacterReverseCharacterReverse
AAMMYY
B N Z5
C OO11
D P 2S
E3Q 3E
F R 4 
G S25Z
HHTT6 
IIUU7 
JLVV88
K WW9 
LJXX  

 

注意是没有数字0的哦。(该题,数字 0 与字母 O 看成是一样的)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1000 + 5;
 8 char mirror[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
 9 const char *msg[4] = {" -- is not a palindrome.", " -- is a regular palindrome.", " -- is a mirrored string.", " -- is a mirrored palindrome."};
10 char s[maxn];
11 
12 char change(char ch)
13 {
14     if (ch >= 'A' && ch <= 'Z')
15        return mirror[ch-'A'];
16     return mirror[ch-'0'+25];
17 }
18 
19 int main()
20 {
21     #ifndef ONLINE_JUDGE
22         freopen("in.txt", "r", stdin);
23     #endif // ONLINE_JUDGE
24 
25     while (scanf("%s", s) != EOF) {
26         int len = strlen(s);
27         int p = 1, m = 1;
28 
29         for (int i = 0; i < len; i++) {
30             if (s[i] != s[len-i-1]) p = 0;
31             if (change(s[i]) != s[len-i-1]) m = 0;
32         }
33         printf("%s%s\n\n", s, msg[p+m*2]);
34     }
35     return 0;
36 }

 

转载于:https://www.cnblogs.com/windysai/p/5348481.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值