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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章