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;
    }
}

2 comments:

  1. What is wrong with this code, it is not getting accepted:
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;

    bool mirror(string x)
    {
    char revList[35] = { 'A', ' ', ' ', ' ', '3', ' ',
    ' ', 'H', 'I', 'L', ' ', 'J', 'M', ' ', 'O', ' ', ' ',
    ' ', ' ', 'T', 'U', 'V', 'W', 'X', 'Y', '5',

    '1', 'S','E', ' ', 'Z', ' ', ' ', '8', ' ' }; // digits

    char c;
    int s = x.size()-1;
    int i;

    for(i = 0; i <= s ; i++, s--)
    {
    c = x[i];
    if(c>=49 && c<=57) c = revList[c-23]; // for digits
    else c = revList[c-'A'];

    if(c != x[s]) return false;
    }

    return true;
    }

    bool palindrome(string x)
    {
    int b = 0;
    int e = x.size() - 1;

    while(b<=e)
    {
    if(x[b++] != x[e--]) return false;
    }

    return true;
    }

    int main()
    {

    string str;

    while(cin >> str)
    {
    if(palindrome(str))
    {
    if(mirror(str)) cout << str << " -- is a mirrored palindrome.\n";
    else cout << str << " -- is a regular palindrome.\n";
    }
    else
    {
    if(mirror(str)) cout << str << " -- is a mirrored string.\n";
    else cout << str << " -- is not a palindrome.\n";
    }
    }

    return 0;
    }

    ReplyDelete
  2. The includes aren't specified at the top.

    ReplyDelete