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 thereforeONLY 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.
Miguel Revilla 2001-04-16
题目大意:如果是回文的字符串,那么就是"is a regular palindrome",如果是镜像对称的字符串,那么就是"is a mirrored string ",如果既是回文的又是镜像对称的字符串则是"is a mirrored palindrome"; 既不是回文的也不是镜像对称的就是"is not a palindrome".
思路:模拟..
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
char ch[25];
map<string,string>mapTmp;
int isPalindrome()
{
int len=strlen(ch),half;
if(len&1)
half=len/2;
else
half=(len-1)/2;
for(int i=0;i<=half;i++)
{
if(ch[i]!=ch[len-1-i]) //一旦出现两边不是同一个字母,利马返回失败
return 0;
}
return 1;
}
int isMirrored()
{
int len=strlen(ch),half;
if(len&1)
half=len/2;
else
half=(len-1)/2;
for(int i=0;i<=half;i++)
{
string tmp;
tmp.assign(ch,i,1); //在这用assign,是很把字符放到string变量中
string tmp1=mapTmp[tmp];
string tmp2;
tmp2.assign(ch,len-1-i,1);
//cout<<"tmp1 tmp2 "<<tmp1<<" "<<tmp2<<endl;
if(tmp1.compare(tmp2))//一旦出现两边不是镜像的,利马返回失败
return 0;
}
return 1;
}
int main()
{
mapTmp["A"]="A";
mapTmp["B"]="";
mapTmp["C"]="";
mapTmp["D"]="";
mapTmp["E"]="3";
mapTmp["F"]="";
mapTmp["G"]="";
mapTmp["H"]="H";
mapTmp["I"]="I";
mapTmp["J"]="L";
mapTmp["K"]="";
mapTmp["L"]="J";
mapTmp["M"]="M";
mapTmp["N"]="";
mapTmp["O"]="O";
mapTmp["P"]="";
mapTmp["Q"]="";
mapTmp["R"]="";
mapTmp["S"]="2";
mapTmp["T"]="T";
mapTmp["U"]="U";
mapTmp["V"]="V";
mapTmp["W"]="W";
mapTmp["X"]="X";
mapTmp["Y"]="Y";
mapTmp["Z"]="5";
mapTmp["1"]="1";
mapTmp["2"]="S";
mapTmp["3"]="E";
mapTmp["4"]="";
mapTmp["5"]="Z";
mapTmp["6"]="";
mapTmp["7"]="";
mapTmp["8"]="8";
mapTmp["9"]="";
while(~scanf("%s",ch))
{
int isP=isPalindrome();
int isM=isMirrored();
if(isP==0&&isM==0)//都不是
{
printf("%s -- is not a palindrome.\n\n",ch);
}
else if(isP==0&&isM==1)
{
printf("%s -- is a mirrored string.\n\n",ch);
}
else if(isP==1&&isM==0)
{
printf("%s -- is a regular palindrome.\n\n",ch);
}
else if(isP==1&&isM==1)
{
printf("%s -- is a mirrored palindrome.\n\n",ch);
}
}
return 0;
}
C++ string类的成员函数,用于拷贝、赋值操作,它们允许我们顺次地把一个string 对象的
部分内容拷贝到另一个string 对象上。
函数原型:
string &operator=(const string &s);把字符串s赋给当前字符串
string &assign(const char *s);用c类型字符串s赋值
string &assign(const char *s,int n);用c字符串s开始的n个字符赋值
string &assign(const string &s);把字符串s赋给当前字符串
string &assign(int n,char c);用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);把first和last迭代器之间的部分赋给字符串
上面的是用map写的,感觉其实大才小用.下面用的是字符串数组,其实也一样的.也是映射了,只不过,上面的空串我只好用一个空格来代替了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
using namespace std;
char ch[25];
char mapTmp[300];
int isPalindrome()
{
int len=strlen(ch),half;
if(len&1)
half=len/2;
else
half=(len-1)/2;
for(int i=0;i<=half;i++)
{
if(ch[i]!=ch[len-1-i]) //一旦出现两边不是同一个字母,利马返回失败
return 0;
}
return 1;
}
int isMirrored()
{
int len=strlen(ch),half;
if(len&1)
half=len/2;
else
half=(len-1)/2;
for(int i=0;i<=half;i++)
{
char tmp1=mapTmp[ch[i]];
if(tmp1!=ch[len-1-i]) //一旦出现两边不是镜像的,利马返回失败
return 0;
}
return 1;
}
int main()
{
mapTmp['A']='A';
mapTmp['B']=' ';
mapTmp['C']=' ';
mapTmp['D']=' ';
mapTmp['E']='3';
mapTmp['F']=' ';
mapTmp['G']=' ';
mapTmp['H']='H';
mapTmp['I']='I';
mapTmp['J']='L';
mapTmp['K']=' ';
mapTmp['L']='J';
mapTmp['M']='M';
mapTmp['N']=' ';
mapTmp['O']='O';
mapTmp['P']=' ';
mapTmp['Q']=' ';
mapTmp['R']=' ';
mapTmp['S']='2';
mapTmp['T']='T';
mapTmp['U']='U';
mapTmp['V']='V';
mapTmp['W']='W';
mapTmp['X']='X';
mapTmp['Y']='Y';
mapTmp['Z']='5';
mapTmp['1']='1';
mapTmp['2']='S';
mapTmp['3']='E';
mapTmp['4']=' ';
mapTmp['5']='Z';
mapTmp['6']=' ';
mapTmp['7']=' ';
mapTmp['8']='8';
mapTmp['9']=' ';
while(~scanf("%s",ch))
{
int isP=isPalindrome();
int isM=isMirrored();
if(isP==0&&isM==0)//都不是
{
printf("%s -- is not a palindrome.\n\n",ch);
}
else if(isP==0&&isM==1)
{
printf("%s -- is a mirrored string.\n\n",ch);
}
else if(isP==1&&isM==0)
{
printf("%s -- is a regular palindrome.\n\n",ch);
}
else if(isP==1&&isM==1)
{
printf("%s -- is a mirrored palindrome.\n\n",ch);
}
}
return 0;
}