EOJ 1854 【枚举】 【模拟】

/*题目链接*/

题意:有一串由“r”、“b”、“w”构成的环形项链,“r”代表红色,“b”代表蓝色,“w”可以代表红蓝两色。现在需要把该项链断开,问断开后断点两侧相同颜色的珠子加起来最多有多少个。“bwwwbbw”、“rrrr”都是连续颜色的珠子,断点左侧的珠子允许和右侧的珠子颜色不同。

题目分析:

因为珠子的个数最多之后350个,所以我们可以枚举断点的位置,然后分别向两侧扩展,将能扩展出的珠子数和最大值比较,更新最大值,最后输出答案。
扩展时注意点:1、向左扩展时,当位于下标0处,再扩展应该扩展下标为n-1的珠子,向右同理。(项链是环形的)
    2、“w”既可以当蓝色也可以当红色,但它本身不是一种颜色,所以只需考虑红色、蓝色的情况。

AC码:


#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
 
using namespace std;
 
int down(int i,int n)
{
    return (i-1+n)%n;
}
 
int up(int i,int n)
{
    return (i+1+n)%n;
}
 
bool check(int i, char ch, string &s)
{
    if (ch == 'w' || s[i] == 'w')
        return true;
    if (s[i] != ch)
        return false;
    return true;
}
int main()
{
    int n,ans=0;
    string s;
    cin >> n >> s;
    for (int i=0; i<n; ++i)
    {
        bool a[400] = {0};
        int j = down(i,n),ans1=0,ans2=0,ch = 'w';
        while (!a[j])
            if (j==down(i,n) || check(j,ch,s))
            {
                ++ans1;
                a[j] = true;
                if (s[j] != 'w')
                    ch = s[j];
                j = down(j,n);
            }
            else
                break;
        j = i;
        ch = 'w';
        while (!a[j])
            if (j==i || check(j,ch,s))
            {
                ++ans2;
                a[j] = true;
                if (s[j] != 'w')
                    ch = s[j];
                j = up(j,n);
            }
            else
                break;
        ans = max(ans,ans1+ans2);
    }
    cout << ans << endl;
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值