1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| #include<bits/stdc++.h> #define x first #define y second #define ok cout << "ok" << endl; using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const double PI = acos(-1.0); const int INF=0x3f3f3f3f; const ll LINF=0x3f3f3f3f3f3f3f3f; const int N=1e5+9; const int shift=1e3+9; const double Eps=1e-7;
int a[N], b[N], c[N], d[N], n, m; string s;
struct node { int ma, cost; }dp[N];
int main(void) { if(fopen("in", "r")!=NULL) {freopen("in", "r", stdin); freopen("out", "w", stdout);} while(cin >> n >> s >> m) { s = '0' + s; int len = s.length(); memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c); memset(d, -1, sizeof d); for(int i = 1; i < len; i++) { c[i] = c[i-1] + (s[i] == '?' ? 1 : 0); if(i >= 2) { a[i] = a[i-2] + (s[i] == '?' || s[i] == 'a' ? 1 : 0); b[i] = b[i-2] + (s[i] == '?' || s[i] == 'b' ? 1 : 0); } else { a[i] = (s[i] == '?' || s[i] == 'a' ? 1 : 0); b[i] = (s[i] == '?' || s[i] == 'b' ? 1 : 0); } } for(int i = m; i < len; i++) { if(m & 1) { int ta = a[i] - a[max(0, i - m - 1)]; int tb = max(0, b[i-1] - b[max(0, i - m)]); if(ta >= (m+1)/2 && tb >= m/2) d[i] = c[i] - c[i-m]; } else { int ta = a[i-1] - a[max(0, i - m - 1)]; int tb = b[i] - b[i - m]; if(ta >= (m+1)/2 && tb >= m/2) d[i] = c[i] - c[i-m]; } } memset(dp, 0, sizeof dp); for(int i = m; i < len; i++) { if(d[i] == -1) dp[i] = dp[i-1]; else if(dp[i-m].ma + 1 > dp[i-1].ma || (dp[i-m].ma + 1 == dp[i-1].ma && dp[i-m].cost + d[i] < dp[i-1].cost)) dp[i].ma = dp[i-m].ma + 1, dp[i].cost = dp[i-m].cost + d[i]; else dp[i] = dp[i-1]; } printf("%d\n", dp[len-1].cost); } return 0; }
|