CodeForces 701C They Are Everywhere (滑动窗口)

Posted shuoed

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 701C They Are Everywhere (滑动窗口)相关的知识,希望对你有一定的参考价值。

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

题意:找到字符串中能包含所有元素的最短字符串长度。

利用“滑动窗口”解题

解题思路:

1.遍历找到所有元素进行统计,元素数sum

2.设置两个”指针“ st、en,双重while 循环

3.代码可以解释之后的思路,好好想想便可理解这种思路了0.0

AC code:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int dp[300];
 4 int main()
 5 {
 6     int n;
 7     string s;
 8     while(~scanf("%d",&n))
 9     {
10         cin>>s;
11         int sum=0;
12         memset(dp,0,sizeof(dp));
13         for(int i=0; i<n; i++)
14         {
15             if(!dp[s[i]])
16             {
17                 dp[s[i]]=1;
18                 ++sum;
19             }
20         }
21         int st,en,sum1,ans;
22         st=en=sum1=0;
23         ans=1<<30;
24         memset(dp,0,sizeof(dp));
25         while(st<n)
26         {
27             while(en<n&&sum!=sum1)
28             {
29                 if(dp[s[en]]==0)
30                 {
31                     sum1++;
32                 }
33                 dp[s[en++]]++;
34             }
35             if(sum==sum1)ans=min(ans,en-st);
36             if(--dp[s[st++]]==0)sum1--;
37         }
38         cout<<ans<<endl;
39     }
40     return 0;
41 }

 

以上是关于CodeForces 701C They Are Everywhere (滑动窗口)的主要内容,如果未能解决你的问题,请参考以下文章

!Codeforces Round #364 (Div. 2) C. They Are Everywhere

701 C. They Are Everywhere

1060. Are They Equal (25)

1060. Are They Equal (25)

1060. Are They Equal (25)

1060 Are They Equal (25 分)