1895: 985的0-1串难题(好题)

1895: 985的0-1串难题

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 197   Solved: 51

Submit Status Web Board

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

26 30101006 2010100

Sample Output

54
很好的思维题,记录0的位置,找出符合条件最长的末位置减初位置就可以了.


#include<stdio.h>
#include<string.h> 
#include<iostream>
#include<algorithm>
using namespace std;


int main ()
{
	int dis[100010];
	char s[100010];
	int t,n,m;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		scanf("%s",s);
		int p = 0,g = 0;
		dis[p++] = 0;
		
		for(int i=0; i<n; i++){
			if(s[i] == '0'){
				dis[p++] = i+1;	
			}
		}
		dis[p++] = n+1;
		
		if(p-1 <= m){
			printf("%d\n",n);continue;
		}		
		else{
			for(int i=m+1; i<p; i++){
				g = max(g,dis[i]-(dis[i-(m+1)]+1));
			}
			printf("%d\n",g);
		}
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值