aiheshan

有多自律,才能有多自由

导航

UVa 401 - Palindromes

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=342
 
题目:
1. 回文数: 一串由字母和数字组成的字符串,正着读和逆着读是相同的;例如"ABCDEDCBA";
2. 镜像数:一串由字母和数字组成的字符串,当每个字符都改成它的逆字符,逆着读与原来字符串相同;
3. 镜像回文数:符合回文数和镜像数的特点;
 
所有有效字符的逆字符:
注意:零'0' 不属于有效字符;
 
输入:
多行字符串,每一行包括1-20个有效字符;
 
输出:
对于每一行字符串,先打印字符串然后再打印判断结果;
每打印一行结果,然后打印一个空行;
 
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.
 
思路分析:
分别判断字符串是否为回文字符串和镜像字符串;
char aa[4][40] 表示4种打印结果;
char bb[40] 记录有效字符的逆字符,其中不存在逆字符的记为'0';
回文字符串: 对于字符串s,存在s[i]=s[len-i-1];
镜像字符串:对于字符串s,如果存在不存在逆字符的字符,则一定不是镜像字符串; 将字符串s中字符均换成逆字符,然后再判断逆着读是否与原来字符串相同;
 
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
 
char aa[4][40]={" -- is not a palindrome."," -- is a regular palindrome."," -- is a mirrored string."," -- is a mirrored palindrome."};
 
char bb[40]={'A','0','0','0','3','0','0','H','I','L','0','J','M','0','O',
'0','0','0','2','T','U','V','W','X','Y','5','1','S','E','0','Z','0','0','8','0'};
 
bool palindome(string);
bool mirrored(string);
 
int main()
{
   // freopen("input.txt","r",stdin);
    string s;
   bool f1,f2;
   int i;
   while(cin>>s)
   {
       f1=palindome(s);
       f2=mirrored(s);
     if(!f1&&(!f2))
         cout<<s<<aa[0]<<endl;
     else if(f1&&!f2)
             cout<<s<<aa[1]<<endl;
      else if(!f1&&f2)
              cout<<s<<aa[2]<<endl;
         else
               cout<<s<<aa[3]<<endl;
      cout<<endl;
    }
     return 0;
 }
 
bool palindome(string s)
{
    int i,len;
    len=s.length();
    for(i=0;i<len/2;i++)
        if(s[i]!=s[len-i-1])
            return false;
    return true;
 }
 
bool mirrored(string s)
{
   int i,len,j=0;
   string s1;
   len=s.length();
   for(i=0;i<s.length();i++)
     { 
       if(s[i]>='A'&&s[i]<='Z')
         {
            if(bb[s[i]-'A']=='0')
               return false;
            s1[i]=bb[s[i]-'A'];
         }
       else
         {
            if(bb[s[i]-'0'+25]=='0')
               return false;
            s1[i]=bb[s[i]-'0'+25];
         }
     }
     for(j=0;j<s.length();j++)
        if(s[j]!=s1[--i])
            return false;
      return true;
}

  

posted on 2018-04-16 10:04  aiheshan  阅读(202)  评论(0编辑  收藏  举报