Codeforces Round # 575 (Div. 3) D1 + D2. RGB Substring (easy version) D2. RGB Substring (hard version) (thinking, enumeration, and prefix)

D1. RGB Substring (easy version)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The only difference between easy and hard versions is the size of the input.

You are given a string s consisting of n characters, each character is 'R', 'G' or 'B'.

You are also given an integer k. Your task is to change the minimum number of characters in the initial string s so that after the changes there will be a string of length k that is a substring of s, and is also a substring of the infinite string "RGBRGBRGB ...".

A string a is a substring of string b if there exists a positive integer i such that a1=bi, a2=bi+1, a3=bi+2, ..., a|a|=bi+|a|−1. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.

You have to answer q independent queries.

Input
The first line of the input contains one integer q (1≤q≤2000) — the number of queries. Then q queries follow.

The first line of the query contains two integers n and k (1≤k≤n≤2000) — the length of the string s and the length of the substring.

The second line of the query contains a string s consisting of n characters 'R', 'G' and 'B'.

It is guaranteed that the sum of n over all queries does not exceed 2000 (∑n≤2000).

Output
For each query print one integer — the minimum number of characters you need to change in the initial string s so that after changing there will be a substring of length k in s that is also a substring of the infinite string "RGBRGBRGB ...".

Example
inputCopy
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
outputCopy
1
0
3
Note
In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB".

In the second example, the substring is "BRG".

Meaning of the questions:
two questions but different data ranges, so put together to explain, give you a string and a k, ask you how many characters the least change can make a substring of string length k is infinite RGBRGBRGB ***** long sub-string?
Ideas:

D1: time complexity will be apparent to O (n * k) do

We first constructed three RGB string length k to R, G, B are the beginning.

Then go to O (n- k) enumerate all string length k successive substrings O (. 3 K) different numbers of scanning the character sub-string and a string of three GBR,

Maintain a minimum number of different topics is the answer.

D2: time complexity of O (N + K) is acceptable.

So let's construct three RGB string of length n, so each match and continue the string str.

Match if we maintain a [i] and the i-th string RGB strings of different, different from 1, 0 is the same.

Then find a [i] and the prefix, and O (N) sweep over the processing of a string of length k and the number of different values ​​of RGB least string.

Two codes: the first is D1, D2 is the second

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
 
    int q;
    int n,k;
    gbtb;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        cin>>s;
        string str1,str2,str3;
        str1="";
        str2="";
        str3="";
        for(int i=0;i<k;++i)
        {
            if(i%3==0)
            {
                str1.pb('R');
            }else if((i%3)==1)
            {
                str1.pb('G');
            }
            else
            {
                str1.pb('B');
            }
        }
        for(int i=0;i<k;++i)
        {
            if(i%3==0)
            {
                str2.pb('G');
            }else if((i%3)==1)
            {
                str2.pb('B');
            }
            else
            {
                str2.pb('R');
            }
        }
        for(int i=0;i<k;++i)
        {
            if(i%3==0)
            {
                str3.pb('B');
            }else if((i%3)==1)
            {
                str3.pb('R');
            }
            else
            {
                str3.pb('G');
            }
        }
        int ans=inf;
        for(int i=0;i+k-1<n;++i)
        {
            int cnt=0;
            for(int j=i;j<=i+k-1;++j)
            {
                if(s[j]!=str1[j-i])
                {
                    cnt++;
                }
            }
            ans=min(ans,cnt);
            cnt=0;
            for(int j=i;j<=i+k-1;++j)
            {
                if(s[j]!=str2[j-i])
                {
                    cnt++;
                }
            }
            ans=min(ans,cnt);
            cnt=0;
            for(int j=i;j<=i+k-1;++j)
            {
                if(s[j]!=str3[j-i])
                {
                    cnt++;
                }
            }
            ans=min(ans,cnt);
            cnt=0;
        }
        cout<<ans<<endl;
    }
 
 
    return 0;
}
 
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}
 
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
char s[maxn];
int a[maxn];
int sum[maxn];
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
 
    int q;
    int n,k;
    gbtb;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        cin>>s;
        string str1,str2,str3;
        str1="";
        str2="";
        str3="";
        for(int i=0;i<n;++i)
        {
            if(i%3==0)
            {
                str1.pb('R');
            }else if((i%3)==1)
            {
                str1.pb('G');
            }
            else
            {
                str1.pb('B');
            }
        }
        for(int i=0;i<n;++i)
        {
            if(i%3==0)
            {
                str2.pb('G');
            }else if((i%3)==1)
            {
                str2.pb('B');
            }
            else
            {
                str2.pb('R');
            }
        }
        for(int i=0;i<n;++i)
        {
            if(i%3==0)
            {
                str3.pb('B');
            }else if((i%3)==1)
            {
                str3.pb('R');
            }
            else
            {
                str3.pb('G');
            }
        }
        int ans=inf;
        rep(i,0,n)
        {
            if(s[i]!=str1[i])
            {
                a[i]=1;
            }else
            {
                a[i]=0;
            }
        }
        sum[0]=a[0];
        rep(i,1,n)
        {
            sum[i]=sum[i-1]+a[i];
        }
        for(int i=k-1;i<n;++i)
        {
            int temp=0;
            if(i-k>=0)
                temp=sum[i-k];
            ans=min(ans,sum[i]-temp);
        }
        rep(i,0,n)
        {
            if(s[i]!=str2[i])
            {
                a[i]=1;
            }else
            {
                a[i]=0;
            }
        }
        sum[0]=a[0];
        rep(i,1,n)
        {
            sum[i]=sum[i-1]+a[i];
        }
        for(int i=k-1;i<n;++i)
        {
            int temp=0;
            if(i-k>=0)
                temp=sum[i-k];
            ans=min(ans,sum[i]-temp);
        }
        rep(i,0,n)
        {
            if(s[i]!=str3[i])
            {
                a[i]=1;
            }else
            {
                a[i]=0;
            }
        }
        sum[0]=a[0];
        rep(i,1,n)
        {
            sum[i]=sum[i-1]+a[i];
        }
        for(int i=k-1;i<n;++i)
        {
            int temp=0;
            if(i-k>=0)
                temp=sum[i-k];
            ans=min(ans,sum[i]-temp);
        }
        cout<<ans<<endl;
    }
 
 
    return 0;
}
 
inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}

Guess you like

Origin www.cnblogs.com/qieqiemin/p/11247870.html