您的位置:首页 > 其它

【zzulioj 1895 985的0-1串难题】

2016-08-09 21:26 344 查看
985的0-1串难题

Description

985有一个长度为n的0-1串,已知他最多可以修改k次(每次修改一个字符即0->1 或者 1->0),他想知道连续的全1子串最长是多少。

Input

第一行输入一个整数t,代表有t组测试数据。

每组数据第一行输入两个整数n,k分别代笔上面的信息。

注:1 <= t <= 12,1 <= n <= 100000,0 <= k <= 100000。

Output

一个整数代表可以得到的最大长度。

Sample Input

2

6 3

010100

6 2

010100

Sample Output

5

4

#include<stack>
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define   K   100011
#define INF 0x3f3f3f
using namespace std;
int  pa[K];
char st[K];
int main()
{
int i,j;
int T;
int k;
int N;
int ml;
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&k);
scanf("%s",st);
pa[0]=0;//开始和结尾各加一个零
ml=1;//记录零的个数
for(i=0;i<N;i++)
if(st[i]=='0')
pa[ml++]=i+1;
pa[ml]=N+1;
if(k>=ml-1)//如果零的总数小于k,则 最长字符串为N
{
printf("%d\n",N);
continue;
}
ans=0;
for(i=k+1;i<=ml;i++)
ans=max(ans,pa[i]-pa[i-k-1]-1);//更新字符串的最大长度
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: