1196D2 - RGB Substring (hard version)

1196D2 - RGB Substring (hard version)


#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> //#include <unordered_set> //#include <unordered_map> #define ll long long #define pii pair<int, int> #define rep(i,a,b) for(ll i=a;i<=b;i++) #define dec(i,a,b) for(ll i=a;i>=b;i--) #define forn(i, n) for(ll i = 0; i < int(n); i++) using namespace std; int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const double eps = 1e-6; const int mod = 1e7 + 7; inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } inline ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } void exgcd(ll A, ll B, ll& x, ll& y) { if (B) exgcd(B, A % B, y, x), y -= A / B * x; else x = 1, y = 0; } bool prime(int x) { if (x < 2) return false; for (int i = 2; i * i <= x; ++i) { if (x % i == 0) return false; } return true; } inline int qpow(int x, ll n) { int r = 1; while (n > 0) { if (n & 1) r = 1ll * r * x % mod; n >>= 1; x = 1ll * x * x % mod; } return r; } /**********************************************************/ int len , n; int solve(string& s,string& t) { vector<int> v(len); int cnt =0; forn(i,len) { if(s[i]!=t[i]) v[i]=1; } int i=0,j=0; while(j<n) { if(v[j]==1) cnt++; j++; } int ans=inf; while(j<len) { ans=min(ans,cnt); if(j<len) cnt+=-v[i]+v[j]; i++,j++; } ans=min(ans,cnt); return ans; } int main(){ int T; cin>>T; while(T--) { cin>>len>>n; string s; cin>>s; string t; for(int i=0;i<len;i++) { if(i%3==0) t+='R'; else if (i%3==1) t+='G'; else t+='B'; } int ans=inf; ans=min(ans,solve(s,t)); rep(i,0,1) { t.erase(0,1); if((len+i)%3==0) t+='R'; else if ((len+i)%3==1) t+='G'; else t+='B'; ans=min(ans,solve(s,t)); } cout<<ans<<endl; } }

 尺取的时候长度应该是n+1

posted @ 2020-07-24 10:50  DeaL57  阅读(120)  评论(0编辑  收藏  举报