Obsessive String The first part of substring search(using KMP ) is clear. I was going through the editorial but the DP state is unclear to me. Is it some standard counting technique. If yes could anyone provide similar problem links ?
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Obsessive String The first part of substring search(using KMP ) is clear. I was going through the editorial but the DP state is unclear to me. Is it some standard counting technique. If yes could anyone provide similar problem links ?
Name |
---|
I just did this problem, and I agree that the editorial is a but unclear.
Let's just have one dp array. dp[i] counts the number of ways to be at the following position: "we haven't decided what to do with character i yet, but we have decided for stuff before i."
At character i, we can ignore it and move to i+1. This is easy: dp[i+1] += dp[i].
At character i, we can also choose to take it. Let f denote the first index (f>=i) such that the substring from f to f+T-1 is equivalent to T. Notice that if we take i, we always have to take up to f+T-1 as well. This means that we can only choose a substring which ends at f+T-1 to S. Therefore, we do a range update (suppose with a segment tree) from f+T to S+1 inclusive (add dp[i] to this range).
If this isn't clear yet maybe go back and convince yourself why this works.
Now we want to make this O(N) because segment tree solution is O(N log N). Notice that you are going through the array one index by one index and you always add to values after yourself. This means we can use a prefix sum which we maintain as we go.
Specifically, we keep delta[] array, and if we want to update from f+T to the end, we simply do delta[f+T] += dp[i]. Then, at each index, we keep a prefix sum (call it cur), and do cur += delta[i]. Notice that after this, cur is actually the value which you would normally query from the range tree. Try convince yourself of this.
Then, obviously we have a dp array, and we simply read the value from dp[S+1] (because that means we are about to do character S+1 which doesn't exist, so we are finished).
Thanks for answering. "At character i, we can ignore it and move to i+1."
Instead of dp[i+1] += dp[i] Shouldn't it be dp[i] += dp[i-1]
No, dp[i] is the value if we haven't considered i yet. Because we just considered i, we need to update dp[i+1].
In other words dp[x] means ignore everything strictly before x.