Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version)

Portal

Meaning of the questions:

To give you a length only by n 'R', 'G', 'B' string s configuration, you need to find out which a substring. This makes the substring "RGBRGBRGBRGB ........ (in RGB circulatory section, we call this as the string str)" which is a sub-string, the substring of length k

But there may not be found in the string s, so this time we can change some of the letters in the string s position to complete the task. Q. least need to change how many letters

 

answer:

Mainly to see the violence of posture right. On a top of D1, I s is the position of each character string is' R & lt ', enumeration' G ',' B ', as if the substring is substring of str, then certainly is' GB ',' B ', "RGB" after the opening of the three RGB are

. 1  IF (S [I] =! ' G ' ) SUM ++; // , the printf ( ". 1 \ n-"); 
2      IF (S [I + . 1 ] =! ' B ' ) SUM ++; // , the printf ( "2 \ n-"); 
. 3  for ( int k = 0 , I = J + 2 ; J <I + m; J ++, ++ k, k% = . 3 )
 . 4  // because the string str is RGB loop, so that k is now represented R, G, B in whom 
. 5  
. 6  IF (S [I] =! ' B ' SUM ++); // , the printf ( ". 6 \ n-");
 . 7                  // IF (S [I +. 1 !] = 'B') sum ++;
 8                 for(int k=0,j=i+1; j<i+m; ++j,k++,k%=3)
 9 
10 
11 
12 //if(s[i]!='B') sum++,printf("6\n");
13                 //if(s[i+1]!='B') sum++;
14                 for(int k=0,j=i; j<i+m; ++j,k++,k%=3)

Is such a position to enumerate each string s, then the first letter enumerated beginning ( 'R', 'G', 'B')

But the T T_T

T Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<map>
 6 #include<math.h>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn=2e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
13 char s[maxn];
14 int main()
15 {
16     int t;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int n,m,ans=INF,sum;
21         scanf("%d%d",&n,&m);
22         scanf("%s",s);
23         if(m==1)
24         {
25             printf("0\n");
26             continue;
27         }
28         for(int i=0; i<n; ++i)
29         {
30             if(i+m<=n)
31             {
32                 sum=0;
33                 if(s[i]!='G') sum++;//,printf("1\n");
34                 if(s[i+1]!='B') sum++;//,printf("2\n");
35                 for(int k=0,j=i+2; j<i+m; ++j,k++,k%=3)
36                 {
37                     if(k==0)
38                     {
39                         if(s[j]!='R') ++sum;//,printf("3\n");
40                     }
41                     else if(k==1)
42                     {
43                         if(s[j]!='G') ++sum;//,printf("4\n");
44                     }
45                     else if(k==2)
46                     {
47                         if(s[j]!='B') ++sum;//,printf("5\n");
48                     }
49                 }
50                 ans=min(sum,ans);
51                 sum=0;
52                 if(s[i]!='B') sum++;//,printf("6\n");
53                 //if(s[i+1]!='B') sum++;
54                 for(int k=0,j=i+1; j<i+m; ++j,k++,k%=3)
55                 {
56                     if(k==0)
57                     {
58                         if(s[j]!='R') ++sum;//,printf("7\n");
59                     }
60                     else if(k==1)
61                     {
62                         if(s[j]!='G') ++sum;//,printf("8\n");
63                     }
64                     else if(k==2)
65                     {
66                         if(s[j]!='B') ++sum;//,printf("9\n");
67                     }
68                 }
69                 ans=min(sum,ans);
70                 //printf("%d %d\n",sum,i);
71                 sum=0;
72                 //if(s[i]!='B') sum++,printf("6\n");
73                 //if(s[i+1]!='B') sum++;
74                 for(int k=0,j=i; j<i+m; ++j,k++,k%=3)
75                 {
76                     if(k==0)
77                     {
78                         if(s[j]!='R') ++sum;//,printf("77\n");
79                     }
80                     else if(k==1)
81                     {
82                         if(s[j]!='G') ++sum;//,printf("88\n");
83                     }
84                     else if(k==2)
85                     {
86                         if(s[j]!='B') ++sum;//,printf("99\n");
87                     }
88                 }
89                 ans=min(sum,ans);
90             }
91             else
92             {
93                 break;
94             }
95         }
96         printf("%d\n",ans);
97     }
98     return 0;
99 }
View Code

 

In addition to this, but also you can enumerate the first k (beginning of the first letter), and then enumerate the location of each of the string s

This also includes all the cases, specific look at the code

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<map>
 6 #include<math.h>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn=2e5+5;
10 const int mod=26;
11 const int INF=0x3f3f3f3f;
12 const int block=300;
 13 is  char S [MAXN], NUM [MAXN];
 14  int Check ( int Status, int position)   // is matched to look at this position is unsuccessful, or need to change the position of the character 
15  {
 16      IF (Status = = 0 && S [position] == ' R & lt ' )
 . 17          return  0 ;
 18 is      the else  IF (Status == . 1 && S [position] == ' G ' )
 . 19          return  0 ;
 20 is      the else  IF (Status == 2 && S [position] =='B')
21         return 0;
22     return 1;
23 }
24 int main()
25 {
26     int t;
27     scanf("%d",&t);
28     while(t--)
29     {
30         int n,m,ans=INF,sum;
31         scanf("%d%d",&n,&m);
32         scanf("%s",s);
33 is          IF (m == . 1 )
 34 is          {
 35              the printf ( " 0 \ n- " );
 36              Continue ;
 37 [          }
 38 is          for ( int Status = 0 ; Status < . 3 ; Status ++) // begin enumeration first letter 
39          {
 40              SUM = 0 ;
 41 is              Memset (NUM, 0 , the sizeof (NUM));
 42 is              for ( int I = . 1 ; I <= n-; I ++)  // here and on a TLE code is not the same, here is an enumeration to the n- 
43              {    // the only way to be included in all cases when the status changes, you can practice it! 
44 is                  NUM [I] = Check ((Status + I- . 1 )% . 3 , I- . 1 );
 45                  SUM = + NUM [I];
 46 is                  IF (I> = m)
 47                      sum- NUM = [I- m];  
 48                  IF (I> = m)
 49                      ANS = min (ANS, SUM);
 50              }
 51 is          }
 52 is          the printf ( " % D \ n- " , ANS);
53     }
54     return 0;
55 }
View Code

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/11529817.html