【Gym 102202】 A - Rainbow Beads 規律 簽到題

Jaehyun has a bead which consists of N jewels arranged from left to right. Each jewel is in one of the three colors: Red, Blue, and Violet, which is represented as a single character R, B, V. As one of the committees in an important contest, Jaehyun wants to use it as a souvenir for some participant.

Jaehyun likes a bead with diverse colors, so he defines a bead beautiful if every adjacent jewel has different colors. For example, RBVBV is a beautiful bead because every adjacent jewel has a different color. V is a beautiful bead because it does not have adjacent pairs. However, RBBV is not a beautiful bead, because two Bs in the middle are adjacent in the string.

Not only Jaehyun likes a bead with diverse colors, but he likes a contest with diversity. This time, Jaehyun wants to make a bead that is also colorful to colorblind people. For convenience, we will only consider three kinds of people in this problem.

Non-colorblind people, who can tell all three colors.
Red-colorblind people (Protanopia), who can’t tell apart red and violet: They consider violet jewels as red jewels.
Blue-colorblind people (Tritanopia), who can’t tell apart blue and violet: They consider violet jewels as blue jewels.
In this case, the string RVB is colorful for non-colorblind people, but it is not colorful for red-colorblind people as red and violet jewels are adjacent, and it is also not colorful for blue-colorblind people as violet and blue jewels are adjacent.
Jaehyun wants to pick some contiguous part of the bead and cut it out to give as a souvenir. The part Jaehyun cuts should be colorful to all three kinds of people. Note that, if the whole bead is beautiful, then Jaehyun does not necessarily cut it out, but just give the whole bead. What is the length of the longest bead he can give?

Input
The first line contains an integer N, denoting the length of the bead. (1≤N≤250000)

The next line contains string of length N, where every character is either R, B, or V.

Output
Print the maximum possible length of contiguous beads, which is colorful for all three kinds of people.

Examples
Input
4
VRRB
Output
2
Input
5
RBBRR
Output
2

題意:找出字符串中最長的連續RB 或 BR 序列,V也可單一形成長度爲1的序列

思路:

找一遍連續RB和BR即可。然後如果有V單獨出現的再用最後結果和1取max。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 3e4+2;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll ans1 = 0 ; char cur = 'R';
    ll ans = 0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]==cur)
        ans1++,cur=(s[i]=='B'?'R':'B') ;
        else
        {
            ans = max(ans1,ans), cur='R',ans1=0;
            if(s[i]==cur)
            ans1++, cur=(s[i]=='B'?'R':'B') ;
        }
        ans = max(ans1,ans);
      // cout<<ans<<' '<<s[i]<<' '<<i<<' '<<cur<<endl;
    }
    ans = max(ans,ans1);
 //   cout<<ans<<endl;
    cur = 'B';ans1=0;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]==cur)
        ans1++,cur=(s[i]=='B'?'R':'B') ;
        else
        {
            ans = max(ans1,ans),cur='B',ans1=0;
            if(s[i]==cur)
            ans1++,cur=(s[i]=='B'?'R':'B') ;
        }

    }  ans = max(ans,ans1);
    ll cnt = 0;
     for(int i=0;i<s.size();i++)
     if(s[i]=='V')
     {
         cnt++;
         break;
     }
    ans = max(cnt,ans);
    cout<<ans<<endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章