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 ?
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3821 |
3 | Benq | 3736 |
4 | Radewoosh | 3631 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3388 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
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 ?
Название |
---|
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.