跳到正文

题解

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

NoneOfTheAbove约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年3月6日
收录
2026年3月7日
更新
2026年11月18日
篇幅
约 2 分钟
获赞
2
题目
P15629

版本

  1. v2当前
  2. v1

link

Solution

对于以第 ii 个字符结尾的最长色彩丰富串,如果第 ii 个字符和第 i−1i-1 个字符一样或有一个是 V,那么就要重组字符串,长度为 11,否则长度为以第 i−1i-1 个字符结尾的最长色彩丰富串加一,所以考虑 DP。

上面已经讲了 DP 转移式,按上面模拟并在过程中取最大值,最大值就是答案。

Code

code
C++
#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;
}
原文
存档评论4 条抓取于