UVA Online Judge 题目401 Palindromes 回文串
问题描述:
回文串(Palindromes)就是正着读和反着读完全一样的字符串,例如"ABCDEDCBA"。
镜像串(Mirrored string)有些类似回文串,字符'3'的镜像可以看成是'E',字符'A'本身就是对称的所以它的镜像字符还是'A'。我们把像"3AIAE"这样的字符串看做镜像串。
镜像回文串(Mirrored palindrome)是符合上面两个条件的字符串,比如"ATOYOTA"。‘A’、‘T’、‘O’、‘Y’几个字符的镜像字符都是其自身。
详细的字符与镜像字符对应表如下:
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 |
注意:数字0被看做和字母O相同,并且输入中只包含有字母O。
输入格式:
每行为一个需要判断的字符串,读取到文件结束符后结束。
输出格式:
针对每一行输入,你需要判断其字符串类型并输出原字符串加上语句,见下表:
STRING | CRITERIA |
" -- is not a palindrome." | 既不是回文也不是镜像串 |
" -- is a regular palindrome." | 普通回文串 |
" -- is a mirrored string." | 镜像串 |
" -- is a mirrored palindrome." | 镜像回文串 |
注意:每行语句之间有一个空行,也就是说需要在字符串后面写上“\n\n”,这个地方WA了一次。。太坑爹了。
示例输入:
NOTAPALINDROME
ISAPALINILAPASI
2A3MEAS
ATOYOTA
示例输出:
NOTAPALINDROME -- is not a palindrome. ISAPALINILAPASI -- is a regular palindrome. 2A3MEAS -- is a mirrored string. ATOYOTA -- is a mirrored palindrome.
代码:(没注意输出格式WA一次。。)
/*
Problem : UVA Online Judge - 401 Palindromes
Date:2014-04-03
Author:Leroy
*/ #include <stdio.h>
#include <string.h> char ch[] = "AEHIJLMOSTUVWXYZ12358";
char chRe[] = "A3HILJMO2TUVWXY51SEZ8"; int hasRe(char c)
{
int has = ;
for (int i = ; i < ; i++)
{
if (ch[i] == c)
has = ;
}
return has;
} char getRe(char c)
{
for (int i = ; i < ; i++)
{
if (ch[i] == c)
return chRe[i];
}
} int is_P(char* str, int n)
{
int i;
for (i = ; i < n / ; i++)
{
if (str[i] != str[n - i - ])
return ;
}
return ;
} int is_M(char* str, int n)
{
if (n == )
{
if (hasRe(str[]))
{
return ;
}
else
{
return ;
}
} for (int i = ; i < n / ; i++)
{
if (hasRe(str[i]))
{
char t = getRe(str[i]);
if (t != str[n - i - ])
{
return ;
}
}
else
{
return ;
}
} if (n % != )
{
int x = n / ;
if (hasRe(str[x]))
{
return ;
}
else
{
return ;
}
} return ;
} int main(){
char str[];
while (gets(str) != NULL)
{
int len = strlen(str);
if (len == )
break;
int isMirro = , isPalin = ;
int i = ; isPalin = is_P(str, len);
isMirro = is_M(str, len); if (isPalin == ){
if (isMirro == )
printf("%s -- is not a palindrome.\n\n", str);
else
printf("%s -- is a mirrored string.\n\n", str);
}
else
{
if (isMirro == )
printf("%s -- is a regular palindrome.\n\n", str);
else
printf("%s -- is a mirrored palindrome.\n\n", str);
} }
}