UVA - 401 Palindromes (回文串)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19520

题意:传统的回文串是一个由字母或数字组成的正读和反读一样的字符串。而映照字符串是当这个元素被它所映照的元素替换之后,从后往前读跟原来字符串一样的一种字符串。

判断给定的字符串是什么类型的字符串。

注意:有些字符的映照字符串是自身,有些没有,有些事其他的元素,所以没有映照的字符就是非法的,输入也不会出现。

思路:直接用两个函数判断是否是回文或者映照即可,回文简单,第二种我们可以用两个常量数组把变的值保存下来,这样方便很多。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>

#define CL(arr, val)    memset(arr, val, sizeof(arr))

#define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)

#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define lowbit(x)   (x)&(-x)
#define Read()  freopen("a.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);
#define N 100005
using namespace std;

char *s1="AEHIJLMOSTUVWXYZ12358"; 
char *s2="A3HILJMO2TUVWXY51SEZ8";

bool palindrome(char *str)  //判断回文
{
    int i,len=strlen(str);
    for(i=0;i<len/2;i++)
    {
        if(str[i]!=str[len-i-1]) return 0;
    }
    return 1;
}

bool mirrored(char *str)  //判断是否是映照字符串
{
    int i,j,len=strlen(str);
    if(len==1)   //长度为1 的字符串特殊考虑
    {
        for(i=0;i<21;i++)
            if(str[0]==s1[i]) break;
        if(i==21||s2[i]!=str[0]) return 0;
    }
    else if(len>1)
    {
        for(i=0;i<len/2;i++)
        {
            for(j=0;j<21;j++)
            {
                if(str[i]==s1[j]) break;
            }
            //printf("%c %c\n",s2[j],str[len-i-1]);
            if(j==21||s2[j]!=str[len-i-1]) return 0;
        }
    }
    return 1;
}

int main()
{
   //Read();
    char s[25];
    while(~scanf("%s",s))
    {
        if(palindrome(s))
        {
            if(mirrored(s)) printf("%s -- is a mirrored palindrome.\n",s);
            else printf("%s -- is a regular palindrome.\n",s);
        }
        else
        {
            if(mirrored(s)) printf("%s -- is a mirrored string.\n",s);
            else printf("%s -- is not a palindrome.\n",s);
        }
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值