UVa401 - Palindromes

题目链接:点击打开链接


解题思路:理解题意,主要是对回文与镜像回文的判断。两者可以同时进行。

对回文来说,首尾字符要相同;对镜像回文来说,第一个字符需要是最后一个字符的镜像,第二个字符需要是倒数第二个字符的镜像,……。注意,在对回文进行判断时,如果是奇数个字符,那么中间一个的无需判断,但是对于镜像回文来说,中间一个字符必然有镜像,且它的镜像必然是它自己。

输入字符串不含空格 Tab等,所以可以直接用scanf输入。


代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctype.h>
using namespace std;

const int N = 10000000;
const char a[100] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
const char *msg[4] = {"is not a palindrome.", "is a mirrored string.", "is a regular palindrome.", "is a mirrored palindrome."};
char s[N];

char rev(char ch)
{
   if(isalpha(ch))
      return a[ch-'A'];
   else
      return a[ch-'0'+25];
}

int main()
{
    while(scanf("%s", s) != EOF)
    {
        int a, b;
        a = b = 1;
        int len = strlen(s);
        for(int i = 0; i < (len+1)/2; i++)
        {
            if(s[i] != s[len-1-i])
                a = 0;
            if(rev(s[i]) != s[len-1-i])
                b = 0;
        }

         printf("%s -- %s", s, msg[a*2+b]);

         printf("\n\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值