题解:P15629 [2019 KAIST RUN Spring] Rainbow Beads

· · 题解

题目思路

我们需要找到串珠中最长的连续子串,使得它对三种人来说都符合相邻字符不同。\ 对于非色盲,任意相邻两颗颜色不同。\ 对于红色盲, \texttt{V} 被视为 \texttt{R}。\ 对于蓝色盲, \texttt{V} 被视为 \texttt{B}。\ 这等价于字串中任意相邻两颗不能是 \texttt{RR}\texttt{BB}\texttt{VV}\texttt{RV}\texttt{VR}\texttt{BV}\texttt{VB}。\ 所以我们可以得出,合法的相邻颜色对只有 \texttt{BR}\texttt{RB}。 :::success[AC Code]

#include<bits/stdc++.h>
using namespace std;
int n,ma=1,tmp=1;
string s; 
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>s;
    for(int i=1;i<n;i++)
    {
        if((s[i]=='R'&&s[i-1]=='B')||(s[i]=='B'&&s[i-1]=='R'))
        {
            tmp++;
            ma=max(ma,tmp);
        }
        else tmp=1;
    }
    cout<<ma;
    return 0;
}