Palindromes(UVA401)

3:Palindromes

  • 查看
  • 提交
  • 统计
总时间限制: 
1000ms 
内存限制: 
65536kB
描述
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 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.
输出
For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings. 

" -- 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. 
样例输入
NOTAPALINDROME 
ISAPALINILAPASI 
2A3MEAS 
ATOYOTA
样例输出
NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.
  • 查看 
  • 提交 
  • 统计

















































































#include <stdio.h>
#include <string.h>
char mirword1[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char mirword2[]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
char mirword[]="BCDFGKNPQR4679";

int judepal(char *);
int judemir(char *);

int main ()
{
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);

    char str[1000];

    int judep, judem;
    while(scanf("%s", str)!=EOF)
    {
        judep=judem=1;
        judep=judepal(str);
        judem=judemir(str);
        if(judep&&judem)
            printf("%s -- is a mirrored palindrome.\n\n",str);
        else if(judep==1&&judem==0)
            printf("%s -- is a regular palindrome.\n\n",str);
        else if(judep==0&&judem==1)
            printf("%s -- is a mirrored string.\n\n",str);
        else
            printf("%s -- is not a palindrome.\n\n",str);
            //printf("%d\n", judem);
    }

    return 0;
}
//函数功能:判断是否为回文数
int judepal(char *str)
{
    int i, j;
    int len=strlen(str);
    for(i=0, j=len-1;i<j;i++,j--)
    {
        if(str[i]!=str[j])
            return 0;
    }
    return 1;
}
//函数功能:判断是否为镜像串
int judemir(char *str)
{
    int i, j;
    int len=strlen(str);
   <span style="background-color: rgb(102, 0, 204);"> for(i=0;i<len;i++)
        if(strchr(mirword,str[i])!=NULL)
            return 0;</span>
   <span style="color:#ff0000;"> for(i=0, j=len-1;i<j;i++,j--)
    {
        if(str[j]!=*(strchr(mirword1, str[i])-mirword1+mirword2))//巧妙地运用了地址
                return 0;
    }</span>
    return 1;
}

备注:这道题目输入框架以及回文数的判断不用多说。重点是镜像串的判断

怎么来处理呢?这里受到了函数定义的启发。把原字母看成一个集合,把其对应的镜像看成另一个集合,

集合用数组来模拟,映射关系用数组地址来解决。这题很值得注意的是蓝色部分,第一次提交WA了,

我百思不得其解,想不明白怎么会错。后来我在UVA上debug时现输入AGA,C等等之类时我的答案是

镜像串,我忽然明白了,原来镜像串中一个也不能含有没有镜像的字符。处理方法见蓝色部分。这道题思

路很简单,不像一些水题一样完全没有头绪,但是在将思路用代码来表现的时候因为编程能力不够而实现不

了自己的思路,这点是很悲剧的,还是要多练才可以。有趣的是同样的题目在POJ新站上我第一次的代码竟 然AC了。。。


你可能感兴趣的:(字符串,ACM,uva)