#575ラウンドCodeforces(DIV。3)D1 + D2。RGBサブストリング(簡単バージョン)D2。RGBサブストリング(ハードバージョン)(思考、列挙、および接頭辞)

D1。RGBサブストリング(簡単バージョン)
TEST2の秒当たりの時間制限
test256メガバイト当たりのメモリ制限
入力inputstandard
出力outputstandard
容易とハードバージョン間の唯一の違いは、入力の大きさです。

もしn個の文字からなる文字列sを与えられて、各文字が「G」または「B」、「R」です。

また、整数kを与えています。あなたの仕事は、変更後sの部分であり、また無限の文字列の部分文字列である長さkの文字列が存在することになるように、最初の文字列s中の文字の最小数を変更することです「RGBRGBRGB ...」 。

| | = BI + | | -1私は、A1 = BI、A2 = BI + 1、A3 = BI + 2、...、それは正の整数が存在する場合、文字列Aは、B文字列の部分文字列です。たとえば、文字列「GBRG」、「B」、「BR」は無限の文字列の部分文字列です「RGBRGBRGB ...」「GR」一方では、「RGR」と「GGG」はありません。

あなたは、Qの独立したクエリに応答する必要があります。

入力
クエリの数-入力の最初の行は1つの整数Q(1≤q≤2000)を含みます。そして、q個のクエリが続きます。

文字列sの長さとサブストリングの長さ - クエリの最初の行は二つの整数nとk(1≤k≤n≤2000)を含みます。

クエリの2行目はn文字「R」、「G」及び「B」からなる文字列sを含んでいます。

すべてのクエリの上に、nの合計は2000(Σn≤2000)を超えないことが保証されています。

出力
各クエリのプリント1つの整数の場合-あなたはまた、無限の文字列「RGBRGBRGBのストリングであるSの長さkの部分文字列があるだろう変更した後、最初の文字列sとなるように変更する必要がある文字の最小数... 」。

例えば
inputCopy
3
5 2
BGGGG
5 3
RBRGR
5
BBBRR
outputCopy
1
0
3

最初の例では、「R」に最初の文字を変更して、サブストリング「RG」を得る、または「R」に第二の文字を変更することができると「BR」を得る、または「B」に第3、第4または第5文字を変更し、「GB」を得ます。

第2の例では、ストリングは「BRG」です。

質問の意味:
二つの質問が異なるデータ範囲なので、無限RGBRGBRGBで少なくとも変更は、文字列の長さkの部分文字列を作ることができますどのように多くの文字を尋ね、説明あなたの文字列とKを与えるために一緒に入れ*****長い部分文字列?
アイデア:

D1:時間の複雑さは、(N * K)を行うOには明らかであろう

我々は最初のR、Gまでの3つのRGB文字列の長さkを構築し、Bが開始されています。

次いでOに移動(N k)はすべての文字列の長さkの連続するサブストリングO(3列挙文字サブストリング三GBRの列を走査K)異なる数を、

さまざまなトピックの最小数が答えです維持します。

D2:O(N + K)の時間計算量が許容されます。

それでは、3つのRGBの長さnの文字列なので、各試合を構築し、文字列strを続けましょう。

我々は1と異なる[i]と異なるのi番目の文字列RGBの文字列を、維持する場合、一致は、0は同じです。

次いで、[i]と、プレフィックス、及び長さkの文字列の処理上O(N)スイープとRGB少なくとも文字列の異なる値の数を見つけます。

2つのコード:最初はD1、D2が第二であります

詳細コードを参照してください。

#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';
        }
    }
}

おすすめ

転載: www.cnblogs.com/qieqiemin/p/11247870.html