1. 程式人生 > >Uva--401 Palindromes

Uva--401 Palindromes

題目

A regular palindrome is a string of numbers or letters that is the same forward as backward. Forexample, the string “ABCDEDCBA” is a palindrome because it is the same when the string is read fromleft 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 itsreverse (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.

在這裡插入圖片描述
Note that ‘0’ (zero) and ‘O’ (the letter) are considered the same character and therefore ONLY theletter ‘O’ 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 exactlyone of the following strings.
在這裡插入圖片描述
Note that the output line is to include the ‘-’s and spacing exactly as shown in the table above anddemonstrated 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.

題意:輸入一個字串,判斷它是否為迴文串。輸入字串保證不包含數字0.所謂迴文串,就是反轉之後和原串相同,如abba和madam。所有映象串,就是左右映象之後和原串相同,如2S和3AIAE。但是並不是每個字元在映象之後都能得到一個合法字元。
解題思路:建立一個映象字串陣列,先判斷該字串是否為迴文串,然後通過映象字串找到該字串中的字元的映象字元,再判斷其是否為迴文串。

Java-Code

import java.util.Scanner;

public class Uva401 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String[] str = {" -- is not a palindrome."," -- is a regular palindrome.",
				" -- is a mirrored string."," -- is a mirrored palindrome."};
		while(sc.hasNext())
		{
			String s = sc.next();
			int p =1;
			int m =2;
			int n = s.length();
			for(int i = 0;i<n/2;i++)
			{
				if(s.charAt(i)!=s.charAt(n-1-i))
				{
					p=0;
				}
				if(Mirror(s.charAt(i)) != s.charAt(n-1-i))
				{
					m=0;
				}
			}
			System.out.println(s+str[p+m]);
			System.out.println();
		}
		sc.close();
	}
	public static char Mirror(char ch) {
		String Mirr ="A   3  HIL JM O   2TUVWXY51SE Z  8 ";
		if(Character.isUpperCase(ch))
		{
			System.out.println(Mirr.charAt(ch-'A'));
			return Mirr.charAt(ch-'A');
		}
		System.out.println(Mirr.charAt(ch-'0'+25));
		return Mirr.charAt(ch-'0'+25);
	}

}