D - Longest X
https://atcoder.jp/contests/abc229/tasks/abc229_d
参考: https://zhuanlan.zhihu.com/p/441875505
思路
使用acc累计数组,统计每个位置之前的.的数目
设置滑动窗口最大化字串长度,使得窗口内k次替换都被执行, 从头到尾进行统计,记录最大字串长度。
Code
string s; int k; int acc[200005]; int main() { cin >> s; cin >> k; memset(acc, 0, sizeof(acc)); int n = s.size(); for(int i=0; i<n; i++){ char one = s[i]; int rep = 0; if (one == '.'){ rep = 1; } acc[i+1] = acc[i] + rep; } int maxlen = 0; int i=1, j=1; while(true){ if (i > n || j > n){ break; } // for k == 0, it is possible to be this: // i > j // if (i > j){ // break; // } while(acc[j]-acc[i-1]<=k){ j++; if (j>n){ break; } } // for k == 0, and ...., j may be 1, so it is need to updated. if (i > j){ j = i; continue; } // back to last element X, // from i to it (inclusively) k operation done. j--; maxlen = max(maxlen, j-i+1); j++; i++; } cout << maxlen << endl; return 0; }
标签:acc,ATCODER,--,break,int,Longest,https From: https://www.cnblogs.com/lightsong/p/16823807.html