呵呵,又继续刷题了,这次是字符串练习题。。。 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.
字符串的练习,镜像字符比较少见,用了两个函数判断是否回文与镜像,然后根据情况输出。
把数组清空时第一次用了memset函数,意外的好用。
代码如下:
#include<stdio.h>
#include<string.h>
char ch[200];
int len, isp, ism;
int ispld(void);
int ismrs(void);
char mr(char n);
int main()
{
for (;(scanf("%s", ch)) != EOF;memset(ch, 0, 200)){
len = strlen(ch);
if (len == 1)
if (ch[0] == mr(ch[0])){
printf("%s -- is a mirrored string.\n\n", ch);
continue;
}else{
printf("%s -- is a regular palindrome.\n\n", ch);
continue;
}
isp = ispld();
ism = ismrs();
if (isp == 1 && ism == 1)
printf("%s -- is a mirrored palindrome.\n\n", ch);
if (isp == 1 && ism == 0)
printf("%s -- is a regular palindrome.\n\n", ch);
if (isp == 0 && ism == 1)
printf("%s -- is a mirrored string.\n\n", ch);
if (isp == 0 && ism == 0)
printf("%s -- is not a palindrome.\n\n", ch);
}
return 0;
}
int ispld(void)
{
int i;
for (i = 0; i < len / 2; i ++)
if (ch[i] != ch[len - i - 1])
return 0;
return 1;
}
int ismrs(void)
{
int i;
for (i = 0; i < len / 2; i ++)
if (ch[i] != mr(ch[len - i - 1]))
return 0;
return 1;
}
char mr(char n)
{
switch (n){
case 'A': return 'A';
case 'E': return '3';
case 'H': return 'H';
case 'I': return 'I';
case 'J': return 'L';
case 'L': return 'J';
case 'M': return 'M';
case 'O': return 'O';
case 'S': return '2';
case 'T': return 'T';
case 'U': return 'U';
case 'V': return 'V';
case 'W': return 'W';
case 'X': return 'X';
case 'Y': return 'Y';
case 'Z': return '5';
case '1': return '1';
case '2': return 'S';
case '3': return 'E';
case '5': return 'Z';
case '8': return '8';
default: return 0;
}
}
Submit后WA了。。。
于是网上找解题报告,发现是0与O的问题。我觉得也是,但却不知道ONLY the letter "0" is a valid character是什么意思(其实现在也糊里糊涂的)。。。干脆把0全部转化为O来处理,结果AC了。很奇怪的测试点,不知道测试的什么。。。
浏览别人解题报告时发现他们都用两个数组就解决了我里面的mr()函数,而我却用了那么长的代码来搞,弱爆了。。。这次学乖了以后要灵活点。。。
AC代码:
#include<stdio.h>
#include<string.h>
char ch[200];
int len, isp, ism;
int ispld(void);
int ismrs(void);
char mr(char n);
int main()
{
for (;(scanf("%s", ch)) != EOF;memset(ch, 0, 200)){
len = strlen(ch);
for (int i = 0; i < len; i ++)
if (ch[i] == '0')
ch[i] = 'O';
isp = ispld();
ism = ismrs();
if (isp == 1 && ism == 1)
printf("%s -- is a mirrored palindrome.\n\n", ch);
if (isp == 1 && ism == 0)
printf("%s -- is a regular palindrome.\n\n", ch);
if (isp == 0 && ism == 1)
printf("%s -- is a mirrored string.\n\n", ch);
if (isp == 0 && ism == 0)
printf("%s -- is not a palindrome.\n\n", ch);
}
return 0;
}
int ispld(void)
{
int i;
for (i = 0; i <= (len + 1) / 2; i ++)
if (ch[i] != ch[len - i - 1])
return 0;
return 1;
}
int ismrs(void)
{
int i;
for (i = 0; i <= (len + 1) / 2; i ++)
if (ch[i] != mr(ch[len - i - 1]))
return 0;
return 1;
}
char mr(char n)
{
switch (n){
case 'A': return 'A';
case 'E': return '3';
case 'H': return 'H';
case 'I': return 'I';
case 'J': return 'L';
case 'L': return 'J';
case 'M': return 'M';
case 'O': return 'O';
case 'S': return '2';
case 'T': return 'T';
case 'U': return 'U';
case 'V': return 'V';
case 'W': return 'W';
case 'X': return 'X';
case 'Y': return 'Y';
case 'Z': return '5';
case '1': return '1';
case '2': return 'S';
case '3': return 'E';
case '5': return 'Z';
case '8': return '8';
default: return 0;
}
}