标签:bit pre %s ios == r++ com div class
https://ac.nowcoder.com/acm/contest/3002/H
题意:给出长度为n的01字符串,可以进行k次操作:将一个字符0变1,1变0.问最长相同子串为多长。
解法:贪心,双指针模拟l , r 操作过程。
//#include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> #define ME(x , y) memset(x , y , sizeof(x)) #define SF(n) scanf("%d" , &n) #define rep(i , n) for(int i = 0 ; i < n ; i ++) #define INF 0x3f3f3f3f #define mod 1000000007 #define PI acos(-1) using namespace std; typedef long long ll ; char s[200009]; int n , k ; int deal(char a , char a1) { int l = 0 , r = 0 , ans = 0 , t = 0 ; for(int i = 0 ; i < n ; i++) { if(s[i] == a) { if(t < k) { t++; r++ ; } else{ while(r >= l && s[l] != a) l++; l++; r++; } } else{ r++; } ans = max(ans , r - l); } return ans ; } int main() { cin >> n >> k ; scanf("%s" , s); cout << max(deal(‘1‘ , ‘0‘) , deal(‘0‘ , ‘1‘)) << endl; }
标签:bit pre %s ios == r++ com div class
原文地址:https://www.cnblogs.com/nonames/p/12266156.html