401 - Palindromes

Posted on 2011-09-04 23:39 kalvinyZhang 阅读(100) 评论(0)  编辑 收藏 引用 所属分类: OJ
  说实话有点郁闷,我不知道哪里出了问题到现在没有AC,但是基本的思想就放在那里   

  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.

 


CharacterReverseCharacterReverseCharacterReverse
AAMMYY
BNZ5
COO11
DP2S
E3Q3E
FR4
GS25Z
HHTT6
IIUU7
JLVV88
KWW9
LJXX

 

 


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.

 


STRINGCRITERIA
" -- 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. 
#include <stdio.h>
palindrome的意思就是正读反过来读都是一样的,mirrored的情况就列在上面的表上。下面是代码

 

#include <string.h>

const char one[] = "AEHIJLMOSTUVWXYZ12357";
const char two[] = "A3HILJMO2TUVWXY51SEZ8";
const long len = 35;

bool Palindrome(const char *s)
{
    
long begin = 0, end = strlen(s)-1;
    
while (begin <= end) {
        
if (s[begin] == s[end]) {
            begin
++;
            end
--;
        }
        
else
            
return false;
    }
    
return true;
}
long pos(char ch) 
{
    
for (long i = 0; i < len; i++)
        
if (one[i] == ch)
            
return i;
    
return len;
}
bool Mirror(const char *s)
{
    
long begin = 0, end = strlen(s)-1;
    
while (begin <= end) {
        
long tmp = pos(s[begin]);
        
if (s[end] == two[tmp]) {
            begin
++;
            end
--;
        }
        
else
            
return false;
    }
    
return true;
}
int main()
{
    
char str[1000];
    
while (scanf("%s", str) != EOF) {
        
bool a = false, b = false;
        a 
= Palindrome(str);
        b 
= Mirror(str);
        
if (a && b)
            printf(
"%s -- is a mirrored palindrome.\n", str);
        
else if (a)
            printf(
"%s -- is a regular palindrome.\n", str);
        
else if (b)
            printf(
"%s -- is a mirrored string.\n", str);
        
else
            printf(
"%s -- is not a palindrome.\n", str);
        putchar(
'\n');
    }
    
return 0;
}
是用了CPP写的代码,应该是C风格的CPP,因为我是先看了部分CPP最后不爽转到了C嘛。。。基本的思想是这样的,首先,第一个函数Palindrome,两个参数,分别正反读取字符串,这个思想以后可以常用。然后Mirroed和pos函数之间的关系是这样的,pos函数主要是确定字符串中的字符在列表中的位置,然后拿去Mirroed中和第二个数组比较,最终输出结果,我不知道这个程序哪里出了问题ing,慢慢学习,也许几个星期之后。。。我能用纯C直接写出来了。。。

posts - 0, comments - 0, trackbacks - 0, articles - 9

Copyright © kalvinyZhang