time limit exceeds 错误 in spoj next palindrome
Time limit exceeds error in spoj next palindrome
我正在尝试解决 SPOJ 中的下一个回文问题。我在下面的 Java 代码中收到超出时间限制的错误。
"A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros."
import java.math.BigInteger;
import java.util.Scanner;
public class Nextpalindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in =new Scanner(System.in);
int t=in.nextInt();
for (int i=1;i<=t;i++)
{
BigInteger bi = in.nextBigInteger();
String str=bi.toString();
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
int l=str.length();
int comp=0;
if (l==2)
{
str1=str.substring(0,1);
str2=str.substring(1,2);
if (Integer.parseInt(str1)>Integer.parseInt(str2))
str1=str1.concat(str1);
else if (Integer.parseInt(str2)>Integer.parseInt(str1))
{
str2=str2.concat(str2);
str1=str2;
}
else if (Integer.parseInt(str1)==Integer.parseInt(str2))
{
int x=Integer.parseInt(str1)+1;
str1=Integer.toString(x);
str1=str1.concat(str1);
}
}
if (l%2>0)
{
str1=str.substring(0,l/2);
str2=str.substring((l/2)+1,l);
str3 =str.substring(l/2,(l/2)+1);
str4=new StringBuffer(str1).reverse().toString();
BigInteger bi1 = new BigInteger(str1);
BigInteger bi2 = new BigInteger(str2);
comp= bi1.compareTo(bi2);
int mid=Integer.parseInt(str3);
if (comp==-1)
{
mid+=1;
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
else if (comp==1)
{
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
else if (comp==0)
{
mid+=1;
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
}
if ((l>2)&&(l%2==0))
{
str1=str.substring(0,l/2);
str2=str.substring(l/2,l);
BigInteger bi1 = new BigInteger(str1);
BigInteger bi2 = new BigInteger(str2);
BigInteger bi3=new BigInteger("1");
comp= bi1.compareTo(bi2);
if (comp==-1)
{
bi1=bi1.add(bi3);
str1=bi1.toString();
str4=new StringBuffer(str1).reverse().toString();
str1=str1.concat(str4);
}
else if ((comp==1)||(comp==0))
{
str4=new StringBuffer(str1).reverse().toString();
str1=str1.concat(str4);
}
}
System.out.println(str1);
}
in.close();
}
}
既然您想找到大于 K 的最小回文,请考虑以下规则和逻辑:
- 为了使最小的回文大于K,你必须使用所有的数字。 (即 1122 => 1221 就是答案)。
- 一个奇数长度只能有ONE个数字,频率为1,这个数字是中心数字,其他数字必须有一个偶数频率(即11121 = > 11211 就是答案)。否则无法创建回文。
- 对于偶数长度的数字,所有数字的频率必须为偶数(示例,请参见项目符号 1)。否则无法创建回文。
我将从获取每个数字的频率开始,然后从那里开始。
希望对您有所帮助。
我正在尝试解决 SPOJ 中的下一个回文问题。我在下面的 Java 代码中收到超出时间限制的错误。
"A positive integer is called a palindrome if its representation in the decimal system is the same when read from left to right and from right to left. For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros."
import java.math.BigInteger;
import java.util.Scanner;
public class Nextpalindrome {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in =new Scanner(System.in);
int t=in.nextInt();
for (int i=1;i<=t;i++)
{
BigInteger bi = in.nextBigInteger();
String str=bi.toString();
String str1=new String();
String str2=new String();
String str3=new String();
String str4=new String();
int l=str.length();
int comp=0;
if (l==2)
{
str1=str.substring(0,1);
str2=str.substring(1,2);
if (Integer.parseInt(str1)>Integer.parseInt(str2))
str1=str1.concat(str1);
else if (Integer.parseInt(str2)>Integer.parseInt(str1))
{
str2=str2.concat(str2);
str1=str2;
}
else if (Integer.parseInt(str1)==Integer.parseInt(str2))
{
int x=Integer.parseInt(str1)+1;
str1=Integer.toString(x);
str1=str1.concat(str1);
}
}
if (l%2>0)
{
str1=str.substring(0,l/2);
str2=str.substring((l/2)+1,l);
str3 =str.substring(l/2,(l/2)+1);
str4=new StringBuffer(str1).reverse().toString();
BigInteger bi1 = new BigInteger(str1);
BigInteger bi2 = new BigInteger(str2);
comp= bi1.compareTo(bi2);
int mid=Integer.parseInt(str3);
if (comp==-1)
{
mid+=1;
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
else if (comp==1)
{
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
else if (comp==0)
{
mid+=1;
String str5=Integer.toString(mid);
str1=str1.concat(str5);
str1=str1.concat(str4);
}
}
if ((l>2)&&(l%2==0))
{
str1=str.substring(0,l/2);
str2=str.substring(l/2,l);
BigInteger bi1 = new BigInteger(str1);
BigInteger bi2 = new BigInteger(str2);
BigInteger bi3=new BigInteger("1");
comp= bi1.compareTo(bi2);
if (comp==-1)
{
bi1=bi1.add(bi3);
str1=bi1.toString();
str4=new StringBuffer(str1).reverse().toString();
str1=str1.concat(str4);
}
else if ((comp==1)||(comp==0))
{
str4=new StringBuffer(str1).reverse().toString();
str1=str1.concat(str4);
}
}
System.out.println(str1);
}
in.close();
}
}
既然您想找到大于 K 的最小回文,请考虑以下规则和逻辑:
- 为了使最小的回文大于K,你必须使用所有的数字。 (即 1122 => 1221 就是答案)。
- 一个奇数长度只能有ONE个数字,频率为1,这个数字是中心数字,其他数字必须有一个偶数频率(即11121 = > 11211 就是答案)。否则无法创建回文。
- 对于偶数长度的数字,所有数字的频率必须为偶数(示例,请参见项目符号 1)。否则无法创建回文。
我将从获取每个数字的频率开始,然后从那里开始。
希望对您有所帮助。