给一个串,问有多少和它长度相同的串,使得LCS为l - 1。
【字符串|【codeforces 576D】LCS Again】
#include
#include
#include
#include
#include
#define Rep(i, x, y) for (int i = x;
i <= y;
i ++)
#define Dwn(i, x, y) for (int i = x;
i >= y;
i --)
#define RepE(i, x) for(int i = pos[x];
i;
i = g[i].nex)
using namespace std;
typedef long long LL;
const int N = 100005;
int n, m, p, q, s1[N], s2[N];
char s[N];
LL ans;
int main()
{
scanf ("%d%d", &n, &m);
scanf ("%s", s + 1);
ans = n * (m - 1);
Rep(i, 1, n - 1) {
s1[i] = s1[i - 1] + (s[i] != s[i + 1]);
ans += s1[i] * (m - 1);
}
Dwn(i, n, 2) {
s2[i] = s2[i + 1] + (s[i] != s[i - 1]);
ans += s2[i] * (m - 1);
}
Rep(i, 1, n - 1) {
p = max(0, p - 2);
while (i + p + 2 <= n && s[i + p] == s[i + p + 2]) p ++;
if (s[i] != s[i + 1]) ans -= p + 1;
swap(p, q);
}
printf("%I64d\n", ans);
return 0;
}
推荐阅读
- 每日一题|每日一题-解码(第十一届蓝桥杯)(简单思维)
- codeforces B. Young Explorers
- codeforces C. Mere Array
- codeforces D. Omkar and Bed Wars
- codeforces C. Omkar and Waterslide
- codeforces B. Omkar and Infinity Clock
- codeforces B. Ternary Sequence
- 题库-CF|【Codeforces Round 370 (Div 2) E】【线段树 等比数列 区间合并】Memory and Casinos 赌场区间[l,r] l进r先出的概率
- 题库-CF|【Codeforces Round 263 (Div 2)C】【贪心 哈弗曼思维】Appleman and Toastman 每个非1size子树延展为2子树的最大权
- Codeforces|Codeforces Round #605 (Div. 3) D. Remove One Element