codeforces #354(div 2) C

题目链接:
http://codeforces.com/contest/676/problem/C
题目大意:
给定一个长度小于等于100 000的一个只含有a或者b字母的字符串和一个常数k,
问在最多只能改变k个字符的情况下最长的元素都相等的子串的长度为多少?
解题思路:
算法比较简单,不妨假设满足题意的子串所含的元素为a,定义两个变量
f和r代表一前一后的两个指针,tmp保存指针所指的子串中b的个数,然后f往前
扫,遇到b就更新tmp,当tmp>k时f不动,r往前扫,遇到b也更新tmp,当tmp<=k
时继续重复刚才的操作,每次移动f时也要记得更新ans。
AC代码:

#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
#define _inf(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#define mp make_pair
#define FORD(i,a,b) for (int i=(a); i<=(b); i++)
#define FORP(i,a,b) for (int i=(a); i>=(b); i--)
#define REP(i,n) for (int i=0; i<(n); i++)
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;

typedef long long LL;

int dir[4][2]={{0,-1},{0,1},{1,0},{-1,0}};
LL pow_mod(LL a,LL n,LL m)
{
	if(n == 0) return 1;
	LL x = pow_mod(a,n>>1,m);
	LL ans = x*x%m;
	if(n&1) ans = ans*a%m;
	return ans;
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}
int main()
{
    /*#ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif*/
	//ios_base::sync_with_stdio(false); cin.tie(0);

	int n,k,a[100000+5] = {0},b[100000+5] = {0};
	scanf("%d%d\n",&n,&k);
	int ans = 0,ans2 = 0;
	for(int i = 0;i<n;i++)
	{
		char ch;
		scanf("%c",&ch);
		if(ch == 'a') a[i]++;
		else b[i]++;
	}
	int f = -1,r = -1,tmp = 0;
	while(1)
	{
		f++;
		if(f>=n)break;
		tmp+=b[f];
		while(tmp>k)
		{
			r++;
			tmp-=b[r];
		}
		ans = max(ans,f-r);
	}

	f = -1;r = -1;tmp = 0;
	ans2 = f;
	while(1)
	{
		f++;
		if(f>=n)break;
		tmp+=a[f];
		while(tmp>k)
		{
			r++;
			tmp-=a[r];
		}
		ans2 = max(ans2,f-r);
	}
	
	printf("%d\n",max(ans,ans2));

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值