简体   繁体   English

1772年加勒比海在线法官给出时间限制超出错误。 请帮我解释为什么我的算法花了这么长时间

[英]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.

相关问题 超过在线判断的时间限制 - Time limit exceeded online judge UVa在线判断器#458超出时间限制 - UVa online judge #458 time limit exceeded 算法中的时限超出错误 - Time Limit Exceeded Error in algorithm 请帮我找到这个错误。 我正在使用 hibernate 5.6.4 和 MySQL 8.0.29 - Please help me to Find this error. I'm using hibernate 5.6.4 with MySQL 8.0.29 我收到以下运行时错误。 请帮我找出原因 - I'm getting the following runtime error. Please help me figure out why 为什么我的 in.ready() 需要这么长时间? - Why is my in.ready() taking so long? 出现错误的 QuickSort Java 代码超出时间限制 - QuickSort Java code giving error Time Limit Exceeded 我不知道为什么我的程序给我花括号错误。 我怎样才能解决这个问题 ? 请帮忙 - I don't know why my program is giving me curly brace errors. How can I fix this ? Please help 我访问相机的 kotlin 代码给出了这个错误-&gt;未解决的参考:PERMISSION_GRANTED。请帮我解决这个错误 - My kotlin code to access the camera is giving this error ->unsolved reference:PERMISSION_GRANTED.Please help me to solve the error 当我启动Web Start应用程序时,出现以下错误。 请帮我 - When I launch an web start application i am geteng the following error. Please help me
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM