zoukankan      html  css  js  c++  java
  • UVa 401

    题意

    输入一个字符串,判断它是否为回文串以及镜像串。输入字符串保证不含数字0。所谓回文串,就是反转以后和原串相同,如abba和madam。所有镜像串,就是左右镜像之后和原串相同,如2S和3AIAE。注意,并不是每个字符在镜像之后都能得到一个合法字符。

    思路

    水题
    用字符串记录一下镜像表即可

    但是要注意几个细节:
    1. 当已经判断某串为镜像/镜像回文时 , 如果串长为奇数, 要特判最中间元素是否镜像, 如果最中间元素不镜像那么这就是一个普通回文串
    2. 特判串长为1的 , 如果该元素镜像则是镜像回文, 否则只是回文

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    using namespace std;
    
    const int maxn = 30;
    char s[maxn];
    
    char ms[] = " SE Z               3    L J      2      5";
    char mp[] = "1      8        A      HI   M O    TUVWXY ";
    
    int main()
    {
        int len;
        while( ~scanf("%s",s) )
        {
            len = strlen(s);
            bool MS = false, MP = false, OR = false;
            if( len == 1 ){
                if( mp[s[0]-'1'] != ' ' )   MP = true;
                else OR = true;
            }
            for( int i = 0; i < len/2; i++ ){
                if( s[i] == s[len-i-1]){
                    if( mp[s[i]-'1'] != ' ' ){
                        MP = true;
                        if(MS) MP = false;
                        if(OR) MP = false;
                    }
                    else{
                        OR = true;
                        MP = false;
                    }
                }
                else{
                    if( ms[s[i]-'1'] == s[len-i-1] ){
                        MS = true;
                        if(MP) MP = false;
                        if(OR) MS = false;
                    }
                    else{
                        OR = false;
                        break;
                    }
                }
                //printf("%d & %d
    ",i,len-i-1);
                //printf("MP:%d   MS:%d   OR:%d
    ",MP,MS,OR);
            }
            if( len % 2 != 0 ){
                if( MP && mp[s[len/2]-'1'] == ' ' ){
                    MP = false;
                    OR = true;
                }
                else if( MS && mp[s[len/2]-'1'] == ' ' ){
                    MS = false;
                    OR = false;
                }
            }
            if( MS )     cout << s << " -- is a mirrored string." << endl << endl;
            else if( MP )     cout << s << " -- is a mirrored palindrome." << endl << endl;
            else if( OR )    cout << s << " -- is a regular palindrome." << endl << endl;
            else     cout << s << " -- is not a palindrome." << endl << endl;
        }
        return 0;
    }
  • 相关阅读:
    33选6算法:M个数N个为一组,无重复的排列组合
    文件墙 CFilewall
    公司代码阅读笔记 记于 2013-09-23
    简单的同步MSMQ
    Markdown 使用说明
    Freezable 对象(WPF)
    AngularJS-入门篇
    第二节Unity3D开发环境安装(windows系统)
    第一节Unity3D简介
    数据库出现可疑解决办法
  • 原文地址:https://www.cnblogs.com/JinxiSui/p/9740628.html
Copyright © 2011-2022 走看看