Hi everyone, recently I encounter this problem Fibonacci Words and I can not find bug in my code. I have try to generate many test cases and compare the result again the correct code, and I found not any single test case failed. Please help to debug my code, I'm really depressed now. Currently I failed test case number #8 in codeforces.
Briefly about my implementation, I divide the problem into two cases: n <= 26 and n > 26. For the case n <= 26, I just brute force and use KMP to find the number of occurences of s in the current words (I choose n = 26 because n = 25 and 26 both have len >= 1e5 which is max length of s). For the case n > 26, dp[n] = dp[n — 1] + dp[n — 2] + occurences of s that intersect both n — 1 and n — 2 strings.
Suppose: string 25 = b
string 26 = a
=> string 27 = ab
=> string 28 = aba
=> string 29 = abaab
=> string 30 = abaababa
=> I saw that the intersection for the first string 27 is ab, and from 28 there will be an alternate between aa and ba. Based on that, I can find the occurences of s in ab, aa and ba respectively.
Here is my code: