D2. RGB Substring(hard version)(동적 계획)

4108 단어
[제목]
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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≤2⋅105) — the number of queries. Then q queries follow.
The first line of the query contains two integers n and k (1≤k≤n≤2⋅105) — 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 2⋅105 (∑n≤2⋅105).
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
input
Copy
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR

output
Copy
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".
[제목]
초기 문자열 s (RGB만 포함) 를 주고, S 문자열의 일부 문자를 어떻게 바꾸는지 물어보면, s 문자열 k 길이의 하위 문자열은 RGBRGBRGBRGB의 하위 문자열이다.
최소한의 변화 횟수를 구하다.
[문제풀이]
dp[i][0]는 s열이 RGB에서 순환하기 시작한다는 것을 의미한다.
dp[i][1]은 s열이 GBR에서 순환하기 시작한다는 것을 의미한다.
dp[i][2]는 s열이 BRG에서 순환하기 시작한다는 것을 의미한다.
분명히
for(int i=1;i<=n;++i)
        {
            dp[i][0]=dp[i-1][0]+1;
            if(s[i]==t0[id]) dp[i][0]--;

            dp[i][1]=dp[i-1][1]+1;
            if(s[i]==t1[id]) dp[i][1]--;

            dp[i][2]=dp[i-1][2]+1;
            if(s[i]==t2[id]) dp[i][2]--;
            id=(id+1)%3;
        }

그러면 정답은 K 길이의 최소 변화를 매거하는 거예요.
for(int i=0;i+k<=n;++i)
        {
            for(int j=0;j<=2;++j)
            {
                ans=min(ans,dp[i+k][j]-dp[i][j]);
            }
        }

【코드】
#include 
using namespace std;
const int N=2e5+10;
int dp[N][3];
char s[N];
char t0[10]="RGB",t1[10]="GBR",t2[10]="BRG";

int main()
{
    int _;
    cin>>_;
    int n,k;
    while(_--)
    {
        scanf("%d%d",&n,&k);
        scanf("%s",s+1);
        //memset(dp,0,sizeof(dp));
        if(k==1)
        {
            printf("0
"); continue; } int id=0; for(int i=1;i<=n;++i) { dp[i][0]=dp[i-1][0]+1; if(s[i]==t0[id]) dp[i][0]--; dp[i][1]=dp[i-1][1]+1; if(s[i]==t1[id]) dp[i][1]--; dp[i][2]=dp[i-1][2]+1; if(s[i]==t2[id]) dp[i][2]--; id=(id+1)%3; } int ans=n; for(int i=0;i+k<=n;++i) { for(int j=0;j<=2;++j) { ans=min(ans,dp[i+k][j]-dp[i][j]); } } printf("%d
",ans); } }

좋은 웹페이지 즐겨찾기