poj 1590 Palindromes

//这题需要考虑的情况也比较多,有点烦,一定需要用清晰的思路,要不很容易出错,尤其是对四种情况的讨论! 
//这题WA了两次,一次是:输出的英文写错了,是自己手打的,没有进行对样例的复制粘贴,下次要谨记!
//第二次是没有讲‘O’和‘0’考虑进去,粗心了! 
#include <iostream>
#include <string> 
using namespace std;


int main()
{
    int i, j, len; 
    string str, tmp; 
    bool is_palindrome, is_mirrored, is_pm; 
    while (cin >> str){
          is_palindrome = false;
          is_mirrored = false; 
          is_pm = false; 
          len = str.length();
          tmp = str; 
          
          //判断是否为palindrome 
          for (i = 0, j = len-1; i < len/2; i++, j--){
              if ((str[i] == 'O' && str[j] == '0') || (str[i] == '0' || str[j] == 'O'))
                  continue; 
              else if (str[i] != str[j]){
                  is_palindrome = true;
                  break; 
              } 
          }
          
          //判断是否为mirrored 
          for (i = 0; i < len; i++){
               if (str[i] != 'A' && str[i] != 'M' && str[i] != 'Y' && 
                   str[i] != 'Z' && str[i] != 'O' && str[i] != '1' && 
                   str[i] != '2' && str[i] != 'E' && str[i] != '3' && 
                   str[i] != 'S' && str[i] != '5' && str[i] != 'H' && 
                   str[i] != 'T' && str[i] != 'I' && str[i] != 'U' && 
                   str[i] != 'J' && str[i] != 'V' && str[i] != '8' &&
                   str[i] != 'W' && str[i] != 'L' && str[i] != 'X'){ 
                         is_mirrored = true;
                         break; 
                   } 
               else{
                    if (str[i] == '2')
                        str[i] = 'S';
                    else if (str[i] == '3')
                        str[i] = 'E';
                    else if (str[i] == '5') 
                        str[i] = 'Z'; 
               } 
          }
          
          //在是mirrored的情况之下判断是否为palindrome,注意:这里用于判断回文的字符串应该是用最原始的字符串,并不是用用mirrored变换过来的字符串! 
          if (!is_mirrored){
               for (i = 0, j = len-1; i < len/2; i++, j--){
                   if ((str[i] == 'I' && str[j] == 'J') || (str[i] == 'J' && str[j] == 'I'))
                       continue;
                   else if (tmp[i] != tmp[j]){
                        is_pm = true;
                        break; 
                   } 
               } 
          } 
           
          //输出的时候需要注意,样例之间需要有一个空行! 
          if (is_palindrome && is_mirrored) 
              cout << tmp << " -- is not a palindrome." << endl << endl;
          else if (!is_mirrored && is_palindrome && is_pm)
              cout << tmp << " -- is a mirrored string." << endl << endl;
          else if (!is_palindrome && is_mirrored)
              cout << tmp << " -- is a regular palindrome." << endl << endl;
          else if (!is_pm)
              cout << tmp << " -- is a mirrored palindrome." << endl << endl;   
     } 
    
    system("pause"); 
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值