401 Palindromes(回文词)
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.
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |
Note that O (zero) and 0 (the letter) are considered the same character and thereforeONLY 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.
题目大意:
回文词(palindrome)就是顺序逆序都相同的词句,例如“ABCDEDCBA”。
镜像词语,就是两个词成镜像相反,例如“A”、:"I"都是自己是自己的镜像,“3”和“E”互成镜像,题目中也列出了大写字母以及数字的镜像。
题目要求就是判断一个字符串是否是回文串、镜像串,根据判断结果有四种解答,分别是:
字符串 | 标准 |
" -- is not a palindrome." | 既不是回文串,也不是镜像串 |
" -- is a regular palindrome." | 是回文串,但不是镜像串 |
" -- is a mirrored string." | 是镜像串,但不是回文串 |
" -- is a mirrored palindrome." | 既是回文串,又是镜像串 |
首先用一个字符串数组将所有的镜像单词存储下来char mirror[]={"1SE Z 8 A 3 HIL JM O 2TUVWXY5"};(从1~Z,根据ASCII码,数字后留七个空格,才到大写字母),然后进行镜像的选择。
1)回文的时候,可能有人使用strrev函数,但它不属于ANSI C标准,所以会出现错误(Compilation error),所以可以自己写回文函数。
#include <stdio.h>
#include <stdlib.h>
#include <string.h> char str[1000], mir[1000];
char mirror[]={"1SE Z 8 A 3 HIL JM O 2TUVWXY5"}; int main(){
int len;
int i, flag1, flag2;
while(scanf("%s", str)!=EOF){ len = strlen(str);
for(i = 0; i<len; i++){ /*镜像选择*/
mir[len-i-1] = mirror[str[i]-'1'];
}
mir[len] = '\0'; /*最后要有结束的符号*/ flag1=flag2=0; /*标记的初始化,0代表是,非0代表不是*/
for(i = 0; i<=(len/2); i++){ /*判断是不是回文串*/
if(str[i]!=str[len-i-1]){
flag1 = 1;
break;
}
} flag2 = strcmp(mir, str); /*判断是不是镜像串*/ printf("%s -- ", str);
if(flag1!=0&&flag2!=0){
printf("is not a palindrome.");
}
else if(flag1==0&&flag2!=0){
printf("is a regular palindrome.");
}
else if(flag1!=0&&flag2==0){
printf("is a mirrored string.");
}
else if(flag1==0&&flag2==0){
printf("is a mirrored palindrome.");
}
printf("\n\n");
}
}
401 Palindromes(回文词)的更多相关文章
- hdu 1318 Palindromes(回文词)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1318 题意分析:输入每行包含一个字符串,判断此串是否为回文串或镜像串. 表面上看这道题有些复杂,如果能 ...
- UVa-401 Palindromes回文词
虽然是水题,但是容易错.参照了紫书的代码可以写的很简洁.主要还是注意常量数组的使用,能让代码变得简单许多 #include <iostream> #include <cstdio&g ...
- 回文词 (Palindromes,Uva401)
例题 3-3 回文词 (Palindromes,Uva401) 输入一个字符中,判断它是否为回文串以及镜像串.输入字符串保证不含数字0.所谓回文串,就是反转以后和原串相同,如abba和madam.所有 ...
- CSU 1328: 近似回文词
省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 ...
- 字符串 - 近似回文词 --- csu 1328
近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...
- csuoj 1328: 近似回文词
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 1328: 近似回文词 Time Limit: 1 Sec Memory Limit: 1 ...
- Vijos1327回文词【动态规划】
回文词 回文词是一种对称的字符串--也就是说,一个回文词,从左到右读和从右到左读得到的 结果是一样的.任意给定一个字符串,通过插入若干字符,都可以变成一个回文词.你的任务是写 一个程序,求出将给定字符 ...
- 回文词_KEY
回文词 (palin.pas/c/cpp) [问题描述] 回文词是一种对称的字符串--也就是说,一个回文词,从左到右读和从右到左读得的结果是一样的.任意给定一个字符串,通过插入若干字符,都可以变成一个 ...
- csu-1328 近似回文词 和 最长回文字符串
原博文地址:http://blog.csdn.net/u012773338/article/details/39857997 最长回文子串 描述:输入一个字符串,求出其中最长的回文子串.子串的含义是: ...
随机推荐
- ASP.NET MVC+Bootstrap个人博客之praise.js点赞特效插件(二)
1. 为啥要做这个点赞插件? praise.js是一款小巧的jQuery点赞插件,使用简便,效果美观. 在做个人博客时遇到了文章点赞问题.联想到各大社交网络中的点赞特效:手势放大.红心放大等等, ...
- HDU 5783 Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- java中的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- vim简单使用教程
vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一定会对这个编辑器失去兴趣的.下面的文章翻译自<Learn Vim Progress ...
- bjfu1235 两圆公共面积
给定两个圆,求其覆盖的面积,其实也就是求其公共面积(然后用两圆面积和减去此值即得最后结果). 我一开始是用计算几何的方法做的,结果始终不过.代码如下: /* * Author : ben */ #in ...
- PHP图像操作:3D图、缩放、旋转、裁剪、添加水印(三)
来源:http://www.ido321.com/887.html 5.PHP对图像的旋转 1: <div> 2: <h4>旋转之前</h4> 3: <img ...
- (转载)OC学习篇之---@class关键字的作用以及#include和#import的区别
前一篇文章说到了OC中类的三大特性,今天我们来看一下在学习OC的过程中遇到的一些问题,该如何去解决,首先来看一下我们之前遗留的一个问题: 一.#import和#include的区别 当我们在代码中使用 ...
- Tkinter教程之Canvas篇(1)
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811803 '''Tkinter教程之Canvas篇(1)'''# 提供可以用来进行绘图的Co ...
- php 正则表达
今天看书,看到代码里面出现了一段正则表达式匹配语句preg_match,感觉水很深的感觉,网上搜了一些资料,暂时没时间学习,但是觉得以后学的话有两个网址比较靠谱,如下: php正则表达式手册:php ...
- poj1001 Exponentiation
Description Problems involving the computation of exact values of very large magnitude and precision ...