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 最长回文子串 描述:输入一个字符串,求出其中最长的回文子串.子串的含义是: ...
 
随机推荐
- hdu 2845(dp基础题)
			
题意:容易理解. 分析:以后碰到这种类型的题,就要考虑把矩阵先按行来处理,再按列处理.先算出每行能够能够得到的最大值,然后按列处理即可. 代码实现: #include<stdio.h> # ...
 - GIT  分支的理解
			
乎所有的版本控制系统都以某种形式支持分支. 使用分支意味着你可以把你的工作从开发主线上分离开来,以免影响开发主线. 在很多版本控制系统中,这是一个略微低效的过程——常常需要完全创建一个源代码目录的副本 ...
 - UILabel 自适应大小
			
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStylealloc]init]; paragraphStyle.lineB ...
 - Mahout应用(一)
			
Mahout应用(一) Mahout 是应用于hadoop上的数据挖掘工具(废话不多说) 这里先简单介绍一下mahout的一般使用方法. 拿kmeans为列子 Mahout中的kmeans所需要的输入 ...
 - 【Hadoop学习】Super用户以其他用户的名义执行操作
			
Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ...
 - Junit3.8 私有方法测试
			
1. 测试类的私有方法时可以采取两种方式:1) 修改方法的访问修饰符,将private修改为default或public(但不推荐采取这种方式).2) 使用反射在测试类中调用目标类的私有方法(推荐). ...
 - POJ2763-Housewife Wind(树链剖分)
			
也是入门题,和上一题不一样的是权值在边上. 调了半天后来发现线段树写错了,build的时候没有pushup...蠢哭了好吗.... 做题还是不专心,太慢辣.. #include <algorit ...
 - build.gradle 使用tips
			
7.查看依赖 gradlew [你想查看的module]:dependencies >dependencies.txt 6.buildToolsVersion build tools版本号 co ...
 - Java设计模式系列之观察者模式
			
观察者模式 Observer的定义 观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象. 这个主题对象在状态上发生变化时,会通知所有观察者对象,让它们能够自动更新自己. 第一 ...
 - labview多个并行循环同时退出
			
labview中停止并行的循环 问题: 在labview中我如何停止两个并行的循环?我使用一个局部变量,但是当我停止程序执行后,第二次不能运行程序.我该如何解决这个问题呢? 解答: 你使用局部变量来 ...