I read many codes and they apply brute force and it passes how?
def get_ans(string):
n = len(string)
ans = 0
for i in range(2,n,2):
if(string[:i//2]==string[(i//2):i]):
if(string[i:((n+i)//2)] == string[(n+i)//2:n]):
ans += 1
print(ans)
This solution is passed but I did this why i got tle?
One case that was causing TLE to my brute force solution was when all the characters are same. Handling it separately by answering (n-2)/2 . It passed all test cases.
Same here. I killed so much of my time coding and debugging a Rabin-Karp style approach with expected time complexity O(n). Still it got TLE. Whereas people using substrings in C++ have got AC.
Same here , I spent lot of time optimising it using longest prefix suffix array that we calculate in KMP algorith whereas bruteforce solution got passed of many guys. Thats really bad.
def get_ans(string):
n = len(string)
ans = 0
for i in range(2, n, 2):
if (string[:i // 2] == string[(i // 2):i]) and (string[i:((n + i) // 2)] == string[(n + i) // 2:n]):
ans += 1
print(ans)
for _ in range(5):
string = "a"*(10**5)
get_ans(string)
This solution get TLE in CC compiler as in question already mention that sum of length o string in all test case may be upto 10^6 now see this above solution …it will give TLE
@rishup_nitdgp@admin this is so wrong that even brute force runs and very weak test cases
Tell me one thing (anyone tell me) why same type of solution ( Brute force) when we use substr function in c++ it will be accepted but when i make my own substr function or let’s say function to check validity (as i mentioned abve) it got tle ? Is “Substr” function is faster?
and one more thing why substr function of java give TLE while C++ give AC verdict ?
I know it’s frustrating and I might sound biased but I don’t think that would be fair, since a lot of people who submitted using substr() (including me) would’ve tried other string matching methods if they got a TLE. But I left the question and went to the next because it was AC.
I think they should rather rejudge with test cases so that all O(n^2) codes(that are correct) pass. That would be fair to everyone
The standard library substr is probably faster because the compiler knows exactly what it does and can optimize it better. Java is often slower than C++ so a solution that TLEs in Java and ACs in C++ is normal.
Who said it got accepted? I was pissed because I got TLE thrice. So there’s no issue with a custom substr() function. I believe that the test cases are poor.
I agree that rejudging with strong tests isn’t ideal but I think what you’re suggesting is worse. If we let all O(N^2) solutions pass then all the people who were naive enough to try a brute force gets AC quickly while all the people who correctly determined that O(N^2) should be too slow wasted a bunch of time implementing the intended solution.
bhai…there are sites where all languages have same time limit.
codechef atleast has 5X for python.
if u are using python from so long…then u should know when it works
and when to use c++.