Loveletter代码问题:詹姆斯找到了他的朋友哈利为他的女朋友写的情书。詹姆斯是一个恶作剧者,所以他决定干涉这封信。他把信中的所有单词都改成了回文。
为此,他遵循两条规则:
(a)他可以减少一封信的价值,例如:他可以改变' d'到了'但他不能改变' c'到了'。 (b)为了形成一个回文,如果他必须反复减少一个字母的价值,他可以这样做,直到这封信成为“a”。一旦信件改为' a',就不能再更改了。
任何字母值的每次减少都算作一次操作。找到将给定字符串转换为回文所需的最小操作数。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
static int getAscii(char letter)
{
return (int)letter;
}
static boolean checkPalindrome(String str, int strLen)
{
String revStr ="";
for(int i=strLen;i >= 0;i--)
{
revStr = revStr + str.charAt(i);
}
if(str.equals(revStr))
{
return true;
}
return false;
}
static void reduceAscii(String str)
{
StringBuilder sb = new StringBuilder(str);
str = str.toString();
int strLen = str.length()-1;
int numOfOperations=0;
char letter;
for(int j=strLen; j >= 0; j--)
{
letter = str.charAt(j);
while(getAscii(letter) > 97 && getAscii(letter) <= 122)
{
if(checkPalindrome(str, strLen))
{
break;
}
else
{
int asciiValofLetter = (char)getAscii(letter);
char charToreplace = (char)(asciiValofLetter-1);
sb.setCharAt(j, charToreplace);
str = sb.toString();
letter = charToreplace;
numOfOperations++;
}
}
}
System.out.println(numOfOperations);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfTestCases;
String str;
numOfTestCases = sc.nextInt();
for(int i=0; i < numOfTestCases; i++)
{
str = sc.next();
Solution.reduceAscii(str);
}
}
}
我的代码传递给abc,abcd,abcba,但很多测试用例都失败了。以下是一些:
pue
heubsbn
feazhaxpux
hmhcy
tmp
答案 0 :(得分:2)
您的解决方案看起来过于复杂。您被要求查找所需的操作数,而不是实际结果。
Java不是我的事,所以这里有一些伪代码:
Number of letters to change = int(length of string/2) (for a seven letter string we change three letters)
Total operations = 0
for n=0 to number of letters to change
take nth letter and (string length-n)th letter
(number of operations on each letter is difference of ASCII values so...)
subtract ASCII value of first letter from ASCII value of second
add absolute value of this to your total
next letter
print total.
答案 1 :(得分:0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.InputMismatchException;
public class ConvertToPal {
public ConvertToPal() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter a string to be palindromized! ");
String str = br.readLine();
char[] strArr = str.toCharArray();
int size = strArr.length;
int numOfOperations = 0;
for (int i = 0; i < (size/2) ; i++){
int dif = (getAscii(strArr[i])-getAscii(strArr[size-1-i]));
if (dif > 0){
strArr[i] = strArr[size-1-i];
numOfOperations += Math.abs(dif);
}
if(dif < 0){
strArr[size-1-i] = strArr[i];
numOfOperations += Math.abs(dif);
}
}
System.out.println(new String(strArr));
System.out.println("num of ops: "+ numOfOperations);
}
static int getAscii(char letter){
return (int)letter;
}
}
答案 2 :(得分:0)
这是Java中用于实现此目的的方法
public int palindromeCount(String s) {
int count = 0;
int i = 0, j = s.length() - 1;
while (i < j) {
int front = (int) s.charAt(i);
int back = (int) s.charAt(j);
count += (Math.abs(front - back));
i++;
j--;
}
return count;
}