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. C/C++中内存区域划分大总结

    C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...

  2. 云计算中iaas、paas、saas的区别和联系

    概念: iass : Infrastructure(基础设施)-as-a-Service, paas : Platform(平台)-as-a-Service, saas : Software(软件)- ...

  3. linux shell的输出效果修改方法(界面颜色)

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例: echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置背景成为蓝色,前景白色, ...

  4. Python中编码问题?

    一.键盘输入 raw_input('请输入:'.decode('utf-8').encode('gbk'))raw_input(unicode('请输入:','utf-8').encode('gbk' ...

  5. com组件的注册

    错误: 检索 COM 类工厂中 CLSID 为 {79AD7B73-C515-40B4-8B02-CB0F5FA5A1A8} 的组件失败,原因是出现以下错误: 80040154 没有注册类 (异常来自 ...

  6. (11)nehe教程5---3D空间

    3D空间: 我们使用多边形和四边形创建3D物体,在这一课里,我们把三角形变为立体的金子塔形状,把四边形变为立方体. 在上节课的内容上作些扩展,我们现在开始生成真正的3D对象,而不是象前两节课中那样3D ...

  7. linux下PostgreSQL数据库的源码安装

    实验环境>>>>>>>>>>>>>>>>>>操作系统:CentOS release 6.3 ...

  8. CSS3做小三角形

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXgAAAA2CAIAAABC2hVZAAAgAElEQVR4nKzcd3cbV57web+1p20FW8

  9. C# 任意类型数据转JSON格式

    /// <summary> /// List转成json /// </summary> /// <typeparam name="T">< ...

  10. PHP 反射机制Reflection

    简介 PHP Reflection API是PHP5才有的新功能,它是用来导出或提取出关于类.方法.属性.参数等的详细信息,包括注释. class Reflection { } interface R ...