[英]1772 of Caribbean online judge giving a time limit exceeded error. please help me find why is my algorithm taking so long
So I am trying to solve the problem 1772 of the Caribbean online judge web page http://coj.uci.cu/24h/problem.xhtml?abb=1772 , the problem asks to find if a substring of a bigger string contains at least one palindrome inside it: 所以我试图解决加勒比在线评判网页的问题1772 http://coj.uci.cu/24h/problem.xhtml?abb=1772 ,该问题要求查找更大字符串的子字符串是否包含在里面至少有一个回文:
eg Analyzing the sub-strings taken from the following string: "baraabarbabartaarabcde" 例如,分析从以下字符串中取出的子字符串:“baraabarbabartaarabcde”
"bara" contains a palindrome "ara" “bara”包含回文“ara”
"abar" contains a palindrome "aba" “abar”包含一个回文“aba”
"babar" contains a palindrome "babar" “babar”包含一个回文“babar”
"taar" contains a palindrome "aa" “taar”包含一个回文“aa”
"abcde" does not contains any palindrome. “abcde”不包含任何回文。
etc etc etc... 等等等......
I believe my approach is really fast because I am iterating the strings starting at the first char and at the last char at the same time, advancing towards the center of the string looking only for the following patterns: "aa" "aba" whenever I find a pattern like those I can say the substring given contains a palindrome inside it. 我相信我的方法非常快,因为我在同一时间从第一个字符串和最后一个字符处开始迭代字符串,向字符串的中心前进,只查看以下模式:“aa”“aba”每当我找到一个像我可以说的那样的模式,给定的子串包含一个回文。 Now the problem is that the algorithm is taking a long time but I can't spot the problem on it. 现在的问题是该算法需要很长时间,但我无法发现问题。 Please help me find it I am really lost on this one. 请帮我找到它我真的迷失了这个。 Here is my algorithm 这是我的算法
public static boolean hasPalindromeInside(String str)
{
int midpoint=(int) Math.ceil((float)str.length()/2.0);
int k = str.length()-1;
for(int i = 0; i < midpoint;i++)
{
char letterLeft = str.charAt(i);
char secondLetterLeft=str.charAt(i+1);
char letterRight = str.charAt(k);
char secondLetterRight = str.charAt(k-1);
if((i+2)<str.length())
{
char thirdLetterLeft=str.charAt(i+2);
char thirdLetterRight=str.charAt(k-2);
if(letterLeft == thirdLetterLeft || letterRight == thirdLetterRight)
{
return true;
}
}
if(letterLeft == secondLetterLeft || letterRight==secondLetterRight)
{
return true;
}
k--;
}
return false;
}
}
I have removed the code that grabs the input strings and intervals of sub-strings, I am using String.substring() to get the substrings and I don't think that will be causing the problem. 我删除了抓取输入字符串和子字符串间隔的代码,我使用String.substring()来获取子字符串,我不认为这会导致问题。 If you need that code please let me know. 如果您需要该代码,请告诉我。 Thanks! 谢谢!
I think you can solve this in O(1) time per query given O(n) preprocessing to find the locations of all 2 and 3 character palindromes. 我认为你可以在每个查询的O(1)时间内解决这个问题,给定O(n)预处理以找到所有2和3个字符回文的位置。 (Any even plaindrome will have a 2 character plaindrome at the centre, while any odd will have a 3 character one so it suffices to check 2 and 3.) (任何偶数plaindrome在中心将有一个2字符plaindrome,而任何奇数将有一个3字符一个所以它足以检查2和3.)
For example, 例如,
Given your string baraabarbabartaarabcde, first compute an array indicating the locations of the 2 character palindromes: 鉴于你的字符串baraabarbabartaarabcde,首先计算一个数组,指出2个字符回文的位置:
baraabarbabartaarabcde
000100000000001000000-
Then compute the cumulative sum of this array: 然后计算此数组的累积总和:
baraabarbabartaarabcde
000100000000001000000-
000111111111112222222-
By doing a subtraction you can immediately work out whether there are any 2 character palindromes in a query range. 通过减法,您可以立即确定查询范围内是否有任何2个字符的回文。
Similarly for three character plaindromes: 同样对于三个字符的平台:
baraabarbabartaarabcde String
01001000100000010000-- Indicator
01112222333333344444-- Cumulative
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.