首页 > 数据结构 > Hash表 > POJ 1590 Palindromes [解题报告] Java
2013
11-09

POJ 1590 Palindromes [解题报告] Java

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.

解题代码:

//* @author: Yeming Hu"[email protected]"
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.io.BufferedInputStream;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
	Map map = new HashMap();
	map.put('A','A');
	map.put('E','3');
	map.put('H','H');
	map.put('I','I');
	map.put('J','L');
	map.put('L','J');
	map.put('M','M');
	map.put('O','O');
	map.put('S','2');
	map.put('T','T');
	map.put('U','U');
	map.put('V','V');
	map.put('W','W');
	map.put('X','X');
	map.put('Y','Y');
	map.put('Z','5');
	map.put('1','1');
	map.put('2','S');
	map.put('3','E');
	map.put('5','Z');
	map.put('8','8');
        while(sc.hasNext())
	{
	    String original = sc.next();
	    boolean isPalindrome = true;
	    boolean isMirrored = true;
	    int len = original.length();
	    for(int i = 0; i < len/2; i++)
	    {
	        if(original.charAt(i) != original.charAt(len - i - 1))
		{
		    isPalindrome = false;
		    break;
		}
	    }
	    for(int i = 0; i <= len/2; i++)
	    {
	        Character mirror = map.get(map.get(original.charAt(len - i -1)));
	        if(mirror == null || original.charAt(i) != map.get(mirror).charValue())
		{
		    isMirrored =false;
		    break;
		}
	    }
	    System.out.print(original + " -- is ");
	    if(isPalindrome && isMirrored)
	    {
	        System.out.println("a mirrored palindrome.");
	    }else if(isPalindrome)
	    {
	        System.out.println("a regular palindrome.");
	    }else if(isMirrored)
	    {
	        System.out.println("a mirrored string.");
	    }else
	    {
	        System.out.println("not a palindrome.");
	    }
	    System.out.println();
	}
    }
}

  1. 站长好。我是一个准备创业的互联网小白,我们打算做一个有关国*际*游*学的平台。手上也有了一些境外资源。现阶段的团队现在没有cto.原意出让一些管理股寻找一个靠谱的技术专家做合伙人, 不知道是不是能得到您的帮助。发个帖子或者其他方式。期待您的回应。可以加我微信tianxielemon聊聊。

  2. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;