D2. RGB Substring (hard version)

 https://codeforc.es/contest/1196/problem/D2

思n路:3种情况:RGBRGB....   GBRGBR....  BRGBRG...就是枚举开头;与原串比较哪里换了,换了贡献就是1,然后计算前缀和,枚举区间取最值;

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%lld",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define pii pair
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define zero(x) (((x)>0?(x):-(x))
#include
#include 
using namespace __gnu_pbds;
*/

//gp_hash_tablemp2;
//__gnu_pbds::priority_queueq;//因为放置和std重复,故需要带上命名空间
//__gnu_pbds::priority_queue,pairing_heap_tag> pq;//最快
//----------------------------------------------------------


/*
//----------------------------------------------------------
const int BufferSize = 1 << 16;
char buffer[BufferSize], *_head, *_tail;
inline char Getchar() {
    if (_head == _tail) {
        int l = fread(buffer, 1, BufferSize, stdin);
        _tail = (_head = buffer) + l;
    }
    return *_head++;
 }
inline int read() {
    int x = 0, f = 1;char c = Getchar();
    for (;!isdigit(c);c = Getchar()) if (c == '-') f = -1;
    for (;isdigit(c);c = Getchar()) x = x * 10 + c - '0';
    return x * f;
 }
//----------------------------------------------------------
*/


const int maxn=1e6+9;
const int maxm=4e5+9;
const int mod=1e9+7;

int sum[3][maxn];
int main()
{

    int q;
    cin>>q;
    while(q--)
    {

        int n;
        int k;
        string s;
        cin>>n>>k>>s;
        int ans=INF;
        string t="RGB";

        sum[0][0]=sum[1][0]=sum[2][0]=0;
        for(int d=0;d<3;d++)
        {
            for(int i=1;i<=n;i++)
            {
                if(s[i-1]!=t[(d+i-1)%3])
                    sum[d][i]=sum[d][i-1]+1;
                else sum[d][i]=sum[d][i-1];
                //cout<

 

你可能感兴趣的:(思维)