UVA 401 Solution

using namespace std;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define LLU long long unsigned int
char  mir[256]={‘ ‘};
bool palin(string s)
{
    int i,j;
    i=0,j=s.length()-1;
    while(s[j]==’ ‘)j–;
    while(s[i]==’ ‘)i++;
    for(;i<=j;i++,j–)
    {
        if(s[i]!=s[j])
        {
            return false;
        }
    }
    return true;
}
bool mirrored(string s)
{
    int i=0,j=s.length()-1;
    while(s[j]==’ ‘)j–;
    while(s[i]==’ ‘)i++;
    for(;i<=j;i++,j–)
    {
        if(mir[s[i]]==’ ‘ || mir[s[j]]==’ ‘) return false;
        else if(mir[s[i]]!=s[j])
        {
            return false;
        }
    }
    return true;
}
int main()
{
    string str;
    mir['A']=’A’;mir['E']=’3′;mir['H']=’H’;mir['I']=’I’;mir['J']=’L’;
    mir['L']=’J’;mir['M']=’M’;mir['O']=’O’;mir['S']=’2′;mir['T']=’T’;
    mir['U']=’U’;mir['V']=’V’;mir['W']=’W’;mir['X']=’X’;mir['Y']=’Y’;
    mir['Y']=’Y’;mir['Z']=’5′;mir['1']=’1′;mir['2']=’S’;mir['3']=’E’;
    mir['5']=’Z’;mir['8']=’8′;
    bool first=true;
    while(getline(cin,str))
    {
        bool ans1=mirrored(str),ans2=palin(str);
        if(!ans1 && !ans2)
        cout<<str<<” — is not a palindrome.\n”;
        else if(ans2 && !ans1)
        cout<<str<<” — is a regular palindrome.\n”;
        else if(ans1 && !ans2)
        cout<<str<<” — is a mirrored string.\n”;
        else
        cout<<str<<” — is a mirrored palindrome.\n”;
        cout<<endl;
    }
}

About these ads

Leave a comment

Filed under String, uva

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s