UVA 401 Palindromes(回文词)



回文词

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld
& %llu

Submit Status Practice UVA
401

Appoint description: 
acmparand  (2013-07-07)Luke  (2014-01-20)
System Crawler  (2015-05-10)

Description

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.

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.

Hint

use the C++‘s class of string will be convenient, but not a must

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	string s;
	while(cin >> s) {
		int len = s.size();
		for(int i = 0; i < len; i++) {
			if(s[i] == '0') s[i] = 'O';
		}
		int sin_dou = len%2; //表示字符串的奇偶
		int half_len = len/2;
		int i, node = 0;
		for(i = 0; i < half_len; i++) {//判断是否是回文串
			if(s[i] == s[len-i-1]) continue;
			else break;
		}
		if(i == half_len) {//如果是回文串的话
			for(i = 0; i < half_len; i++) {//用node来统计镜像字符
				if(s[i] == 'A' || s[i] == 'H' || s[i] == 'I' || s[i] == 'M' || s[i] == 'O' || s[i] == 'T' || s[i] == 'U' || s[i] == 'V' || s[i] == 'W' || s[i] == 'X' || s[i] == 'Y' || s[i] == '1' || s[i] == '8') node++;
			}
			int f = 0;
			//如果是偶数
			if(sin_dou == 0) f = 1;
			else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' ||  s[half_len] == '1' || s[half_len] == '8') f = 1;
			else f = 0;
			//如果node == half_len && f == 1说明,不仅是回文串,而且是镜像回文串,也就是回文串中的每个字母都有对应的镜像字母
			if(node == half_len && f == 1) cout << s << " -- is a mirrored palindrome." << endl << endl;
			//否则,就是一个普通的回文串
			else cout << s << " -- is a regular palindrome." << endl << endl;
		}
		else//如果不是回文串的话
		{
			for(i = 0; i < half_len; i++) {//判断是否是镜像字符串
				if(s[i] == s[len-i-1]) { if(s[i] == 'B' || s[i] == 'C' || s[i] == 'D' || s[i] == 'F' || s[i] == 'G' || s[i] == 'K' || s[i] == 'N' || s[i] == 'P' || s[i] == 'Q' || s[i] == 'R' || s[i] == '4' || s[i] == '6' || s[i] =='7' || s[i] == '9') break; else continue; }
				else if((s[i] == 'E' && s[len-i-1] == '3') || (s[i] == '3' && s[len-i-1] == 'E') || (s[i] == '2' && s[len-i-1] == 'S') || (s[i] == 'S' && s[len-i-1] == '2') || (s[i] == 'Z' && s[len-i-1] == '5') || (s[i] == '5' && s[len-i-1] == 'Z') || (s[i] == 'J' && s[len-i-1] == 'L') || s[i] == 'L' && s[len-i-1] == 'J') continue;
				else break;
			}
			int f = 0;
			if(sin_dou == 0) f = 1;
			else if(s[half_len] == 'A' || s[half_len] == 'H' || s[half_len] == 'I' || s[half_len] == 'M' || s[half_len] == 'O' || s[half_len] == 'T' || s[half_len] == 'U' ||s[half_len] == 'V' || s[half_len] == 'W' || s[half_len] == 'X' || s[half_len] == 'Y' ||  s[half_len] == '1' || s[half_len] == '8') f = 1;
			else f = 0;
			//如果i == half_len 且 f == 1的话,说明是一个镜像字符串
			if(i == half_len && f == 1) {
			cout << s << " -- is a mirrored string." << endl << endl;
			}
			//否则,字符串s是一个普通的字符串
			else cout << s << " -- is not a palindrome." << endl << endl;
		}
	}
	return 0;
}

时间: 2024-12-26 03:38:32

UVA 401 Palindromes(回文词)的相关文章

hdu 1318 Palindromes(回文词)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1318 题意分析:输入每行包含一个字符串,判断此串是否为回文串或镜像串. 表面上看这道题有些复杂,如果能熟练运用字符数组的话,代码也颇为简洁.. /*Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 657 Accepted

UVa 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 i

回文词 (Palindromes,Uva401)

例题 3-3 回文词 (Palindromes,Uva401) 输入一个字符中,判断它是否为回文串以及镜像串.输入字符串保证不含数字0.所谓回文串,就是反转以后和原串相同,如abba和madam.所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE.注意,并不是每个字符在镜像之后都能得到一个合法字符.在本题中,每个字符的镜像如图3-3所示(空白项表示该字符镜像后不能得到一个合法字符). 输入的每行包含一个字符串(保证只有上述字符.不含空白字符),判断它是否为回文串和镜像串(共4种组合).每

UVa 401 Palindromes(简单字符串)

简单的判断是否是回文串.镜像串,然后自己写的真费劲,没逃掉刘汝佳的书,这里的代码很有技巧性,特别值得学习,额,其实他书上的代码都很精简 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

UVa401 回文词

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

回文词

#include <iostream> #include <stdio.h> #include <iomanip> #include <string> using namespace std; const char *rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";//字符A-9的镜像字符,没有镜像则为空格 const char * msg[] = {"not a palindrome",&

Vijos1327回文词

回文词 回文词是一种对称的字符串--也就是说,一个回文词,从左到右读和从右到左读得到的 结果是一样的.任意给定一个字符串,通过插入若干字符,都可以变成一个回文词.你的任务是写 一个程序,求出将给定字符串变成回文词所需插入的最少字符数. 比如字符串"Ab3bd",在插入两个字符后可以变成一个回文词("dAb3bAd"或"Adb3bdA"). 然而,插入两个以下的字符无法使它变成一个回文词. 输入格式: 文件的第一行包含一个整数N,表示给定的字符串的

csuoj 1328: 近似回文词

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1328 1328: 近似回文词 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 355  Solved: 138 [Submit][Status][Web Board] Description 输入一行文本,输出最长近似回文词连续子串.所谓近似回文词是指满足以下条件的字符串: 1. S以字母开头,字母结尾 2. a(S)和b(S)最多有2k个位置不同

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

原博文地址:http://blog.csdn.net/u012773338/article/details/39857997 最长回文子串 描述:输入一个字符串,求出其中最长的回文子串.子串的含义是:在原串连续出现的字符 串片段.回文的含义是:正着看和倒着看是相同的,如abba和abbebba.在判断是要求忽略所有的标点和空格,且忽略大小写,但输出时按原样输出(首 尾不要输出多余的字符串).输入字符串长度大于等于1小于等于5000,且单独占一行(如果有多组答案,输出第一组). 输入 :每行有一个