Palindromes(回文、镜像字符串)

描述

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.(回文字符串)

A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I"are their own reverses, and "3" and "E" are each others' reverses.(镜像字符串)

A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.

Of course,"A","T", "O", and "Y" are all their own reverses.

A list of all valid characters and their reverses is as follows.

Note that 0 (zero) and O (the letter) are NOT considered the same character and therefore ONLY the letter "O" is a valid character.

输入

Input consists of several strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings, except some speical cases. Your program should read to the end of file.

输出

For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.

           STRING                                             CRITERIA
" -- is not a palindrome."         if the string is not a palindrome and is not a mirrored string
" -- is a regular palindrome."     if the string is a palindrome and is not a mirrored string
" -- is a mirrored string."        if the string is not a palindrome and is a mirrored string
" -- is a mirrored palindrome."    if the string is a palindrome and is a mirrored string

Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.

In addition, after each output line, you must print an empty line.

样例输入 

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA

样例输出 

NOTAPALINDROME -- is not a palindrome.ISAPALINILAPASI -- is a regular palindrome.2A3MEAS -- is a mirrored string.ATOYOTA -- is a mirrored palindrome.

思路

此题要注意在处理镜像字符串,我们在遍历每个字符时,记为c,若c没有对应的反转字符,则此字符忽略,在我的代码中体现在不加入到temp中

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<algorithm>
#include <unordered_map>
using namespace std;
unordered_map<char, char> orignial_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'},{'3','E'},{'5','Z'},\
{'8','8'} };
bool reverse_string(string input) {string temp=input;reverse(input.begin(), input.end());if (temp == input) return true;else return false;
}
bool mirror_string(string input) {string temp;for (char c:input) {auto it = orignial_reverse.find(c);if (it != orignial_reverse.end()) temp.push_back(it->second);}reverse(temp.begin(), temp.end());if (temp == input) return true;else return false;
}
int main()
{string input;while (getline(cin, input)) {bool flag1 = reverse_string(input);bool flag2 = mirror_string(input);if (!flag1 && !flag2) {cout << input << " -- is not a palindrome.\n" << endl;}else if (flag1 && !flag2) cout << input << " -- is a regular palindrome.\n" << endl;else if (!flag1 && flag2) cout << input << " -- is a mirrored string.\n" << endl;else if (flag1 && flag2) cout << input << " -- is a mirrored palindrome.\n" << endl;}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/774542.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux命令在线查询工具

您提供的链接是一个名为“Linux 命令在线查询工具 - 轻松查找命令信息”的在线工具页面。这个工具旨在帮助用户快速查找和了解Linux命令的详细信息&#xff0c;从而提高工作效率。 工具概述 Linux命令在线查询工具是一个便捷的资源&#xff0c;它允许用户通过简单的搜索功能来…

dfs (蓝桥备赛)

1、 1317&#xff1a;【例5.2】组合的输出 时间限制: 1000 ms 内存限制: 65536 KB 提交数:52237 通过数: 26231 【题目描述】 排列与组合是常用的数学方法&#xff0c;其中组合就是从n个元素中抽出r个元素(不分顺序且r≤n)&#xff0c;我们可以简单地将n个元素理解…

SpringMvc之映射器HandlerMapping

简介 在springmvc的处理流程中&#xff0c;第一步就是查询请求对应的映射器&#xff0c;然后组装成处理器链处理请求&#xff0c;本文意在梳理该过程 重要实现 HandlerMapping是一个接口&#xff0c;该接口用于通过HttpServletRequest寻找对应的处理器&#xff0c;接口介绍如下…

攻防世界逆向刷题

阅读须知&#xff1a; 探索者安全团队技术文章仅供参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作,由于传播、利用本公众号所提供的技术和信息而造成的任何直接或者间接的后果及损失&#xff0c;均由使用者 本人负责&#xff0c;作者不为此承担任何责任,如…

数据库 表数据添加分页查询 --Java实现

分页数据实体类 public class PageBean implements Serializable {private int rowsPerPage 15;private int rowsNum;//行数private int maxPage;//页数private int pageNum;//页码 } Emp实体类 Data public class Emp implements Serializable {private Long id;private S…

大三实习小菜蛋之JS元素节点

元素节点对象&#xff08;element&#xff09; -在网页中&#xff0c;每一个标签就是一个节点元素 如何获取元素节点对象 1.通过document对象来获取元素节点 2.通过document对象来创建元素节点 通过document来获取已有的元素节点 document.getElementById()-根据id获取一个…

使用`scipy.stats.wasserstein_distance`来计算两个一维分布之间的Earth Mover‘s Distance (EMD)距离

在Python中&#xff0c;计算Earth Mover’s Distance (EMD)通常使用scipy库中的scipy.stats.wasserstein_distance函数&#xff0c;该函数计算的是Wasserstein距离&#xff0c;它与EMD非常相似&#xff0c;都是用来衡量两个分布之间的距离。 以下是一个简单的Python程序例子&a…

超好用的快捷回复软件

随着直播经济和短视频平台的兴起&#xff0c;品牌营销阵地不再局限于传统的电商巨头——淘宝、天猫、京东和拼多多&#xff0c;越来越多的品牌正积极布局快手、抖音等新晋电商平台&#xff0c;同步打造社群矩阵以拓宽产品推广渠道。这种多维度的市场渗透策略有力地提升了品牌的…

C语言看完我这篇编译与链接就够啦!!!

1. 前言 Hello&#xff01;大家好我是小陈&#xff0c;今天来给大家介绍最详细的C语言编译与链接。 2. 编译和链接 我们通常用的编译器&#xff0c;比如Visual Sudio,这样的IDE(集成开发环境&#xff09;一般将编译和链接的过程一步完成&#xff0c;通常将这这种编译和链接合…

算法(6)KMP+trie

KMP&#xff1a; 最浅显易懂的 KMP 算法讲解_哔哩哔哩_bilibili 该视频使用python书写代码&#xff0c;不会python的小伙伴也可以看看了解kmp的大致思路。 问题描述&#xff1a; kmp&#xff1a;字符串匹配算法&#xff0c;用来找一个长字符串中出现了几次小字符串&#xf…

random模块篇

Python 的 random 模块是一个非常实用的工具&#xff0c;它提供了生成各种类型随机数的方法。无论是生成随机整数、浮点数&#xff0c;还是从序列中随机选择元素&#xff0c;random 模块都能满足你的需求。下面是一个详细的教程&#xff0c;介绍 random 模块的主要功能和用法。…

【生成对抗网络GAN】一篇文章讲透~

目录 引言 一、生成对抗网络的基本原理 1 初始化生成器和判别器 2 训练判别器 3 训练生成器 4 交替训练 5 评估和调整 二、生成对抗网络的应用领域 1 图像生成与编辑 2 语音合成与音频处理 3 文本生成与对话系统

【机器学习300问】54、如何找到有效的组合特征?

一、为什么需要去寻找有效的组合特征&#xff1f; 因为并不是所有的特征组合都会意义&#xff0c;都能带来价值。 例如在房价预测场景中&#xff0c;卧室数量和浴室数量的比值有意义&#xff0c;但房屋面积与建造年份相组合作为新的组合特征&#xff0c;可能就没有实际含义&…

【vivado】在原有工程上新建工程

一、前言 在工作中&#xff0c;我们经常需要接触到别人的工程&#xff0c;并在别人的工程上新加设计功能&#xff0c;此时我们需要以别人工程为基础新建工程。 二、在已有工程上新建工程的方法 2.1 vivado 页面file-project-save as... 该方法的优点为&#xff1a;可以直接…

【ORB-SLAM3】在 Ubuntu20.04 上编译 ORM-SLAM3 并使用 D435i、EuRoC 和 TUM-VI 运行测试

【ORB-SLAM3】在 Ubuntu20.04 上编译 ORM-SLAM3 并使用 D435i、EuRoC 和 TUM-VI 运行测试 1 Prerequisites1.1 C11 or C0x Compiler1.2 Pangolin1.3 OpenCV1.4 Eigen3 2 安装 Intel RealSense™ SDK 2.02.1 测试设备2.2 编译源码安装 (Recommend)2.3 预编译包安装 3 编译 ORB-S…

sql常用之CASE WHEN THEN

sql常用之CASE WHEN THEN SQL中的 CASE 类似编程语言里的 if-then-else 语句&#xff0c;用做逻辑判断。可以用于SELECT语句中&#xff0c;也可以用在WHERE&#xff0c;GROUP BY 和 ORDER BY 子句&#xff1b;可以单独使用&#xff0c;也可以和聚合函数结合使用。 语法&#…

PTA L2-037 包装机

一种自动包装机的结构如图 1 所示。首先机器中有 N 条轨道&#xff0c;放置了一些物品。轨道下面有一个筐。当某条轨道的按钮被按下时&#xff0c;活塞向左推动&#xff0c;将轨道尽头的一件物品推落筐中。当 0 号按钮被按下时&#xff0c;机械手将抓取筐顶部的一件物品&#x…

SSM 整合

文章目录 SSM 整合&#xff08;代码配置&#xff09;1. 基本形式2. 无 web.xml 的理论基础3. WebInitializer 替代 web.xml4. SpringWebConfig 替代 spring-web.xml5. 配置『静态资源不拦截』方案一方案二 6. 配置 URL 后缀生效/失效7. 整合 Service 层8. 整合 Dao 层9. Mybati…

07、Lua 流程控制

Lua 流程控制 Lua 流程控制控制结构语句 Lua 流程控制 Lua编程语言流程控制语句通过程序设定一个或多个条件语句来设定。在条件为 true 时执行指定程序代码&#xff0c;在条件为 false 时执行其他指定代码。 以下是典型的流程控制流程图&#xff1a; 控制结构的条件表达式结…

python面试题(1~10)

1、列表&#xff08;list&#xff09;和元组&#xff08;tuple&#xff09;有什么区别&#xff1f; ①列表是不可变的&#xff0c;创建后可以对其进行修改。元组是不可变的&#xff0c;元组一旦创建&#xff0c;就不能对其进行修改。 ②列表表示的顺序&#xff0c;它们是有序…