hdu 1318 Palindromes

时间:2023-01-29 08:18:48

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&lpar;回文词&rpar;

    题目链接: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&sol;HDU 2163 Palindromes&lpar;判断回文串~&rpar;

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

  4. HDU 1544 Palindromes&lpar;回文子串&rpar;

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

  5. HDU 2029 Palindromes &lowbar;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. &lbrack;转载&rsqb;js中return的用法

    一.返回控制与函数结果,语法为:return 表达式; 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果 二.返回控制,无函数结果,语法为:return;  在大多数情况下,为事件处理函 ...

  2. MVC区域 视图必须派生自 WebViewPage 或 WebViewPage&lt&semi;TModel&gt&semi;

    http://blog.csdn.net/iack_ji/article/details/16965885 今天在学习 mvc区域时,将区域控制器类 外迁到其他的程序集的练习中出现了"视图必 ...

  3. (ACM)C&plus;&plus; STL 训练(第一天)

    因为老师说ACM考的是纯C++,所以打算抛弃VS的VC++不用了,针对纯C++的编译器有Intel Compiler(不过要钱),MinGw(个人用的),当然还有微软的VC++ 编译器,IDE你们可以 ...

  4. jquery-ui 之draggable详解

    举一个例子: <div class="box"> <div id="draggable"> <p>Drag me aroun ...

  5. SAX,DOM,JAXP,JDOM,DOM4J比较

    dom,sax,jdom,dom4j的技术特点: 1: DOMDOM 是用与平台和语言无关的方式表示 XML 文档的官方 W3C 标准.DOM 是以层次结构组织的节点或信息片断的集合.这个层次结构允许 ...

  6. jquery判断对象的type

    利用Object.toString.call()方法 看代码 先初始化class2type,将每种对象的toString后的值和type建立对应关系 core_toString.call([])输出& ...

  7. if()&lbrace;&rcub;使用

    1.当作 if else时使用 是判断if()括号内的内容和给定内容是不是相同 package cn.lyun.thread; class Demo{ boolean flag = false; pu ...

  8. oracle查询查询出某字段为空后前台不显示的小测试1

    1.nvl(,''),后台会打印null,前台不显示 2不处理,后台显示null,前台不显示 3.nvl(,' '),后台显示" ",前台显示“ ”

  9. ArcGIS中的数据连接问题——数据类型不统一

    博主在研究空间数据分布的时候经常会用到 ArcGIS 进行空间数据可视化.但是有时候会由于数据类型不统一而无法将 csv 中的数据连接到底图上.比如在底图中的数据是字符串格式,而 csv 中是数字格式 ...

  10. echarts地图定时切换散点及多图表级联联动

    本文目录 1. 摘要 2.引入ECharts以及地图相关json 3. 界面布局 4. js实现图形布局 5.定时循环jquery实现 6. 总结 1.  摘要 最近做项目遇到个统计相关需求,一个页面 ...