本文主要是介绍Codeforces Round #575 (Div. 3)D. RGB Substring (hard version),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:
http://codeforces.com/contest/1196/problem/D2
D2. RGB Substring (hard version)
The only difference between easy and hard versions is the size of the input.You are given a string ss consisting of nn characters, each character is 'R', 'G' or 'B'.
You are also given an integer kk. Your task is to change the minimum number of characters in the initial string ss so that after the changes there will be a string of length kk that is a substring of ss, and is also a substring of the infinite string "RGBRGBRGB ...".
A string aa is a substring of string bb if there exists a positive integer ii such that a1=bia1=bi, a2=bi+1a2=bi+1, a3=bi+2a3=bi+2, ..., a|a|=bi+|a|−1a|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 qq independent queries.
Input
The first line of the input contains one integer qq (1≤q≤2⋅1051≤q≤2⋅105) — the number of queries. Then qq queries follow.
The first line of the query contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the length of the string ss and the length of the substring.
The second line of the query contains a string ss consisting of nn characters 'R', 'G' and 'B'.
It is guaranteed that the sum of nn over all queries does not exceed 2⋅1052⋅105 (∑n≤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 ss so that after changing there will be a substring of length kk in ss that is also a substring of the infinite string "RGBRGBRGB ...".
Example
input
Copy
3 5 2 BGGGG 5 3 RBRGR 5 5 BBBRRoutput
Copy
1 0 3Note
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和一个标准串RGBRGBRGB……,s截取长度为k个子串str,将其子串str修改为标准串的子串代价的最小值?
分析:
前缀和的思想。
举个例子就明白了:
字符:BGGGGG
标准:BRGBRG
代价:01 0 11 0
循环对于修改长度为k子串: sum[i]-sum[i-k]
然后标准换为:RGBRGB、GBRGBR都循环一遍即可。
This is the code:
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long //1844674407370955161
#define INT_INF 0x3f3f3f3f //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]= {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]= {-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{int ret=0, flag=0;char ch;if((ch=getchar())=='-')flag=1;else if(ch>='0'&&ch<='9')ret = ch - '0';while((ch=getchar())>='0'&&ch<='9')ret=ret*10+(ch-'0');return flag ? -ret : ret;
}
const int maxn=500005;
char str[maxn];
char t[maxn];
int a[maxn],sum[maxn];
int main()
{int q;scanf("%d",&q);while(q--){int n,k;scanf("%d%d",&n,&k);scanf("%s",str+1);for(int i=1;i<=n+100;i+=3){t[i]='R';t[i+1]='G';t[i+2]='B';}int ans=1e18;for(int j=1;j<=3;j++){for(int i=1;i<=n;i++){if(t[i+j-1]!=str[i])a[i]=1;elsea[i]=0;}for(int i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];for(int i=k;i<=n;i++)ans=min(ans,sum[i]-sum[i-k]);}printf("%d\n",ans);}return 0;
}
这篇关于Codeforces Round #575 (Div. 3)D. RGB Substring (hard version)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!