https://ac.nowcoder.com/acm/contest/217/B
给出只含msc三种字符的字符串,求出(i, j)开始位置到结束位置中只含msc、mcc两个子序列且不能占用重复元素.
分析可知有且只有
mmsccc,mmcscc,mmccsc,msmccc,mscmcc,mcmscc,mcmcsc,mccmsc
三种字符串可以满足题意。
具体分析看这篇博客https://blog.csdn.net/Jaihk662/article/details/83903786
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
char str[maxn];
char tmp[20][20] = {
"mmsccc",
"mmcscc",
"mmccsc",
"msmccc",
"mscmcc",
"mcmscc",
"mcmcsc",
"mccmsc"
};
int n, nxt[maxn][28];
int main()
{
scanf("%d", &n);
scanf("%s", str + 1);
char tar[10] = "msc";
//nxt[j][i]预处理str[j]后最近的tar[i - 'a']字符
for(register int x = 0; x < 3; x++) {
int i = tar[x];
for(register int j = n; j >= 1; j--) {
nxt[j - 1][i - 'a'] = nxt[j][i - 'a'];
if(str[j] == (char)i) nxt[j - 1][i - 'a'] = j;
}
}
ll ans = 0;
for(register int x = 1; x <= n; x++) {
//枚举每个区间开始位置
int y = n + 1;
for(register int i = 0; i < 8; i++) {
int now = x - 1;
for(register int j = 0; j < 6; j++) {
now = nxt[now][tmp[i][j] - 'a'];
if(now == 0) break;
}
if(now != 0) y = min(y, now);
}
ans += n - y + 1;
}
printf("%lld\n", ans);
}