首页 > ACM题库 > HDU-杭电 > HDU 1318 Palindromes-模拟[解题报告] C++
2013
12-09

HDU 1318 Palindromes-模拟[解题报告] C++

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 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>
#define MAX 25
char str[MAX];
char tmp[MAX];
char num[200];
int Ispalindrome(char *str)
{
    int len=strlen(str);
    char *p,*k;
    for(p=str,k=str+len-1;p<=k;p++,k--)
        if(*p!=*k) return 0;
    return 1;
}
int Ismirror(char *str)
{
    int len=strlen(str);    
    char *p,*k;
    strcpy(tmp,str);
    for(p=tmp,k=tmp+len-1;p<=k;)
    {
        if(*p=='0') *p='O';
        if(*k=='0') *k='O';
        if(num[*p]!=1&&num[*k]!=1&&num[*p]==*k)
        {
            p++;
            k--;
        }
        else return 0;
    }
    return 1;
}
int main()
{
    memset(num,1,sizeof(num));
    num['A']='A';
    num['E']='3';
    num['H']='H';
    num['I']='I';
    num['J']='L';
    num['L']='J';
    num['M']='M';
    num['O']='O';
    num['S']='2';
    num['T']='T';
    num['U']='U';
    num['V']='V';
    num['W']='W';
    num['X']='X';
    num['Y']='Y';
    num['Z']='5';
    num['1']='1';
    num['2']='S';
    num['3']='E';
    num['5']='Z';
    num['8']='8';
    while(~scanf("%s",str))
    {
        int flag1,flag2;
        flag1=Ispalindrome(str);
        flag2=Ismirror(str);
        if(!flag1&&!flag2)
            printf("%s -- is not a palindrome.\n\n",str);
        else if(flag1&&!flag2)
            printf("%s -- is a regular palindrome.\n\n",str);
        else if(!flag1&&flag2)
            printf("%s -- is a mirrored string.\n\n",str);
        else printf("%s -- is a mirrored palindrome.\n\n",str);
    }
    return 0;
}

解题报告转自:http://www.cnblogs.com/yuris115/archive/2013/09/18/3329217.html


  1. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。