zoukankan      html  css  js  c++  java
  • UVA 401 Palindromes

      Palindromes 

    A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.


    A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I" are their own reverses, and "3" and "E" are each others' reverses.


    A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string "ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string. Of course, "A", "T", "O", and "Y" are all their own reverses.


    A list of all valid characters and their reverses is as follows.


    Character Reverse Character Reverse Character Reverse
    A A M M Y Y
    B   N   Z 5
    C   O O 1 1
    D   P   2 S
    E 3 Q   3 E
    F   R   4  
    G   S 2 5 Z
    H H T T 6  
    I I U U 7  
    J L V V 8 8
    K   W W 9  
    L J X X    


    Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.

    Input 

    Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.

    Output 

    For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.


    STRING CRITERIA
    " -- is not a palindrome." if the string is not a palindrome and is not a mirrored string
    " -- is a regular palindrome." if the string is a palindrome and is not a mirrored string
    " -- is a mirrored string." if the string is not a palindrome and is a mirrored string
    " -- is a mirrored palindrome." if the string is a palindrome and is a mirrored string

    Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below.

    In addition, after each output line, you must print an empty line.

    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.



    这道题一开始我在理解上就出现了问题,镜像字符,不仅是要有对应镜像,其字符串从背面看,也是一样的,有点类似回文的味道又不是回文
    因为其对应的字符可以不同,比如E对应3.所以EW3就是一个镜像字串。
    和大多数人的做法一样,首先分清板块,目标很明确,一个函数用来判断是否回文,一个函数用来判断是否镜像。这样,显得整个程序都很简明,
    主函数只要不到十行就写完了。而且正好我WA了几次后,发现没理解好题意,即判断镜像的时候还要考虑其具有“类回文性”
    由于是用函数实现
    对镜像的判断,所以修改起来比较方便,只要在函数里添加几行代码即可。
    下面是修改好AC的代码,自认为优化得还算可以。不到300ms运行时间。

    #include <iostream>
    #include <cstring>
    using namespace std;
    char *s1="AEHIJLMSTUVWXYZ12358O";
    char *s2=" 3  LJ 2      5 SEZ  ";
    bool reverse(char* cc,int l)
    {
        for (int i=0,q=l-1;q>=i;i++,q--)
         {
             if (cc[i]!=cc[q]) return false;
         }
         return true;
    }
    bool mirror(char *ch,int le)
    {
        for (int j=0,k=le-1;j<=k;j++,k--)
         {
             bool pan=false;
             for (int p=0;s1[p];p++)
             {
                 if (ch[j]==s1[p])
                  if (s2[p]==' '&&ch[j]==ch[k]||s2[p]!=' '&&ch[k]==s2[p])
                   {pan=true;break;}
             }
             if(!pan) return false;
         }
         return true;
    }
    int main()
    {
        char ca[1000];
        while (cin.getline(ca,100))
         {
             int len=strlen(ca);
             bool ispa=reverse(ca,len);
             bool ismi=mirror(ca,len);
             if (!ispa&&!ismi) {cout<<ca<<" -- is not a palindrome."<<endl;}
             if (!ispa&&ismi) {cout<<ca<<" -- is a mirrored string." <<endl;}
             if (ispa&&!ismi) {cout<<ca<<" -- is a regular palindrome."<<endl;}
             if (ispa&&ismi) {cout<<ca<<" -- is a mirrored palindrome."<<endl;}
             cout<<endl;
         }
         return 0;
    }


    如代码可见,为了实现对字符串的判断,首先定义了两个字串S1,S2,字串1主要是存贮了镜像字符,字串2是存贮了镜像字符非本身的字符。。。这种
    方法在做字符串类的题目时比较常见也比较实用,方便了之后对字符串的操作。。。还有像对这种字符串长度未知的题目,一定要注意只有一个字符的
    情况,如果没考虑只有一个字符的情况,很容易在设置循环变量时出现漏洞,导致读入一个字符时,根本不进行循环或者出现其他意想不到的结果。
    刘汝佳出的题目确实不错,需要对题目理解非常透彻。否则这种题目虽然好下手,层次分明,但是极易错,但是当你对他设定的规则了解清楚后,就会很好解决了

  • 相关阅读:
    我们应该如何防范黑客的攻击? 有哪些棘手问题?
    德国网络安全公司Avira被收购,估值为1.8亿美元
    物联网会成为黑客攻击的目标,智慧城市如何才安全?
    因新型冠状病毒,笔记本电脑销售增长,人们寻求更好的设备进行远程工作
    从电脑维修工到互联网大佬,他是怎么做到的?解读郭盛华最真实的传奇生涯
    企业防御网络风险保护计划的5个步骤
    加载失败图片加样式
    请求接口无权限
    iview button根据条件 disabled可用或者不可用
    vue跨组件传值
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2864742.html
Copyright © 2011-2022 走看看