UVA 401 - 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 |
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.
#define RUN #ifdef RUN #include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <vector> #include <list> #include <cctype> #include <algorithm> #include <utility> #include <math.h> using namespace std; #define MAXN 21 int n = 0; char input[MAXN]; map<char,char> mymap; void initMap(){ mymap['A'] = 'A'; mymap['E'] = '3'; mymap['H'] = 'H'; mymap['I'] = 'I'; mymap['J'] = 'L'; mymap['L'] = 'J'; mymap['M'] = 'M'; mymap['O'] = 'O'; mymap['S'] = '2'; mymap['T'] = 'T'; mymap['U'] = 'U'; mymap['V'] = 'V'; mymap['W'] = 'W'; mymap['X'] = 'X'; mymap['Y'] = 'Y'; mymap['Z'] = '5'; mymap['1'] = '1'; mymap['2'] = 'S'; mymap['3'] = 'E'; mymap['5'] = 'Z'; mymap['8'] = '8'; } bool isPalindrome(char* string){ int validLen = strlen(string); //printf("validLen = %d\n", validLen); for(int i=0; i<=validLen/2; i++){ if(string[i] != string[validLen-1-i]){ return false; } } return true; } bool isMirrorString(char* string){ char temp[MAXN]; strcpy(temp, string); for(int i=0; i<strlen(string); i++){ map<char,char>::iterator it = mymap.find(string[i]); if(it != mymap.end()){ temp[i] = it->second; } else{ // We must deal with those chars that we cannot find! temp[i] = '?'; } } for(int i=0; i<strlen(string); i++){ if(string[i] != temp[strlen(string)-1-i]){ return false; } } return true; } int main(){ #ifndef ONLINE_JUDGE freopen("401.in", "r", stdin); freopen("401.out", "w", stdout); #endif memset(input, 0, sizeof(input)); initMap(); while(scanf("%s", input) != EOF){ //printf("%s\n", input); /* if(isPalindrome(input)){ printf("isPalindrome\n"); } else{ printf("Not Palindrome\n"); } */ if(!isPalindrome(input) && !isMirrorString(input)){ printf("%s%s\n\n", input, " -- is not a palindrome."); } else if(isPalindrome(input) && !isMirrorString(input)){ printf("%s%s\n\n", input, " -- is a regular palindrome."); } else if(!isPalindrome(input) && isMirrorString(input)){ printf("%s%s\n\n", input, " -- is a mirrored string."); } else if(isPalindrome(input) && isMirrorString(input)){ printf("%s%s\n\n", input, " -- is a mirrored palindrome."); } } } #endif