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 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.

输入样例:

NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA

输出样例:

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.
大意就是判断一个输入的字符串是否为回文和镜像的,因此我写了两个函数。而对于题中表格的处理,本来开始打算用数组的,后来觉得不方便,改用switch语句了。
题目本来不难,注意特殊情况的处理,就是当字符串长度为1的时候。当n==1时,是回文的,当且仅当改字符和本身是镜像的时候,这个字符串是镜像的。
对了,还有很重要的一点就是每次输出后都要再输出一个空行,对于我们这种英语渣渣是怎么会注意到这种东西的。。
 #include <iostream>
#include <cstring>
using namespace std; const char* res[] = {" -- is not a palindrome.", " -- is a regular palindrome.",
" -- is a mirrored string.", " -- is a mirrored palindrome."}; int main(void)
{
char Reverse(char c);
bool Palindrome(char s[], int n);
bool Mirrored(char s[], int n);
char s[];
int len;
while(cin >> s)
{
len = strlen(s);
bool flag1 = Palindrome(s, len);
bool flag2 = Mirrored(s, len);
cout << s;
if(!flag1 && !flag2)
cout << res[] << endl << endl;
if(flag1 && !flag2)
cout << res[] << endl << endl;
if(!flag1 && flag2)
cout << res[] << endl << endl;
if(flag1 && flag2)
cout << res[] << endl << endl;
}
return ;
}
char Reverse(char c)
{
switch (c)
{
case 'A':
case 'H':
case 'I':
case 'M':
case 'O':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case '':
case '':
return c;
case 'E':
return '';
case 'J':
return 'L';
case 'L':
return 'J';
case 'S':
return '';
case 'Z':
return '';
case '':
return 'S';
case '':
return 'E';
case '':
return 'Z';
default:
return ;
}
}
//判断是否是回文字符串
bool Palindrome(char s[], int n)
{
int i;
if(n == )
return true;
for(i = ; i < n / ; ++i)
{
if(s[i] != s[n - - i])
return false;
}
return true;
}
//判断是否是镜像字符串
bool Mirrored(char s[], int n)
{
int i;
if(n == )
{
if(Reverse(s[]) == s[])
return true;
else
return false;
}
for(i = ; i < n / ; ++i)
{
if(Reverse(s[i]) == )
return false;
if(Reverse(s[i]) != s[n - - i])
return false;
}
return true;
}

代码君

UVa401 回文词的更多相关文章

  1. 回文词 (Palindromes,Uva401)

    例题 3-3 回文词 (Palindromes,Uva401) 输入一个字符中,判断它是否为回文串以及镜像串.输入字符串保证不含数字0.所谓回文串,就是反转以后和原串相同,如abba和madam.所有 ...

  2. CSU 1328: 近似回文词

    省赛的A题...现场都没什么人做...其实就一暴力水题......坑死了... 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1 ...

  3. 字符串 - 近似回文词 --- csu 1328

    近似回文词 Problem's Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 analyse: 直接暴力枚举每一个终点,然后枚举 ...

  4. csuoj 1328: 近似回文词

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 1 ...

  5. 401 Palindromes(回文词)

      Palindromes  A regular palindrome is a string of numbers or letters that is the same forward as ba ...

  6. Vijos1327回文词【动态规划】

    回文词 回文词是一种对称的字符串--也就是说,一个回文词,从左到右读和从右到左读得到的 结果是一样的.任意给定一个字符串,通过插入若干字符,都可以变成一个回文词.你的任务是写 一个程序,求出将给定字符 ...

  7. 回文词_KEY

    回文词 (palin.pas/c/cpp) [问题描述] 回文词是一种对称的字符串--也就是说,一个回文词,从左到右读和从右到左读得的结果是一样的.任意给定一个字符串,通过插入若干字符,都可以变成一个 ...

  8. csu-1328 近似回文词 和 最长回文字符串

    原博文地址:http://blog.csdn.net/u012773338/article/details/39857997 最长回文子串 描述:输入一个字符串,求出其中最长的回文子串.子串的含义是: ...

  9. CSU 1328 近似回文词【最长回文字符串(三种方法)】

    输入一行文本,输出最长近似回文词连续子串.所谓近似回文词是指满足以下条件的字符串: 1. S以字母开头,字母结尾 2. a(S)和b(S)最多有2k个位置不同,其中a(S)是S删除所有非字母字符并且把 ...

随机推荐

  1. 使用CSS3实现超炫的Loading(加载)动画效果

    SpinKit 是一套网页动画效果,包含8种基于 CSS3 实现的很炫的加载动画.借助 CSS3 Animation 的强大功能来创建平滑,易于定制的动画.SpinKit 的目标不是提供一个每个浏览器 ...

  2. JDK中常见的package

    SUN公司在JDK中为程序开发者提供了各种实用类,这些类按功能不同分别被放入了不同的包中,供开发者使用,下面简要介绍其中最常用的几个包:1. java.lang — 包含一些Java语言的核心类,如S ...

  3. (转)c语言随机数srandom( )

    转自:http://zhidao.baidu.com/question/334364810.html调用随机数函数 rand()() 的时候, 实际得到的这个随机数并不是绝对随机的,它是以一个初始值, ...

  4. JS获取Url参数的通用方法

    //获取URL中的参数 function request(paras) { var url = location.href.replace('#', ''); var paraString = url ...

  5. 数据库(.udl)简单测试连接

    当我们烦于打开数据库进行连接的时候,我们可以用udl进行测试连接,并可以获得连接字符串. 1.新建一个txt文件,然后将后缀改成udl保存. 2.双击打开udl文件. 3.进行数据库连接测试. 4.用 ...

  6. [你必须知道的.NET]第三十一回,深入.NET 4.0之,从“新”展望

    发布日期:2009.05.22 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. /// <summary> /// 本文开始,将以& ...

  7. vim中如何引用自定义模板文件

    我们在使用vim新建文件时可以引用自定义模板,来避免重复的数据格式处理花费太多时间. 实现方法很简单,只需要2步即可:1. 在.vim/template目录放入自己的模板文件(如shellconfig ...

  8. Textures

    LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer=NULL; //顶点缓存 LPDIRECT3DTEXTURE9 g_Texture=NULL;//纹理对象 bool In ...

  9. 浅析ODS与EDW关系(转载)

    浅析ODS与EDW 关系 刘智琼 (中国电信集团广州研究院广州510630) 摘要 本文重点介绍了企业运营数据仓储(ODS)和企业数据仓库(EDW )的概念,并对ODS与EDW 之间的关系,包括两者相 ...

  10. Java-J2SE学习笔记-树状展现文件结构

    1.利用java.io相关类树状展现文件结构 2.判定给定路径是否为dir,是则递归,每一递归一层缩进一次 3.代码 package Test; import java.io.File; public ...