wannafly28-B

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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值