D - Longest X -- ATCODER
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; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。