codeforces 701C(尺取法)

本文解析了CodeForces上的一道题目,采用尺取法求解覆盖所有字符的最短子串问题,并提供了一个清晰的算法实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/701/C

题意:
给出一长度为n的字符串,问涵盖了所有在该串中出现过的字母的子串(连续)的最小长度是多少

分析:
从第一个字母开始,先找到包含所有字母的子串。想象该子串为一个队列,左边为队首,右边为队尾。左边的字母不断出队(如果出队后不会使某字母缺失),就可以更新最小长度。一旦无法出队,则右边的字母进队,然后继续判左边字母。

举个例子:给出一段序列,球区间和大等于10的最短区间。

序列: 2 5 8 7 3 2

①从头开始找到满足条件的区间, 2 5 8 7 3 2 –>最短长度为3
②删除队首 2 , 2 5 8 7 3 2 –>最短长度为2
③删除队首5,无法删除,7进队, 2 5 8 7 3 2 –>最短长度仍为2
④删除队首5, 2 5 8 7 3 2 –>最短长度为2
⑤……以此类推

以上即为尺取法思想,复杂度O(n)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstring>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define INF ((1<<30)-1)
#define in(x) scanf("%d", &x)
#define inn(x,y) scanf("%d %d", &x,&y)
#define lin(x) scanf("%I64d", &x)
#define out(x) printf("%d\n", x)
#define lout(x) printf("%I64d\n", x)
#define rep(i,a,b) for(int i=a; i<=(b); i++)
#define ll __int64
#define mem(a,b) memset(a, b, sizeof(a))
#define bug(x) cout<<#x<<" = "<<x<<";   "
#define N 100005
#define M 1000000001
#define nn printf("\n")

char s[N];
bool vis[500];
int last[500];

int main()
{
    int n;
    in(n);
    scanf("%s", s+1);

    mem(vis,0);
    mem(last,0);

    int sum=0;
    rep(i,1,n)
    {
        if(vis[s[i]]==0)
            vis[s[i]]=1, sum++;
    }
    int l=1, r;
    for(r=1; r<=n; r++)
    {
        last[s[r]]=r;
        if(vis[s[r]])
            vis[s[r]]=0, sum--;
        if(!sum) break;
    }
    int ans=r-l+1;
    while(r<=n)
    {
        last[s[r]]=r;
        while(last[s[l]]!=l)
        {
            l++;
            ans=Min(ans, r-l+1);
        }
        r++;
    }
    out(ans);

    return 0;
}


### Codeforces Problem 1332C Explanation The provided references pertain specifically to problem 742B on Codeforces rather than problem 1332C. For an accurate understanding and solution approach for problem 1332C, it's essential to refer directly to its description and constraints. However, based on general knowledge regarding competitive programming problems found on platforms like Codeforces: Problem 1332C typically involves algorithmic challenges that require efficient data structures or algorithms such as dynamic programming, graph theory, greedy algorithms, etc., depending upon the specific nature of the task described within this particular question[^6]. To provide a detailed explanation or demonstration concerning **Codeforces problem 1332C**, one would need direct access to the exact statement associated with this challenge since different tasks demand tailored strategies addressing their unique requirements. For obtaining precise details about problem 1332C including any sample inputs/outputs along with explanations or solutions, visiting the official Codeforces website and navigating to contest number 1332 followed by examining section C is recommended. ```python # Example pseudo-code structure often seen in solving competitive coding questions. def solve_problem_1332C(input_data): # Placeholder function body; actual logic depends heavily on the specifics of problem 1332C. processed_result = process_input(input_data) final_answer = compute_solution(processed_result) return final_answer input_example = "Example Input" print(solve_problem_1332C(input_example)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值