このコードのパフォーマンスを向上させ、コンパイル時間を短縮し、コードの機能を同じに保つにはどうすればよいですか?Java 8:次のコードのコンパイル時間を減らす方法は?
このコードは、異なる文字列から2つのサブストリングを抽出し、可能な限り大きなパリンドローム文字列を提供するためにそれらを連結することです。
問題は次のとおりです。あなたは2つの文字列、(a)と(b)を持っています。 (c)=(d)+(e)のような文字列を見つける。 (d)は(a)の空でない部分文字列であり、(e)は(b)の空でない部分文字列であると表すことができる。 (c)はパリンドローム文字列です。 長さは可能な限り長くなります。 入力として受け取った文字列(a)と文字列(b)のペアごとに、新しい行の文字列を検索して出力します。複数の有効な文字列を作成できる場合は、アルファベット順に最初の文字列を印刷します。有効な回答がない場合は、代わりに-1を出力します。
import java.io.*;
import java.util.*;
public class Solution {
boolean isPalindrome(String s) {
int n = s.length();
for (int i=0;i<(n/2);++i) {
if (s.charAt(i) != s.charAt(n - i - 1)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String result="";
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int a=0; a<n; a++)
{int length1, length2, i,c,d,j;
int max_length=0;
String string1 = in.next();
String sub1,sub2;
String string2 = in.next();
length2=string2.length();
length1 = string1.length();
for(c = 0 ; c <length1 ; c++)
{
for(i = length1-c ; i >0 ; i--)
{
sub1 = string1.substring(c, c+i);
for(d = 0 ; d < length2 ; d++)
{
for(j = length2-d ; j >0 ; j--)
{
sub2 = string2.substring(d, d+j);
String temp=sub1+sub2;
Solution obj= new Solution();
if(temp.length()>=max_length && obj.isPalindrome(temp)==true)
{
if (max_length==temp.length())
{ if(temp.compareTo(result)<0)
{
result=temp;
}}
else {
max_length=temp.length();
result=temp;
}
}
}
}
}
}
if(max_length==0)
System.out.println(-1);
else
{
System.out.println(result);
result="";
}
} /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}
これは、あなたのコードに問題がないので、ここで答えている質問のタイプではありません。 –
これは、 'String#contains'をどこかで使用したいと思うかもしれません;) –
ありがとうございます。 私はあなたがどこでそれを使うべきかについてもっと具体的に説明したいと思います。 –