【回文串+镜像串】HDU-1318 Palindromes

本文介绍了一种判断字符串是否为回文串和镜像串的方法,通过C++实现,详细解释了代码逻辑,包括如何使用映射关系判断镜像串,以及如何判断回文串。

在这里插入图片描述
在这里插入图片描述

注解

1、回文串的定义。
2、镜像串的定义。此题给了映射关系,也就相当于是个map了。

代码

#include <iostream>

using namespace std;

int isPar(string s){
    int len = s.length();
    for(int i=0; i<len; i++){
        if(s.at(i)!=s.at(len-i-1)){
            return 0;
        }
    }
    return 1;
}

int isMirror(string s){
    string s1 = "A   3  HIL JM O   2TUVWXY5";
    string s2 = "1SE Z  8 ";
    
    int len = s.length();
    string smir = "";
    for(int i=0; i<len; i++){
        if(s.at(i)>='A' && s.at(i)<='Z'){
            smir += s1.at(s.at(i)-'A');
        }
        else{
            smir += s2.at(s.at(i)-'1'); 
        }
    }
    
    for(int i=0; i<len; i++){
        if(s.at(i)!=smir.at(len-i-1)){
            return 0;
        }
    }
    return 1;
}

int main(){
    string s;
    while(cin>>s){
        if(isPar(s) && isMirror(s)){
            cout<<s<<" -- "<<"is a mirrored palindrome."<<endl<<endl;
        }
        else if(isPar(s)){
            cout<<s<<" -- "<<"is a regular palindrome."<<endl<<endl;
        }
        else if(isMirror(s)){
            cout<<s<<" -- "<<"is a mirrored string."<<endl<<endl;
        }
        else{
            cout<<s<<" -- "<<"is not a palindrome."<<endl<<endl;
        }
    }
    return 0;
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值