#575(DIV。3)D1ラウンドCodeforces。RGBのサブストリング(簡単バージョン)

#575(DIV。3)ラウンドCodeforces

 

D1 - RGBのサブストリング(簡単バージョン)

簡単とハードのバージョン間の唯一の違いは、入力のサイズです

もし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つの整数 - あなたはまた、無限の文字列の部分文字列であるSの長さkの部分文字列があるだろう変更した後、最初の文字列sとなるように変更する必要がある文字の最小数の「RGBRGBRGB ...」 。

入力

3

5 2

BGGGG

5 3

RBRGR

5 5

BBBRR

出力

1

0

3

注意

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

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

 

問題の意味:タイトルの意味はあなただけ「R」「B」と「文字のG」三種類の文字列を与えることで、

あなたは、少なくとも「RGBRGBRGB ...」部分文字列になるためにいくつかの文字を変更したい下さい。

思考:これが質問のD2の意味と同じ問題ですが、データは簡単です、それは「簡単にバージョン」であることから、我々が十分な暴力を直接明らかです。

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<queue>
 9 #include<algorithm>
10 using namespace std;
11 #define ll long long 
12 const int inf=1e9+7;
13 const int mod=1e9+7;
14  
15 const int maxn=1e5+5;
16  
17 char nextt[100];
18  
19 int jisuan(string str)
20 {
21     int minn=inf,ans;
22     char now;
23     
24     ans=0;
25     now='R';//从'R'开始匹配 
26     for(int i=0;i<str.size();i++)
27     {
28         if(str[i]!=now)
29             ans++;
30         
31         now=nextt[now];
32     }
33     if(ans<minn)
34         minn=ans;//刷新最小值 
35         
36     ans=0;
37     now='G';//从'G'开始匹配 
38     for(int i=0;i<str.size();i++)
39     {
40         if(str[i]!=now)
41             ans++;
42         
43         now=nextt[now];
44     }
45     if(ans<minn)
46         minn=ans;//刷新最小值 
47          
48     ans=0;
49     now='B';//从'B'开始匹配 
50     for(int i=0;i<str.size();i++)
51     {
52         if(str[i]!=now)
53             ans++;
54         
55         now=nextt[now];
56     }
57     if(ans<minn)//刷新最小值 
58         minn=ans;
59     
60     return minn;
61 }
62  
63 int main()
64 {
65     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
66     
67     nextt['R']='G';
68     nextt['G']='B';
69     nextt['B']='R';
70     
71     int T;
72     cin>>T;
73     int n,k,ans;
74     string str,now;
75     while(T--)
76     {
77         cin>>n>>k;
78         cin>>str;
79         
80         int minn=inf;
81         
82         for(int i=0;i<n-k+1;i++)
83         {
84             now=str.substr(i,k);//从字符串中依次截取k长度的子串 
85         //    cout<<now<<endl;
86             
87             ans=jisuan(now); 
88             
89             if(ans<minn)
90                 minn=ans;
91             
92         }
93         
94         cout<<minn<<endl;
95     }
96     
97     return 0;
98 }

 

这里放一下标程1写法,讲道理还是我写的比较容易懂,,,

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<map>
 6 #include<set>
 7 #include<vector>
 8 #include<algorithm>
 9 #include<queue>
10 #include<unordered_map>
11 using namespace std;
12 #define ll long long 
13 const int mod=1e9+7;
14 const int inf=1e9+7;
15  
16 //const int maxn=
17  
18 int main()
19 {
20     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
21     
22     const string t="RGB";
23     
24     int T;
25     cin>>T;
26     int n,k;
27     string str;
28     while(T--)
29     {
30         cin>>n>>k>>str;
31         
32         int minn=inf;
33         
34         for(int i=0;i<n-k+1;i++)
35         {
36             for(int now=0;now<3;now++)
37             {
38                 int cnt=0;
39                 for(int j=0;j<k;j++)
40                 {
41                     if(str[i+j]!=t[(j+now)%3])
42                         cnt++;
43                 }
44                 if(cnt<minn)
45                     minn=cnt;
46             }
47         }
48         
49         cout<<minn<<endl;
50         
51     }
52     
53     return 0;
54 }

 

おすすめ

転載: www.cnblogs.com/xwl3109377858/p/11271930.html