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 | 3882 |

2 | maroonrk | 3539 |

3 | Benq | 3513 |

4 | MiracleFaFa | 3466 |

5 | ksun48 | 3462 |

6 | ecnerwala | 3446 |

7 | slime | 3428 |

8 | Um_nik | 3426 |

9 | jiangly | 3401 |

10 | greenheadstrange | 3393 |

# | User | Contrib. |
---|---|---|

1 | awoo | 192 |

2 | -is-this-fft- | 191 |

3 | Monogon | 184 |

4 | YouKn0wWho | 182 |

4 | Um_nik | 182 |

6 | antontrygubO_o | 171 |

7 | maroonrk | 169 |

8 | kostka | 165 |

9 | SecondThread | 164 |

9 | errorgorn | 164 |

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 ?

↑

↓

Codeforces (c) Copyright 2010-2022 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: May/25/2022 23:08:46 (g2).

Desktop version, switch to mobile version.

Supported by

User lists

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.