import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader stdin;
String str;
// TODO Auto-generated method stub
stdin = new BufferedReader(new InputStreamReader(System.in));
char[] stringArray ;
boolean isPalindrone, isMirrored;
while((str = stdin.readLine())!= null) {
//convert string into array using toCharArray() method of string class
stringArray = str.toCharArray();
isPalindrone = new Main().checkPalindromes(stringArray);
isMirrored = new Main().precheckMirroring(stringArray);
if(isPalindrone && isMirrored) {
System.out.println(str + " -- is a mirrored palindrome.");
} else if(isPalindrone) {
System.out.println(str + " -- is a regular palindrome.");
} else if(!isPalindrone && isMirrored) {
System.out.println(str + " -- is a mirrored string.");
} else {
System.out.println(str + " -- is not a palindrome.");
}
System.out.println();
}
}
/* precheckpalindronetest */
public boolean precheckMirroring(char stringArray[]) {
boolean pretest_ismirror = true;
char[] m_char = {'A' , 'E' , 'H', 'I' , 'J', 'L','M', 'O' ,'T','U' ,
'V','W' , 'X' , 'Y', 'Z' , '1', '2','3' , '5' , '8'
};
boolean match = false;
for(int i = 0 ; i < stringArray.length; ++i) {
match = false;
for(int j = 0 ; j < m_char.length; ++j) {
if(stringArray[i] == m_char[j]) {
match = true;
break;
}
}
if(match == false) {
pretest_ismirror = false;
break;
}
}
return pretest_ismirror;
}
/* check palindromes */
public boolean checkPalindromes(char stringArray[]) {
int i = 0 ;
int j = stringArray.length - 1;
boolean check_palindrone = true;
while(i <= j) {
if(stringArray[i] != stringArray[j]){
check_palindrone = false;
break;
}
++i;
--j;
}
return check_palindrone;
}
/* check mirrored */
public boolean checkMirroredString(String str) {
if(this.precheckMirroring(str.toCharArray()) == false) {
return false;
}
StringBuffer strbuf = new StringBuffer(str);
int inc = 0;
Map<String, String> map = new HashMap<String, String>();
map.put("A", "A");
map.put("E", "3");
map.put("H", "H");
map.put("I", "I");
map.put("J", "L");
map.put("L", "J");
map.put("M", "M");
map.put("O", "O");
map.put("T", "T");
map.put("U", "U");
map.put("V", "V");
map.put("W", "W");
map.put("X", "X");
map.put("Y", "Y");
map.put("Z", "5");
map.put("1", "1");
map.put("2", "S");
map.put("3", "E");
map.put("5", "Z");
map.put("8", "8");
int i = 0 ;
int j = strbuf.length() - 1;
while(i < j) {
String left = Character.toString(strbuf.charAt(i));
String right = Character.toString(strbuf.charAt(j));
// If the character is reverse to each other
if(map.get(left) != right || map.get(right) != left ){
return false;
}
if( left != right) {
return false;
}
++i;
--j;
}
return true;
}
}
在线评委给出了错误的答案。
答案 0 :(得分:2)
可能
isMirrored = new Main().precheckMirroring(stringArray);
应该是
isMirrored = new Main().checkMirroredString(stringArray);
此外 - 您无法使用==
可靠地比较字符串,您应该使用equals
。
另外,在checkMirroredString
你可能不想要:
if( left != right) {
return false;
}
答案 1 :(得分:0)
这是我的解决方案,被在线评委接受。问题陈述的pdf版本是错误的,这简直是一种耻辱。这是链接:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342
非常感谢所有对我的查询做出回应的人。
import java.io.*;
import java.util.*;
public class Main{
/**
* @param args
*/
public Map<String, String> map;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader stdin;
String str;
// Read line
stdin = new BufferedReader(new InputStreamReader(System.in));
char[] stringArray ;
boolean isPalindrone , isMirrored;
while((str = stdin.readLine())!= null) {
// check palindrome
isPalindrone = new Main().checkPalindromes(str);
// check mirroring
isMirrored = new Main().checkMirrorString(str);
if( isPalindrone ) {
if(isMirrored) {
System.out.println(str + " -- is a mirrored palindrome.");
} else {
System.out.println(str + " -- is a regular palindrome.");
}
}
else {
if(isMirrored) {
System.out.println(str + " -- is a mirrored string.");
}
else {
System.out.println(str + " -- is not a palindrome.");
}
}
System.out.println();
}
}
/* Check palindromes */
public boolean checkPalindromes(String str) {
char stringArray[] = str.toCharArray();
// Always return true if length is 1
if(stringArray.length == 1) return true;
int i = 0 ;
int j = stringArray.length - 1;
boolean check_palindrone = true;
while(i <= j) {
if(stringArray[i] != stringArray[j]){
check_palindrone = false;
break;
}
++i;
--j;
}
return check_palindrone;
}
/* Load mirrow table */
public void initMirrorTable() {
// Put mirror characters to hashmap
map = new HashMap<String, String>();
map.put("A", "A");
map.put("E", "3");
map.put("H", "H");
map.put("I", "I");
map.put("J", "L");
map.put("L", "J");
map.put("M", "M");
map.put("O", "O");
map.put("S", "2");
map.put("T", "T");
map.put("U", "U");
map.put("V", "V");
map.put("W", "W");
map.put("X", "X");
map.put("Y", "Y");
map.put("Z", "5");
map.put("1", "1");
map.put("2", "S");
map.put("3", "E");
map.put("5", "Z");
map.put("8", "8");
}
/* Check mirror string */
public boolean checkMirrorString(String str) {
// convert it to character array
char tochar[] = str.toCharArray();
// Init mirror table
this.initMirrorTable();
boolean is_mirrorstr = false;
// Replace each character with it's mirror character
for(int i = 0 ; i < tochar.length ; i++) {
// Get the reverse character
String temp_str = map.get(Character.toString(tochar[i]));
// if Not found then return false
if( temp_str == null ) { return false; }
char reverse_char = temp_str.charAt(0);
tochar[i] = reverse_char;
}
String rev = String.valueOf(tochar);
StringBuffer mirror = new StringBuffer(rev).reverse();
if(str.equals(mirror.toString())) is_mirrorstr = true;
return is_mirrorstr;
}
}