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.
题目大意:给你一个字符串判断是不是对称串,是不是镜像串。对称串我就不解释了,镜像串就是根据题目中的表,变换后是对称的字符串 比如 ‘2’跟‘S’是镜像则字符串"2S"就是镜像串。输出我也不解释了。睡觉了==。
题目是挺水的,我人也挺水的。改了这里错了,少了那里,==.前面一直写不对,重写了一份然后把前面的改对了所以有两份AC代码。其实差不多。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAXN 256
#define MAXM 30
char Rev[MAXN];
char str[MAXM];
bool rp,ms;
bool isregular(){
int l = strlen(str);
for(int i = 0;i < l/2;i++){
if (str[i] != str[l-1-i])
return false;
}
return true;
}
bool ismirror(){
int l = strlen(str);
for(int i = 0;i < (l+1)/2;i++){
if (str[l-i-1] != Rev[str[i]])
return false;
}
return true;
}
void changeZerotoO(){
int l = strlen(str);
for(int i = 0;i < l;i++){
if (str[i] == '0')
str[i] = 'O';
}
}
int main(){
//freopen("in.txt","r",stdin);
memset(Rev,0,sizeof(Rev));
Rev['A'] = 'A';
Rev['E'] = '3';
Rev['H'] = 'H';
Rev['I'] = 'I';
Rev['J'] = 'L';
Rev['L'] = 'J';
Rev['M'] = 'M';
Rev['O'] = 'O';
Rev['S'] = '2';
Rev['T'] = 'T';
Rev['U'] = 'U';
Rev['V'] = 'V';
Rev['W'] = 'W';
Rev['X'] = 'X';
Rev['Y'] = 'Y';
Rev['Z'] = '5';
Rev['1'] = '1';
Rev['2'] = 'S';
Rev['3'] = 'E';
Rev['5'] = 'Z';
Rev['8'] = '8';
while(scanf("%s",str)!=EOF){
changeZerotoO();
rp = isregular();
ms = ismirror();
printf("%s -- is ",str);
if (rp && ms){
printf("a mirrored palindrome.\n");
}
else if (rp && !ms){
printf("a regular palindrome.\n");
}
else if (!rp && ms){
printf("a mirrored string.\n");
}
else{
printf("not a palindrome.\n");
}
printf("\n");
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#define MAXN 300
char Rev[MAXN];
char str[MAXN];
using namespace std;
bool ispali(){
int l = strlen(str);
for(int i = 0;i < l/2;i++){
if (str[i] != str[l-i-1])
return false;
}
return true;
}
bool ismir(){
int l = strlen(str);
for(int i = 0;i < (l+1)/2;i++){
if (str[i] != Rev[str[l-i-1]])
return false;
}
return true;
}
void changeZerotoO(){
int l = strlen(str);
for(int i = 0;i < l;i++){
if (str[i] == '0')
str[i] = 'O';
}
}
int main(){
//freopen("in.txt","r",stdin);
memset(Rev,0,sizeof(Rev));
Rev['A'] = 'A'; Rev['E'] = '3'; Rev['H'] = 'H';
Rev['I'] = 'I'; Rev['J'] = 'L'; Rev['L'] = 'J';
Rev['M'] = 'M'; Rev['O'] = 'O'; Rev['S'] = '2';
Rev['T'] = 'T'; Rev['U'] = 'U'; Rev['V'] = 'V';
Rev['W'] = 'W'; Rev['X'] = 'X'; Rev['Y'] = 'Y';
Rev['Z'] = '5'; Rev['1'] = '1'; Rev['2'] = 'S';
Rev['3'] = 'E'; Rev['5'] = 'Z'; Rev['8'] = '8';
while(scanf("%s",str)!=EOF){
changeZerotoO();
bool pa = ispali();
bool mr = ismir();
printf("%s -- is ",str);
if (pa && mr){
printf("a mirrored palindrome.\n");
}
else if (!pa && mr){
printf("a mirrored string.\n");
}
else if (pa && !mr){
printf("a regular palindrome.\n");
}
else{
printf("not a palindrome.\n");
}
printf("\n");
}
return 0;
}

本文介绍了一种算法,用于判断输入字符串是否为对称串(正读反读一致)或镜像串(根据特定字符映射反转后仍保持一致)。通过两个示例代码详细展示了如何实现这一功能。
281

被折叠的 条评论
为什么被折叠?



