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. HDOJ 1028 Ignatius and the Princess III (母函数)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  2. 理解Session的几种模式

    一.写在前面 我们在使用ASP.NET开发的过程中,有时会进行数据存储以实现请求前后的状态保持(HTTP是无状态保持的协议),而Session作为一种快速简单易于实现的方式被我们经常使用,当然如果出于 ...

  3. 超快的 FastText

    Word2Vec 作者.脸书科学家 Mikolov 文本分类新作 fastText:方法简单,号称并不需要深度学习那样几小时或者几天的训练时间,在普通 CPU 上最快几十秒就可以训练模型,得到不错的结 ...

  4. B树、B-树、B+树、B*树---转载

    B树 即二叉搜索树: 1.所有非叶子结点至多拥有两个儿子(Left和Right): 2.所有结点存储一个关键字: 3.非叶子结点的左指针指向小于其关键字的子树,右指针指向大于其关键字的子树: 如: B ...

  5. HDU 1548 A strange lift (Dijkstra)

    A strange lift http://acm.hdu.edu.cn/showproblem.php?pid=1548 Problem Description There is a strange ...

  6. java基础知识回顾之抽象类

    /* 抽象类: 抽象:笼统,模糊,看不懂!不具体. 特点: 1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中.该类必须也被abstract修饰. ...

  7. C# 与C/C++相互调用

    C++调用C#的DLLhttp://www.csharpwin.com/csharpspace/11385r8940.shtml C#调用C/C++动态库必须注意的几个问题http://www.rob ...

  8. ElasticSearch使用IK中文分词---安装步骤记录

    提示1:必须保证之前的ES中不存在index, 否则ES集群无法启动, 会提示red! 提示2:下载的IK如果太新,会报错 TokenStream被重载Caused by: java.lang.Ver ...

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

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

  10. CodeForces485A——Factory(抽屉原理)

    Factory One industrial factory is reforming working plan. The director suggested to set a mythical d ...