【题解】未出现的子串

题目大意

  给定一个长度为\(n\)的数字序列\(s\)\(1 \leq n \leq 100000\)),满足\(1 \leq s_i \leq q\)\(5 \leq q \leq 9\))。求一个最短的子序列\(s'\),满足\(s'\)\(s\)中没有出现。为了方便,请输出\(s'\)的长度。

题解

  当存在一个\(i\),满足\(s_1 \dots s_i\)中数字\(1 \dots q\)都出现过,那么肯定不存在长度\(\leq 1\)\(s'\)。当存在一个\(j\),满足\(s_{i + 1} \dots s_j\)中数字\(1 \dots q\)都出现过,那么肯定不存在长度\(\leq 2\)\(s'\)
  容易推广得对于序列\(p\),如果满足\(p_0 = 1\)\(p_i < p_{i + 1}\)\(p_m \leq n\),且\(s_{p_i} \dots s_{p_{i + 1} - 1}\)中数字\(1 \dots q\)都出现过,则最大的\(m\)即为所求的\(s'\)的长度。

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n, q;
bool b[15];
int ans = 1;

int main()
{
    scanf("%d%d", &n, &q);
    int cnt = 0, tmp;
    while (n--)
    {
        getchar();
        tmp = getchar() - '0';
        if (!b[tmp])
        {
            b[tmp] = true;
            ++cnt;
            if (cnt == q) 
            {
                ++ans;
                cnt = 0;
                memset(b, 0, sizeof b);
            }
        }
    }
    printf("%d", ans);
    return 0;
}

转载于:https://www.cnblogs.com/kcn999/p/11563134.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值