Codeforces Round #354 (Div. 2)_Vasya and String(尺取法)
题目连接:http://codeforces.com/contest/676/problem/C
题意:一串字符串,最多改变k次,求最大的相同子串
题解:很明显直接尺取法
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<string> 5 #include<set> 6 #include<map> 7 #include<vector> 8 #include<queue> 9 #include<algorithm> 10 #include<functional> 11 #define cl(a,b) memset(a,b,sizeof(a)); 12 #define FFC(i,a,b) for(int i=a;i<=b;++i) 13 #define pb push_back 14 #define LL long long 15 #define dbg puts("ok") 16 #define min(a,b) ((a)>(b)?(b):(a)) 17 #define max(a,b) ((a)>(b)?(a):(b)) 18 using namespace std; 19 char a[100010]; 20 int main(){ 21 int n,k; 22 while(~scanf("%d%d",&n,&k)){ 23 scanf("%s",a); 24 int l=0,r=0,ans=0,ca=0,cb=0,tmp=0; 25 while(r<n){ 26 while(r<n&&tmp<=k){ 27 if(a[r]=='a')ca++;else cb++; 28 tmp=min(ca,cb); 29 r++; 30 } 31 if(r==n&&tmp<=k){ans=max(r-l,ans);break;} 32 r--;if(a[r]=='a')ca--;else cb--;tmp=min(ca,cb); 33 ans=max(r-l,ans); 34 while(a[l]==a[l+1]){if(a[l]=='a')ca--;else cb--;l++,tmp=min(ca,cb);} 35 if(a[l]=='a')ca--;else cb--;l++,tmp=min(ca,cb); 36 } 37 printf("%d\n",ans); 38 } 39 return 0; 40 }