The main topic: there is a (a|b) ^n of A and B consisting of a string of length n, allowing to modify the K-bit. The longest number of substrings that can be composed of a or all B are the longest.
Idea, with two queues to save the position of k+1 A and b respectively, the longest substring ending with I is the length of I minus the value of the queue header element.
#include <iostream> #include <cstdio> #include <memory.h> #include <queue>using namespace std; int main (int argc, const char * argv[]) { char input[1000005]; int n,k,res1; Queue<int> Posa; Queue<int> POSB; scanf ("%d%d", &n,&k); for (int i=1;i<=n;i++) { cin>>input[i]; } Posa.push (0);//Pre-save 0 position, the actual input subscript starting from 1 posb.push (0); res1=0; for (int i=1;i<=n;i++) { if (input[i]== ' a ') { posa.push (i); if (Posa.size () >k+1) Posa.pop (); } else{ Posb.push (i); if (Posb.size () >k+1) Posb.pop (); } Res1=max (Res1,i-posb.front ()); Res1=max (Res1,i-posa.front ()); } printf ("%d", res1); return 0;}
Codeforces Round #354 (Div. 2) C. Vasya and String