因为痛,所以叫青春

我有一个梦想:穷屌丝变身富屌丝
401 - Palindromes 解题报告

其实这道题目还是比较水的啦,刚看这道题目的时候,我有些感冒,正头痛着,所以看上去满世界英文,一看就蒙了,不过做ACM这一行的,要时刻把心思集中在这上面,

我就充分的利用了,上下课的时间去想这道题目,很快就想出来了很不错的算法哦,顿时有种豁然开朗的感觉,这几天看的书真是没白看,学会了好多思想。下面是代码

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #define Lenth 2010
 4 //下面定义两个不可改变的字符串,用来查找题目中所提到的镜像字符所对应的字符
 5 const char ST[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
 6 const char _ST[]="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
 7 //这个就是很简单的判断是不是会文的,函数,之所以单独写一个函数,是因为为了程序更清晰
 8 int palindrome(char st[])
 9 {
10     int i, len = strlen(st);
11         for(i = 0;i <= len/2; i++)
12         if(st[i] != st[len-1-i])
13         return 0;
14     return 1;
15 }
16 //这个函数就是判断镜像的函数了
17 int mirrored(char st[])
18 {
19     int i, len = strlen(st), flag, j;
20     //在这里多定义一个字符数组,作用是非常强大的,不用我说,聪明的读者看了自然会懂
21     char s[Lenth];
22     strcpy(s,st);
23     for(i = 0;i <= len/2; i++)
24     {
25         flag = 1;
26         for(j = 0;j < 37; j++)
27         {
28             if(st[i] == ST[j])
29             {
30                 s[len-1-i] = _ST[j];
31                 flag = 0;
32                 break;
33             }
34         }
35         if(flag)
36         break;
37     }
38     if((strcmp(st,s) == 0) && (flag != 1))
39     return 1;
40     else
41     return 0;
42 }
43 //有了前两个函数,在主函数里面只需要做一些输入输出就可以了,看起来既简洁又干脆
44 int main()
45 {
46     char st[Lenth];
47     while(gets(st) != NULL)
48     {
49         if(mirrored(st) && palindrome(st))
50         printf("%s -- is a mirrored palindrome.\n",st);
51         else if(palindrome(st))
52         printf("%s -- is a regular palindrome.\n",st);
53         else if(mirrored(st))
54         printf("%s -- is a mirrored string.\n",st);
55         else
56         printf("%s -- is not a palindrome.\n",st);
57         printf("\n");
58     }
59     return 0;
60 }

posted on 2012-05-28 17:34  Nice!  阅读(178)  评论(0编辑  收藏  举报