博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【UVA】 401 --- Palindromes
阅读量:2039 次
发布时间:2019-04-28

本文共 3569 字,大约阅读时间需要 11 分钟。

【UVA】 401 --- 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 O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter “0” is a valid character.

Input

Input consists of 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. Your program should read to the end of file.

Output

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.

Sample Input

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA

Sample Output

NOTAPALINDROME – is not a palindrome.

ISAPALINILAPASI – is a regular palindrome.

2A3MEAS – is a mirrored string.

ATOYOTA – is a mirrored palindrome.

思路:

用m,n来标记输入语句是否是回文和是否是镜像字符串。
假设输入语句是镜像回文,即:m=1,n=1;
通过遍历判断第i个字符和第x-i-1个字符是否相同,
如果出现不同,令m=0;标记为不是回文。
如果出现第i个字符的镜像不等于第x-i-1个字符,则令n=0;标记为不是镜像字符串。
最后只需判断m,n的情况,依次输出对应语句。

#include 
#include
#include
using namespace std;const char arr[] = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";int main(){
char str[21]; while (scanf("%s", str) == 1) {
int x, m = 1, n = 1; x = strlen(str); for (int i = 0; i < x; i++) {
if (str[i] != str[x - i - 1]) // 判断第i个字符是否和x-i-1位置的字符相同 {
m = 0; } if (str[i] >= 'A'&&str[i] <= 'Z') {
if (str[x - i - 1] != arr[str[i] - 'A']) // 判断第i个字符的镜像是否和x-i-1位置的字符相同 {
n = 0; } } if (str[i] >= '0'&&str[i] <= '9') {
if (str[x - i - 1] != arr[str[i] - '0' + 25]) // 判断第i个字符的镜像是否和x-i-1位置的字符相同 {
n = 0; } } } cout << str; if (m == 0 && n == 0) cout << " -- is not a palindrome." << endl; if (m == 1 && n == 0) cout << " -- is a regular palindrome." << endl; if (m == 0 && n == 1) cout << " -- is a mirrored string." << endl; if (m == 1 && n == 1) cout << " -- is a mirrored palindrome." << endl; cout << endl; } return 0;}

转载地址:http://ogyof.baihongyu.com/

你可能感兴趣的文章
解决activemq多消费者并发处理
查看>>
网络通信分享(一):数字签名,数字证书,https通信,数据加密
查看>>
关于Class.getResource和ClassLoader.getResource的路径问题
查看>>
Spring MVC 基于session 国际化配置!! 亲测可用
查看>>
Netty维持长连接 消息推送及心跳机制
查看>>
如果有人问你数据库的原理,叫他看这篇文章
查看>>
Java 应用一般架构
查看>>
java中的本地缓存
查看>>
一些不错技术博客列表 长期更新~~
查看>>
UDP连接和TCP连接的异同
查看>>
hibernate 时间段查询
查看>>
《深入浅出 Java Concurrency》目录
查看>>
java操作cookie 实现两周内自动登录
查看>>
在Cookie被禁用的情况下使用url rewrite机制保持Session
查看>>
Java根据sessionId获取Session对象
查看>>
rabbitmq rpc java
查看>>
怎么在eclipse中安装properties插件(国际化使用)
查看>>
jstl 中获得session 里面值sessionScope
查看>>
jstl和字符串比较是否相等
查看>>
SpringMVC基于session存储注解的值 (全局使用)
查看>>