题解:P15629 [2019 KAIST RUN Spring] Rainbow Beads
NoneOfTheAbove · · 题解
link
Solution
对于以第 V,那么就要重组字符串,长度为
上面已经讲了 DP 转移式,按上面模拟并在过程中取最大值,最大值就是答案。
Code
:::success[code]
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef double db;
typedef __int128 i128;
const ll inf=9e18,maxn=2.5e5+1;
const i128 Inf=1e35;
ll n,ans[maxn],Ans;
str s;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
s=" "+s;
for(int i=1;i<=n;i++)
{
if(s[i]==s[i-1]) ans[i]=1;
else if(s[i]=='R'&&s[i-1]=='V') ans[i]=1;
else if(s[i]=='V'&&s[i-1]=='R') ans[i]=1;
else if(s[i]=='B'&&s[i-1]=='V') ans[i]=1;
else if(s[i]=='V'&&s[i-1]=='B') ans[i]=1;
else ans[i]=ans[i-1]+1;
Ans=max(Ans,ans[i]);
}
cout<<Ans;
}
:::