UVA - 401 - Palindromes

8:49 PM | , , , , ,



There is a problem on UVA online judge which called Palindrome. A palindrome is a word, phrase, number, or other sequence of units that may be read the same way in either direction, with general allowances for adjustments to punctuation and word dividers.Composing literature in palindromes is an example of constrained writing

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.


CharacterReverseCharacterReverseCharacterReverse
AAMMYY
BNZ5
COO11
DP2S
E3Q3E
FR4
GS25Z
HHTT6
IIUU7
JLVV88
KWW9
LJXX



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.

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



Source Code with java



import java.util.Scanner;
class Palindrome {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        String mir1="AEHIJLMOSTUVWXYZ12358";
        String mir2="A3HILJMO2TUVWXY51SEZ8";
        boolean NP,RP,MP,MS; //NP=Not Poly RP=Regular Poly
        //MP=Mirror Poly  MS=Mirror String
        String hasil;
        while(s.hasNext()){
            //if(s.hasNextLine()) System.out.println("Pindah baris");
            String asli=s.nextLine();          
            String kata=asli;
            kata.replaceAll("0", "O");
            int i=0,k,N=kata.length();
            k=N/2;
            int l=-1,m=-1;
            if(N%2==0) k++;
            MP=true;
            RP=true;
            NP=true;
            MS=true;
            while((i<=k)){
                l=mir1.indexOf(kata.charAt(i));
                m=mir2.indexOf(kata.charAt(N-i-1));
                if(((kata.charAt(i)==kata.charAt(N-i-1)))&&((MP==true)||(RP==true))){                  
                    MP=false;
                    RP=true;
                    if((l>=0)&&(m>=0)){
                        if(mir1.charAt(l)==mir2.charAt(m)){
                            MP=true;                      
                        }
                    }                      
                }else{
                    MP=false;
                    RP=false;
                    if((l>=0)&&(m>=0)){
                        if((mir1.charAt(l)==mir2.charAt(m))&&(MS==true)){
                            MS=true;                      
                        }
                    }else{
                        MS=false;
                    }
                }
                i++;
            }
            System.out.print(asli+" -- ");
            if(MP==true){
                System.out.println("is a mirrored palindrome");
            }else if(RP==true){
                System.out.println("is a regular palindrome");
            }else if(MS==true){
                System.out.println("is a mirrored string");
            }else{
                System.out.println("is not a palindrome");
            }
        }
    }
}






0 comments:

Post a Comment

Please leave a comment