Uva 401 2015.5.25
Source: Uva 401 Palindromes
Category: string
Abstract: Input several lines of strings,then justify whether it is a mirrored string or palindrome. There is a mirroredletters table.
WA: 我一开始把mirrored string理解错了,我以为是要替换以后是mirrored 就可以,原题的意思是,不用替换,直接判断是不是镜面对称就行,我多此一举了。
Idea:
1 use twofunctions to justify whether it is P (palindrome) or M (Mirror).
M: 先判断所有的字母是不是镜面对称;
再判断串是不是镜面对称。
2 There are twotables to accompany M:
1) M[]: 判断是否是镜面对称的字母
2) T[]: 找到相应的镜面对称的字母
A regular palindrome isa string of numbers or letters that is the same forward as backward. Forexample, 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 of the stringis changed to its reverse (if it has a reverse) and the string is readbackwards the result is the same as the original string. For example, thestring "3AIAE" is a mirroredstring 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 regularpalindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if thestring is read backwards, the string is the same as the original and because ifeach of the characters is replaced by its reverse and the result is readbackwards, 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 samecharacter and therefore ONLY the letter "0" is avalid character.
Input consistsof strings (one per line) each of which will consist of one to twenty validcharacters. There will be no invalid characters in any of the strings. Yourprogram should read to the end of file.
Foreach input string, you should print the string starting in column 1 immediatelyfollowed 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 outputline is to include the -'s and spacing exactlyas shown in the table above and demonstrated in the Sample Output below.
In addition, after eachoutput line, you must print an empty line.
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
NOTAPALINDROME -- is nota palindrome.
ISAPALINILAPASI -- is aregular palindrome.
2A3MEAS -- is a mirrored string.
ATOYOTA -- is a mirrored palindrome.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 100000
int T[256];
// T[]: the corresponding mirrored letter
bool M[256];
// M[]: whether the letter is a mirrored letter
char word[MAXN];
void Generate_Reverse_Table()
{
memset(T,0,sizeof(T));
memset(M,false,sizeof(M));
M['A']=true;M['M']=true;M['Y']=true;
M['E']=true;M['O']=true;M['Z']=true;
M['H']=true;M['S']=true;M['1']=true;
M['I']=true;M['T']=true;M['2']=true;
M['J']=true;M['U']=true;M['3']=true;
M['L']=true;M['V']=true;M['5']=true;
M['W']=true;M['8']=true;
M['X']=true;
T['A']='A';T['M']='M';T['Y']='Y';
T['E']='3';T['O']='O';T['Z']='5';
T['H']='H';T['S']='2';T['1']='1';
T['I']='I';T['T']='T';T['2']='S';
T['J']='L';T['U']='U';T['3']='E';
T['L']='J';T['V']='V';T['5']='Z';
T['W']='W';T['8']='8';
T['X']='X';
}
// Whether a string is palindromes
bool is_Palindromes(char s[MAXN])
{
int len=strlen(s);
int len2=len/2;
for(int i=0;i<=len2;i++)
if (s[i]!=s[len-1-i])
return false;
return true;
}
// Whether a string is mirrored
bool is_Mirror(char s[MAXN])
{
int len=strlen(s);
int len2=len/2;
for(int i=0;i<len;i++)
if (!M[s[i]]) return false;
for(int i=0;i<=len2;i++)
if (s[i]!=T[s[len-1-i]]) return false;
return true;
}
int main()
{
bool p,m;
Generate_Reverse_Table();
while (scanf("%s",word)!=EOF)
{
p=false;
m=false;
p = is_Palindromes(word);
m = is_Mirror(word);
if (p && m)
printf("%s -- is a mirrored palindrome.\n",word);
else
if (!p && m)
printf("%s -- is a mirrored string.\n",word);
else
if (p && !m)
printf("%s -- is a regular palindrome.\n",word);
else
printf("%s -- is not a palindrome.\n",word);
printf("\n");
}
return 0;
}