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

转载于:https://www.cnblogs.com/kkrisen/archive/2013/01/17/2864742.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值