###name
Typewriter
###descirption
One day, Jerry found a strange typewriter. This typewriter has 2 input modes: pay p coins to append an arbitrary single letter to the back, or q coins to copy a substring that has already been outputted and paste it in the back.
Jerry now wants to write a letter to Tom. The letter is a string S which contains only lowercase Latin letters. But as Jerry is not very wealthy, he wants to know the minimum number of coins he needs to write this letter.
###input
This problem contains multiple test cases. Process until the end of file.
For each test case, the first line contains string S $(|S|≤2×10^5,∑|S|≤5×10^6)$, consisting of only lowercase Latin letters. And the second line contains 2 integers p and q $(1≤p,q<2^{31})$.
###output
For each test case, output one line containing the minimum number of coins Jerry needs to pay.
###sample input
abc
1 2
aabaab
2 1
###sample output
3
6
###toturial
这个题目首先dp肯定跑不掉的,我们设dp[i]为构造出前i个字母的代价,我们先来分析dp函数的特点,他具有以下这些性质,
* $1.$ dp单调不减
* $2.$ 复制方案的决策点递增,
这两个性质非常好证明
据此我们就可以直接来dp了
dp[i] <- dp[i-1]
dp[i] <- dp[j] 这里要求j是最小的值使得前缀S[1..j]包含子串S[j+1..i]
第二个转移方程的决策点递增,于是我们就可以直接利用这一点,来使用后缀自动机加速
###code
1 |
|