uva 401 Palindromes 解题报告

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342

题目意思:判断一行字符串为以下四种的哪一种:A regular palindrome,A mirrored string,A mirrored palindrome 和 is not a palindrome。A regular palindrome 就是我们见得最多的普通回文字符串,正读和反读是一样的;A mirrored string 就是根据以下的一套规则,将每个character变成对应的reverse,变完之后反读起来是和原来的字符串是一样的。

  

比较坑爹的一个地方是,对于B、C、D、F、G、K...6、7 这些没有reverse的字符,如果字符串里有这些字符,就绝对不是mirrored string了。还有就是记得,输出每一行结果之后,还要追加一个空行!

    

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 1000 + 10;
 8 char s[maxn], rev[maxn];
 9 int len, l;
10 
11 char test[] = {'B', 'C', 'D', 'F', 'G', 'K', 'N', 'P', 'Q', 'R', '4', '6', '7', '9'};     // 非 mirrored 的 字符
12 char match[] =  {'E', 'J', 'L', 'S', 'Z', '2', '3', '5'};
13 char match1[] = {'3', 'L', 'J', '2', '5', 'S', 'E', 'Z'};
14 
15 bool Is_palindrome()
16 {
17     for (int i = 0; i <= len/2; i++)
18     {
19         if (s[i] != s[len-i-1])
20             return false;
21     }
22     return true;
23 }
24 
25 bool check()
26 {
27     for (int j = 0; j < len; j++)
28     {
29         for (int i = 0; i <= 13; i++)
30         {
31             if (s[j] == test[i])  // 找到一个非mirrored 的 字母,就不可能是mirrored string了
32                 return false;
33         }
34     }
35     return true;
36 }
37 
38 bool Is_mirrored()
39 {
40     int f;
41     l = 0;
42     for (int i = 0; i < len; i++)
43     {
44         f = 0;
45         for (int j = 0; j < 8; j++)
46         {
47             if (s[i] == match[j])
48             {
49                 rev[l++] = match1[j];  // 替换该字符的reverse
50                 f = 1;
51             }
52         }
53         if (!f)    // reverse 与 它本身一样,例如A、I
54             rev[l++] = s[i];
55     }
56     for (int j = l-1; j >= 0; j--)
57         if (rev[j] != s[l-j-1])
58             return false;
59     return true;
60 }
61 
62 int main()
63 {
64     while (scanf("%s", s) != EOF)
65     {
66         len = strlen(s);
67       if (Is_palindrome())
68       {
69           if (check() && Is_mirrored())
70               printf("%s -- is a mirrored palindrome.

", s);
71           else
72               printf("%s -- is a regular palindrome.

", s);
73       }
74       else
75       {
76           if (check() && Is_mirrored())
77               printf("%s -- is a mirrored string.

", s);
78           else
79               printf("%s -- is not a palindrome.

", s);
80       }
81     }
82     return 0;
83 }
原文地址:https://www.cnblogs.com/windysai/p/3632905.html