UVA 401
来源:互联网 发布:下载百度壁纸软件 编辑:程序博客网 时间:2024/05/17 05:19
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342
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.
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.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.
源代码:
#include <iostream>#include <cstdio>#include <string>using namespace std;bool hasRev[1000];char Rev[1000];int main(){char ch[25];string s;int len;for (int i = 'A'; i <= 'Z'; ++i) {if (i == 'B' || i == 'C' || i == 'D' || i == 'F'|| i == 'G' || i == 'K' || i == 'N'|| i == 'P' || i == 'Q' || i == 'R')hasRev[i] = false;elsehasRev[i] = true;}for (int i = '1'; i <= '9'; ++i) {if (i == '4' || i == '6' || i == '7' || i == '9')hasRev[i] = false;elsehasRev[i] = true;}for (int i = 0; i <= 1000; ++i)Rev[i] = char(i);Rev[int('E')] = '3';Rev[int('J')] = 'L';Rev[int('L')] = 'J';Rev[int('Z')] = '5';Rev[int('2')] = 'S';Rev[int('3')] = 'E';Rev[int('5')] = 'Z';Rev[int('S')] = '2';while (scanf("%s", ch) != EOF) {s = string(ch);bool pali = true, mirror = true; len = s.length();for (int i = 0; i < len/ 2; ++i) if (s[i] != s[len - i - 1]) {pali = false;break;}if (len == 1)if (!hasRev[int(s[0])])mirror = false;for (int i = 0; i < len/ 2; ++i) if (!hasRev[int(s[i])] || !hasRev[int(s[len - i - 1])] || Rev[int(s[i])] != s[len - i - 1]) {mirror = false;break;}if (pali && mirror)printf("%s -- is a mirrored palindrome.\n", ch);else if (pali)printf("%s -- is a regular palindrome.\n", ch);else if (mirror)printf("%s -- is a mirrored string.\n", ch);elseprintf("%s -- is not a palindrome.\n", ch);printf("\n");}}
注意判断mirror字符串时,对单个字符的特殊判断,否则会被忽视处理!
- uva 401
- uva 401
- uva 401
- uva-401
- uva 401
- uva 401
- UVA 401
- UVa-401
- UVA 401
- UVA 401
- UVA 401
- UVA 401
- uva 401
- uva 401
- UVA-401
- UVA 401
- UVa 401
- UVA 401
- 有意思的面试题:井盖为什么是圆的?
- unix环境编程 阻塞IO和非阻塞IO
- CentOS 6.4 无线上网设置
- Median of Two Sorted Arrays
- 用C4D和ePmv/pdb服务器导出FBX文件的正确方法
- UVA 401
- hdu 1262 寻找素数对
- configure: error: C++ Compiler required to compile Anjuta
- 红黑树(rb_tree) 源代码
- COC Like 游戏中的寻路算法
- -贪多嚼不烂
- 图的存储结构(1):数组表示法
- Eclipse安装Tomcat插件失败问题
- Oracle Database 11g Release 2 RAC On Oracle Linux 5.8 Using VirtualBox