Palindromes

Time Limit:3000MS     Memory Limit:0KB     64bit 
                                                                                        IO Format:%lld & %llu

Description

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

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.

STRING CRITERIA
" -- 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.

Hint

use the C++'s class of string will be convenient, but not a must

【思路】

回文 --即从左至右与从右至左一样,例如:abccab 、aaa、abcba.......

镜像字符串--字符串里的字符都属于镜像字符,且从左至右与字符镜像呈回文。例如:AEH3A(A-A E-3).....

建立两个方法判断回文与判断镜像字符串(见代码)。

回文部分读者自看。

判断镜像字符串,传人字符串,先判断字符串中是否有字符不属于镜像字符,如果有直接返回false,没有在进行下一步扫描

本代码中建立了对应的x1字符数组,存放对应的镜像。x数组存放镜像字符,x1对应x存放其镜像。

从头开始扫描,i从头,j从尾扫描。在x字符数组中扫描找到其c[i]字符在x数组对应的下标k(x1[k]为其镜像),然后判断c[j]与x[k]是否相等(即对应的尾部字符是否是其字符对应的镜像字符),若相等镜像匹配成功,依次扫描,扫描到最后返回true。

【小坑,其中所有的‘0’(零)用‘O’(哦)来代替,看清楚】

 

AC代码:

import java.util.Scanner;

public class Main {
public static boolean pali(String s) {
char[] c = s.toCharArray();
int i, j;
for (i = 0, j = c.length - 1; i < j; i++, j--) {
if (c[i] != c[j]) {
return false;
}
}
return true;
} public static boolean mirr(String s) {
char[] c = s.toCharArray();
int i, j;
char[] x = new char[] { 'A', 'E', 'H', 'I', 'J', 'L', 'M', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2',
'3', '5', '8', 'O' };
for (i = 0; i < c.length; i++) {
for (j = 0; j < x.length; j++) {
if (c[i] == x[j]) {
break;
}
}
if (j == x.length) {
return false;
}
}
int k = 0;
char[] x1 = new char[] { 'A', '3', 'H', 'I', 'L', 'J', 'M', '2', 'T', 'U', 'V', 'W', 'X', 'Y', '5', '1', 'S',
'E', 'Z', '8', 'O' };
for (i = 0, j = c.length - 1; i < j; i++, j--) {
for (k = 0; k < x.length; k++) {
if (c[i] == x[k]) {
if (c[j] != x1[k]) {
return false;
}
}
}
}
return true;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
char[] a = s.toCharArray();
for (int i = 0; i < a.length; i++) {
if (a[i] == '0')
a[i] = 'O';
}
String str = new String(a);
if (pali(str) && mirr(str)) {
System.out.println(str + " -- is a mirrored palindrome.");
} else if (pali(str)) {
System.out.println(str + " -- is a regular palindrome.");
} else if (mirr(str)) {
System.out.println(str + " -- is a mirrored string.");
} else
System.out.println(str + " -- is not a palindrome.");
System.out.println();
}
}
}

  

hdu 1318 Palindromes的更多相关文章

  1. hdu 1318 Palindromes(回文词)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1318 题意分析:输入每行包含一个字符串,判断此串是否为回文串或镜像串. 表面上看这道题有些复杂,如果能 ...

  2. HDU 2163 Palindromes

    http://acm.hdu.edu.cn/showproblem.php?pid=2163 Problem Description Write a program to determine whet ...

  3. HDOJ/HDU 2163 Palindromes(判断回文串~)

    Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a s ...

  4. HDU 1544 Palindromes(回文子串)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...

  5. HDU 2029 Palindromes _easy version

    http://acm.hdu.edu.cn/showproblem.php?pid=2029 Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“ ...

  6. Palindromes

    http://acm.hdu.edu.cn/showproblem.php?pid=1318 Palindromes Time Limit: 2000/1000 MS (Java/Others)    ...

  7. hdu 3948 The Number of Palindromes

    The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (J ...

  8. HDU - 5340 Three Palindromes(manacher算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...

  9. HDU 5340——Three Palindromes——————【manacher处理回文串】

    Three Palindromes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. 【转】深入浅出Java三大框架SSH与MVC的设计模式

    原文链接:http://www.cnblogs.com/itao/archive/2011/08/22/2148844.html 在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架: ...

  2. ES6新特性:Javascript中Set和WeakSet类型的数据结构

    ES6提供了新的数据结构Set,Set对象不是数组, 可以用来保存对象或者基本类型, 所有保存的值都是唯一的, chrome浏览器>38和FF>13,以及nodeJS,对Set支持良好, ...

  3. 一个最简单的ftpsever

    没有什么事情可以做,无聊的很 写个最简单的ftp吧---说白了就是一个简单的文件上传.QAQ 思路:client --读取文件的一行 然后发到server端 然后server 读取 写入文件的一行 先 ...

  4. DTD中的属性类型

    <![CDATA[文本内容]]> DTD中的属性类型 全名:character data 在标记CDATA下,所有的标记.实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待, ...

  5. Java追加文件内容的三种方法

    import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io. ...

  6. jquery的ajax提交form表单

    $.ajax({ cache: true, type: "POST", url:ajaxCallUrl, data:$('#yourformid').serialize(),// ...

  7. EventBus完全解析--组件/线程间通信利器

    github地址:https://github.com/greenrobot/EventBus 1, Android EventBus实战, 没听过你就out了 2,  Android EventBu ...

  8. redis 可视化工具

    Redis Desktop Manager 下载 phpRedisAdmin 是一个用php管理redis的工具 下载

  9. Java并发和多线程(一)基础知识

    1.java线程状态 Java中的线程可以处于下列状态之一: NEW: 至今尚未启动的线程处于这种状态. RUNNABLE: 正在 Java 虚拟机中执行的线程处于这种状态. BLOCKED: 受阻塞 ...

  10. Leetcode 306. Additive Number

    Additive number is a string whose digits can form additive sequence. A valid additive sequence shoul ...