Pages

Sunday, 16 September 2012

UVA - 401 - Palindromes


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Main {

    public static void main(String[] args) throws IOException {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        StringBuffer sb = new StringBuffer("");
        String m="";
        while((m=br.readLine())!=null){
            m=m.trim();
            boolean pali=pali(m);
            boolean mirror=mirror(m);
            if(pali){
                if(mirror)
                    sb.append(m).append(" -- is a mirrored palindrome.\n");
                else
                    sb.append(m).append(" -- is a regular palindrome.\n");
            }else{
                if(mirror)
                    sb.append(m).append(" -- is a mirrored string.\n");
                else
                    sb.append(m).append(" -- is not a palindrome.\n");
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
   
    static boolean pali(String s){
        for(int i=0,j=s.length()-1;i<s.length()/2;i++,j--){
           if(s.charAt(i)!=s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
   
    static boolean mirror(String s){
        if(s.length()%2==1){
            int len=(s.length()/2);
            if(!reverse(s.charAt(len),s.charAt(len)))
                return false;
        }
        for(int i=0,j=s.length()-1;i<s.length()/2;i++,j--){
           if(!reverse(s.charAt(i),s.charAt(j))) {
                return false;
            }
        }
        return true;
    }
   
    static boolean reverse(char x,char y){
         if(x=='A' && y=='A')
            return true;
         if((x=='E' && y=='3')||(x=='3' && y=='E'))
            return true;
         if(x=='H' && y=='H')
            return true;
         if(x=='I' && y=='I')
            return true;
         if((x=='J' && y=='L')||(x=='L' && y=='J'))
            return true;
         if(x=='M' && y=='M')
            return true;
         if((x=='O' && y=='O')||(x=='0' && y=='O'))
            return true;
         if((x=='0' && y=='0')||(x=='O' && y=='0'))
            return true;
         if((x=='S' && y=='2')||(x=='2' && y=='S'))
            return true;
         if(x=='T' && y=='T')
            return true;
         if(x=='U' && y=='U')
            return true;
         if(x=='V' && y=='V')
            return true;
         if(x=='W' && y=='W')
            return true;
         if(x=='X' && y=='X')
            return true;
         if(x=='Y' && y=='Y')
            return true;
         if((x=='Z' && y=='5')||(x=='5' && y=='Z'))
            return true;
         if(x=='1' && y=='1')
            return true;
         if(x=='8' && y=='8')
            return true;
         return false;
    }
}

No comments:

Post a Comment