Uva--401 Palindromes

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z16160102042/article/details/83117539

题目

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);
	}

}

猜你喜欢

转载自blog.csdn.net/z16160102042/article/details/83117539