欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

例题3-3 回文词(Palindromes,UVa401)

程序员文章站 2022-03-04 22:53:52
...

3-3 例题3-3 回文词(Palindromes,UVa401)

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

例题3-3 回文词(Palindromes,UVa401)

输入的每行包含一个字符串(保证只有上述字符。不含空白字符),判断它是否为回文串和镜像串(共4种组合)。每组数据之后输出一个空行。

样例输入:

NOTAPALINDROME

ISAPALINILAPASI

2A3MEAS

ATOYOTA

样例输出:

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.

【分析】

既然不包含空白字符,可以安全地使用scanf进行输入。回文串和镜像串的判断都不复杂,并且可以一起完成,详见下面的代码。使用常量数组,只用少量代码即可解决这个看上去有些复杂的题目。

#include<stdio.h>
#include<string.h>
#include<ctype.h>
//常量字符串,存在镜像列表,与大写字母列表的顺序是对应的
const char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";//一定要与列表对应,特别是空格
const char* msg[] = {"not a palindrome", "a regular palindrome", 
					"a mirrored string", "a mirrored palindrome"};
char r(char ch) {//返回ch的镜像字符
	if(isalpha(ch)) return rev[ch - 'A'];
	else return rev[ch - '0' + 25];//如果是数字,返回镜像列表中对应的镜像
}
int main() {
	char s[30];
	while(scanf("%s", s) == 1) {
		int len = strlen(s);
		int p = 1, m = 1;
		for(int i = 0; i < (len+1)/2; i++) {
			if(s[i] != s[len-1-i]) p = 0; //不是回文串
			if(r(s[i])!= s[len-1-i]) m = 0; //不是镜像串
		}
		printf("%s -- is %s.\n\n", s, msg[m*2+p]);
	}
	return 0;
}

原文如下:

A regular palindrome is a string of numbers or letters that is thesame forward as backward. For example, the string “ABCDEDCBA” is a palindromebecause it is the same when the string is read from left to right as when thestring is read from right to left.

A mirrored string is a string for which when each of the elements ofthe string is changed to its reverse (if it has a reverse) and the string isread 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 aregular palindrome and the criteria of a mirrored string. The string “ATOYOTA”is a mirrored palindrome because if the string is read backwards, the string isthe same as the original and because if each of the characters is replaced by itsreverse and the result is read backwards, the result is the same as theoriginal 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 ‘0’ (zero) and ‘O’ (the letter) are considered the samecharacter and therefore ONLY the

letter ‘O’ is a valid character.

Input

Input consists of strings (one per line) each of which will consistof one to twenty valid characters.

There will be no invalid characters in any of the strings. Yourprogram should read to the end of file.

Output

For each input string, you should print the string starting incolumn 1 immediately followed by exactly

one of the following strings.

                            STRING                                                                                      CRITERIA

‘ -- is not apalindrome.’               if the stringis not a palindrome and is not a mirrored string

‘ -- is aregular palindrome.’    if the string isa palindrome and is not a mirrored string

‘ -- is amirrored string.’       if the string isnot a palindrome and is a mirrored string

‘ -- is amirrored palindrome.’   if the string isa palindrome and is a mirrored string

Note that the output line is to include the ‘-’s and spacing exactlyas 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.

提示3-20:头文件ctype.h中定义的isalpha、isdigit、isprint等工具可以用来判断字符的属性,而toupper、tolower等工具可以用来转换大小写。如果ch是大写字母,则ch-'A'就是它在字母表中的序号(A的序号是0,B的序号是1,依此类推);类似地,如果ch是数字,则ch-'0'就是这个数字的数值本身。