hdu 1318 Palindromes
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的更多相关文章
- hdu 1318 Palindromes(回文词)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1318 题意分析:输入每行包含一个字符串,判断此串是否为回文串或镜像串. 表面上看这道题有些复杂,如果能 ...
- HDU 2163 Palindromes
http://acm.hdu.edu.cn/showproblem.php?pid=2163 Problem Description Write a program to determine whet ...
- HDOJ/HDU 2163 Palindromes(判断回文串~)
Problem Description Write a program to determine whether a word is a palindrome. A palindrome is a s ...
- HDU 1544 Palindromes(回文子串)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1544 问题分析: 问题要求求出字符串的连续子串中的回文子串个数.首先,需要区分连续子串与子序列的区别. ...
- HDU 2029 Palindromes _easy version
http://acm.hdu.edu.cn/showproblem.php?pid=2029 Problem Description “回文串”是一个正读和反读都一样的字符串,比如“level”或者“ ...
- Palindromes
http://acm.hdu.edu.cn/showproblem.php?pid=1318 Palindromes Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3948 The Number of Palindromes
The Number of Palindromes Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (J ...
- HDU - 5340 Three Palindromes(manacher算法)
http://acm.hdu.edu.cn/showproblem.php?pid=5340 题意 判断是否能将字符串S分成三段非空回文串 分析 manacher预处理出前缀和后缀回文的位置, 枚举第 ...
- HDU 5340——Three Palindromes——————【manacher处理回文串】
Three Palindromes Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
随机推荐
- http强制跳转到https
原文地址:http://m.blog.csdn.net/article/details?id=8549290 需求简介 基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.com, ...
- 【BZOJ 2555】SubString
http://www.lydsy.com/JudgeOnline/problem.php?id=2555 一个字符串在原串中的出现次数就是这个字符串对应后缀自动机上的状态的\(|Right|\),要求 ...
- poj1743 后缀数组求不可重叠的重复出现的子串最长长度
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 25348 Accepted: 8546 De ...
- Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析
参考:Android系统Surface机制的SurfaceFlinger服务渲染应用程序UI的过程分析 一句话概括一下Android应用程序显示的过程:Android应用程序调用SurfaceFlin ...
- Thinking in java学习笔记之finalize
finalize:一旦垃圾回收器准备好释放对象占用的存储空间,将首先调用其finalize()方法,并且在下次垃圾回收动作发生时,才会真正回收对象占用的内存,所以可用此作为对象终结条件的验证.注意的三 ...
- 解决bind错误 bind: Address already in use
关于bind错误的处理: bind: Address already in use 原因: 操作系统没有立即释放端口 解决一: 等待一段时间运行网络程序即可 解决二:通过setsockopt进行设置, ...
- 【BZOJ-3337】ORZJRY I 块状链表
3337: ORZJRY I Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 190 Solved: 50[Submit][Status][Discu ...
- 【BZOJ-2440】完全平方数 容斥原理 + 线性筛莫比乌斯反演函数 + 二分判定
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2371 Solved: 1143[Submit][Sta ...
- SSD硬盘的4K对齐
4K对应4096 硬盘模式: 一.让SSD运行在AHCI模式下: AHCI,全称Advanced Host Controller Interface,即高级主机控制器接口,相比老旧的“IDE“ 虚拟模 ...
- Zend Guard Loader/Zend Loader是干什么的
Zend Guard Loader 是加速php的,能提高30%—40%速度.PHP 5.3.X 开始 Zend Optimizer 正式被 Zend Guard Loader 取代.在PHP 5.5 ...