专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C语言

回文字符,该怎么解决

发布时间:2011-06-28 10:30:16 文章来源:www.iduyao.cn 采编人员:星星草
回文字符
RESULT:WRONG ANSWER

Description
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.
Hint
use the C++'s class of string will be convenient, but not a must

/*

#include <stdio.h>
#include <string.h>

char str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char rev[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";
char* msg[] = {"not a palindrome", "a regular palindrome", "a mirrored string", "a mirrored palindrome"};

int IsMirror(char input[], int len)
{
    int flag = 1, i, j;
    int n = strlen(str);
    for(j = 0; j < len; j ++)
        {
            for(i = 0; i < n; i ++)
                if(str[i] == input[j])
                    break;
            if(rev[i] == ' ')
                flag = 0;
        }
    if(flag == 0)
        return 0;
    else return 1;
}

int IsPalindrome(char input[], int len)
{
    int flag = 1;
    for(int i = 0; i < len; i ++)
        if(input[i] != input[len-1-i])
            flag = 0;
    if(flag == 0)
        return 0;
    else return 1;
}

int main()
{

    char input[50];
    int i, len, m, n;

    while(scanf("%s", input) != EOF)
    {
        m = n = 0;
        len = strlen(input);
        m = IsMirror(input, len);
        n = IsPalindrome(input, len);
        printf("%s -- is %s.\n\n", input, msg[m*2+n]);
    }
    return 0;
}

------解决思路----------------------
引用:
Quote: 引用:

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
受教了,谢谢。可是代码运行的结果是正确的,可是提交后 是WA,实在是有点头大,才发帖的


WA一般是边界条件没有考虑好,导致一些边界上的测试数据过不了
------解决思路----------------------
引用:
Quote: 引用:

代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。
提醒:再牛×的老师也无法代替学生自己领悟和上厕所!
单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
受教了,谢谢。可是代码运行的结果是正确的,可是提交后 是WA,实在是有点头大,才发帖的


等等你确定你的输出格式对吗。。我怎么觉得最后多打了个换行?
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: