您的位置:首页 > 其它

Palindromes - uva401

2018-03-29 22:01 295 查看
A regular palindrome is a string of numbers or letters that is the same forward as backward. Forexample, the string “ABCDEDCBA” is a palindrome because it is the same when the string is read fromleft 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 itsreverse (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 criteriaof a mirrored string. The string “ATOYOTA” is a mirrored palindrome because if the string is readbackwards, the string is the same as the original and because if each of the characters is replaced byits 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 ReverseA A M M Y YB N Z 5C O O 1 1D P 2 SE 3 Q 3 EF R 4G S 2 5 ZH H T T 6I I U U 7J L V V 8 8K W W 9L J X XNote that ‘0’ (zero) and ‘O’ (the letter) are considered the same character and therefore ONLY theletter ‘O’ is a valid character.InputInput 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.OutputFor each input string, you should print the string starting in column 1 immediately followed by exactlyone 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 stringNote that the output line is to include the ‘-’s and spacing exactly as shown in the table above anddemonstrated in the Sample Output below.In addition, after each output line, you must print an empty line.Sample InputNOTAPALINDROMEISAPALINILAPASI2A3MEASATOYOTASample OutputNOTAPALINDROME -- is not a palindrome.ISAPALINILAPASI -- is a regular palindrome.2A3MEAS -- is a mirrored string.ATOYOTA -- is a mirrored palindrome.
分析:
从书上学习的代码,很简洁也很清晰,值得学习!
思路也很棒!
主要体现在判断镜像串的时候
设一个数组,把reverse后的值按顺序存入(空格也要存)
对于每一个输入的字符,通过调用函数返回一个它对应的reverse值,若与它对应位置的字符不等于返回值,则不是
函数也很巧妙,通过判断字符是字母还是数字,分别处理,得到相应的字符#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
//存对照信息
char res[]={"A 3 HIL JM O 2TUVWXY51SE Z 8 "};

char ch(char m){
if(isalpha(m))return res[m-'A'];
return res[m-'0'+25];
}

int main(){
char s[50];
while(scanf("%s",s)!=EOF){
int len=strlen(s);
int flag1=1,flag2=1;
for(int i=0;i<(len+1)/2;i
4000
++){
if(s[i]!=s[len-i-1])flag1=0;
if(ch(s[i])!=s[len-1-i])flag2=0;
}
if(!flag1&&!flag2)cout<<s<<" -- is not a palindrome."<<endl;
if(flag1&&!flag2)cout<<s<<" -- is a regular palindrome."<<endl;
if(!flag1&&flag2)cout<<s<<" -- is a mirrored string."<<endl;
if(flag1&&flag2)cout<<s<<" -- is a mirrored palindrome."<<endl;
cout<<endl;
memset(s,0,sizeof(s));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: