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 
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.

Input Samples

NOTAPALINDROME 
ISAPALINILAPASI 
2A3MEAS 
ATOYOTA
Output Samples

NOTAPALINDROME -- is not a palindrome.
 
ISAPALINILAPASI -- is a regular palindrome.
 
2A3MEAS -- is a mirrored string.
 
ATOYOTA -- is a mirrored palindrome.
这道题的意思很明确,就是判断输入的字符串是否是回文串以及镜像串,所谓的镜像串就是根据题目所给的映射转换为新的字符串(如果有映射的话),将转换后的字符串从后往前看如果和之前输入的字符串一样的话,就是镜像串,题目没有什么难度,只是我写完后总是在判断镜像串那个地方出错,搞了好久,问了我的小伙伴才知道原来是初始化问题,在函数里面定义了一个字符数组reversed[],我没有初始化,我以为默认会是0的,结果证明我想多了,查了才知道全局变量没问题,因为开机的时候已经自动初始化为0了,但是如果这在函数中可能就有问题,这是操作系统只是给你一个地址,这个内存地址可能被其它人用过,值也不确定,所以可能出问题,所以要初始化,这种非逻辑方法错误真是害死人啊,因为根本想不到哪里错了,宗旨以后遇到数组就初始化还是好的习惯啊。下面贴上代码,因为比较简单,我就不做解释了,已经AC。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
char rev_str[]="A###3##HIL#JM#O###2TUVWXY5";
char rev_num[]="01SE#Z##8#"; 
char s[51];
char reversed[51];
int is_palindrome(char *a)
{
	int l=strlen(a);
	for(int i=0;i<l;i++){
		if((a[i]=='0'&&a[l-1-i]=='O')||(a[i]=='O'&&a[l-1-i]=='0')) continue;
		if(a[i]!=a[l-1-i]) return 0;
	}
	return 1;
}
int is_mirrored(char *a)
{
	char reversed[51];
	memset(reversed,0,sizeof(reversed));
	int l=strlen(a);
	for(int i=0;i<l;i++){
		if(isalpha(a[i])){
		reversed[l-1-i]=rev_str[a[i]-'A'];
		}else
			reversed[l-1-i]=rev_num[a[i]-'0'];
		if(reversed[l-1-i]=='#') return 0;
	}
		if(strcmp(reversed,a)==0) return 1;
	return 0;	
}
int main()
{
	while(scanf("%s",s)!=EOF){
		if(is_palindrome(s)&&is_mirrored(s)){
			printf("%s -- is a mirrored palindrome.\n\n",s);
			continue;
		}
		else if(is_palindrome(s)){
			printf("%s -- is a regular palindrome.\n\n",s);
			continue;
		}
		else if(is_mirrored(s)){
			printf("%s -- is a mirrored string.\n\n",s);
			continue;
		}
		printf("%s -- is not a palindrome.\n\n",s);
		memset(s,0,sizeof(s));
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章