usaco1.1.4 Broken Necklace

[USACO 1.1.4] Broken necklace 解答 题目: 你有一条由 N 个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里 是 n=29 的二个例子 : 1 2 1 2 r b b r b r r b r b b b r r 阅读详情

一. 原题

Broken Necklace

You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:

                1 2                               1 2
            r b b r                           b r r b
          r         b                       b         b
         r           r                     b           r
        r             r                   w             r
       b               r                 w               w
      b                 b               r                 r
      b                 b               b                 b
      b                 b               r                 b
       r               r                 b               r
        b             r                   r             r
         b           r                     r           r
           r       r                         r       b
             r b r                             r r w
            Figure A                         Figure B
                        r red bead
                        b blue bead
                        w white bead

The beads considered first and second in the text that follows have been marked in the picture.

The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).

Determine the point where the necklace should be broken so that the most number of beads can be collected.

Example

For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.

In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration can include any of the three symbols r, b and w.

Write a program to determine the largest number of beads that can be collected from a supplied necklace.

PROGRAM NAME: beads

INPUT FORMAT

Line 1:N, the number of beads
Line 2:a string of N characters, each of which is r, b, or w

SAMPLE INPUT (file beads.in)

29
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT

A single line containing the maximum of number of beads that can be collected from the supplied necklace.

SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION

Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
                Two necklace copies joined here
                             v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                       ******|*****
                       rrrrrb|bbbbb  <-- assignments
                   5xr .....#|#####  6xb

                        5+6 = 11 total

二. 分析:

数据范围只有350,枚举每个位置作为项链断开位置就行了。有个小技巧题目在OUTPUT EXPLAINATION部分也讲的很清楚了。把输入字符串复制一份放在末尾,这样处理题目里项链的环结构比较方便。


三. 代码:

运行结果:
USER: Qi Shen [maxkibb3]
TASK: beads
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4180 KB]
   Test 2: TEST OK [0.000 secs, 4180 KB]
   Test 3: TEST OK [0.000 secs, 4180 KB]
   Test 4: TEST OK [0.000 secs, 4180 KB]
   Test 5: TEST OK [0.000 secs, 4180 KB]
   Test 6: TEST OK [0.000 secs, 4180 KB]
   Test 7: TEST OK [0.000 secs, 4180 KB]
   Test 8: TEST OK [0.000 secs, 4180 KB]
   Test 9: TEST OK [0.000 secs, 4180 KB]

All tests OK.

YOUR PROGRAM ('beads') WORKED FIRST TIME! That's fantastic -- and a rare thing. Please accept these special automated congratulations.


AC代码:
/*
ID:maxkibb3
PROG:beads
LANG:C++
*/

#include<cstdio>
#include<cstring>
using namespace std;

int n;
char s[400], ss[800];
int ans = 2;

int forward(int idx, int delta) {
    int flag = 0; // 0: uncertain, 1: red, 2: blue
    int ret = 0;
    for(int i = idx; i > 0 && i < n * 2; i += delta) {
        if(flag == 0) {
            if(ss[i] == 'r') {
                flag = 1;
            }
            else if(ss[i] == 'b') {
                flag = 2;
            }
            ret++;
        }
        else if(flag == 1) {
            if(ss[i] == 'b') {
                return ret;
            } 
            else {
                ret++;
            }
        }
        else {
            if(ss[i] == 'r') {
                return ret;
            }
            else {
                ret++;
            }
        }
    }
    return ret;
}

int main() {
    freopen("beads.in", "r", stdin);
    freopen("beads.out", "w", stdout);
    scanf("%d", &n);
    getchar();
    scanf("%s", s);
    strcpy(ss, s);
    strcpy(ss + n, s);
    for(int i = 0; i < n; i++) {
        int r = forward(i + 1, 1);
        int l = forward(i + n, -1);
        if(r + l >= n) {
            ans = n;
            break;
        }
        else if(r + l > ans) {
            ans = r + l;
        }
    }
    printf("%d\n", ans);
    return 0;
}
Usaco 1.1.4 破碎的项链(Broken Necklace) 题目:  破碎的项链 来源:  Usaco1.1.4 题目大意:  一个长度为N,由w、r、b字母组成的长链,w=r或者b,求从哪里断开可以向左向右得  到最长的一条有相同字母组成的链 数据范围: 阅读详情

相关推荐

USACO1.1.4 Broken Necklace(破碎的项链)

You have a necklace of N red, white, or blue beads (3 1 2 1 2 r b b r b r r b r b

九天神龍 1339

USACO 1.1.4 Broken Necklace(模拟)

做这题做了一个下午,都快崩溃了。说下要点: 1、要注意'w'这种特殊情况,到某位置时,注意左边、右边是'w'的情况。 2、得把整个项链看出循环队列,所以指针移动时注意mod。 3、我用的还是暴力法,O(n^2),看解题报告有O(n)的算法,用的是DP,还有待研究。 源程序 #define _CRT_SECURE_NO_WARNINGS #include &lt;iostream&...

编程语言小筑 196

【USACO题库】1.1.4 Broken Necklace破碎的项链

题目描述: 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3 1 2 1 2 r b b r b r r b r b b

Hello,I'm Proking. 2290

[USACO1.1.4]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 标签 搜索/枚举 USACO 难度 普及- 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的。 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号。 图片 A 中的项链可以用下面的...

weixin_30552811的博客 235

Broken Necklace(参考解析)

很容易看懂的usaco上的代码实现。希望对大家有帮助

ROS 2 Slam Toolbox实战:从零跑通2D激光建图

SLAM(同步定位与地图构建)是移动机器人实现自主导航的基础技术,其核心在于通过传感器数据实时估计位姿并构建环境栅格地图。slam_toolbox作为ROS 2 Humble及后续LTS版本官方主推的2D SLAM后端,采用图优化+闭环检测架构,兼顾实时性、鲁棒性与工程可维护性。相比cartographer和hector_slam,它原生支持ROS 2、参数精简、诊断友好,特别适合硬件资源有限、缺乏IMU或深度相机的差速机器人平台。本文聚焦真实部署场景,覆盖RPLIDAR等常见2D激光雷达与TurtleBo

weixin_30596165的博客 357

USACO 1.1.4 Broken Necklace

Broken Necklace You have a necklace of N red, white, or blue beads (3 1 2 1 2 r b b r b r r b r

XMzhou的专栏 746

【信息安全】seed-labs实验-Firewall Exploration Lab

本实验主要就是基于LKM和Netfilter实现简单的防火墙,重点是后者。路由器是有两个ip的,一个是外网ip,一个是内网ip每一次做完实验,都要做好收尾工作(比如:模块装入之后,要记得移除;iptables命令设置完规则之后,要把对应规则删掉)

可乐大牛的博客 5995

USACO 1.1 Broken Necklace(USACO官方)

关于USACO 1.1的broken necklace题目的相关分析

芝兰生于深谷,不以无人而不芳 2939

小白学电路之cadance搭建buffer电路

Buffer(缓冲器)是一种放大器或驱动电路,主要用于隔离输入和输出,以防止负载对前级电路造成干扰或影响。本文作者在电路方面是小白水平,目前正在学习,有不足之处欢迎指正。3.创建新的cellview,对此buffer进行仿真验证。2.创建buffer的symbol。电路中的V2的详细参数。

weixin_58051657的博客 1984

Android HTTPS抓包证书配置全解:Proxyman实战避坑指南

HTTPS抓包本质是中间人(MITM)通信的可信建立过程,其核心在于客户端对代理根证书的信任链配置。在Android平台,自7.0起系统默认不信任用户安装的CA证书,必须通过Network Security Config显式声明trust-anchors,将Proxyman导出的PEM格式根证书纳入App级信任锚点。这一机制区别于iOS和桌面浏览器,导致大量开发者卡在‘证书已安装却无法解密’的误区。技术价值在于打通开发调试闭环:精准定位SSLHandshakeException、绕过证书固定、支持React

weixin_32175665的博客 649

USACO1.1.4 Broken Necklace 破碎的项链

一.题目要求 Broken Necklace You have a necklace of N red, white, or blue beads (3 1 2 1 2 r b b r b r r b

ge_rui_jun的专栏 1604

2026年运维工程师常见面试选择题

这篇文章总结了30道Linux运维面试常见问题及解析,涵盖基础命令、系统监控、故障排查、数据库、缓存、容器编排等多个方面。主要内容包括: 基础命令使用:如df查看磁盘、free查看内存、top监控进程等 系统监控与故障排查:负载判断、端口检查、进程管理等 中间件相关:Nginx状态码、MySQL主从复制、Redis缓存问题 容器技术:Docker优势、Kubernetes作用 运维理念:高可用、监控告警、发布策略、备份容灾 工作原则:故障处理优先级、运维工程师核心能力 文章不仅提供标准答案,还解析了各命令的

不务正业的猿 193

USACO - 1.1.4 - Broken Necklace

USACO - 1.1.4 - Broken Necklace 题目很好懂,思路也很清晰,不就是暴力找断点然后正反各跑一遍,但是这个白色珠子的设定卡了我一上午,连样例都没有过,后来去参考别人的代码,发现了一种已经被我遗忘的处理方法,就是把字符串再加一遍,这样就比我之前写的这个题的代码要更好操作了 有一些细节,需要进行处理,比如如果是白色珠子开头或者为结尾,就要让它变成最近的珠子的颜色,这样才能保证从头到尾或者从尾部到头,尽可能的有最大连续相同颜色的珠子 大部分解释,都以注释的形式写在代码里了 #includ

CUCUC1的博客 222

YOLOv12改进策略【独家融合改进】| SPD-Conv+PPA 再次提升模型针对小目标的特征提取能力

SPD-Conv是一种新的 CNN 构建模块,用于替代传统 CNN 架构中使用的步长卷积(strided convolution)和池化(pooling)层,它由空间到深度(Space-to-depth,SPD)层和非步长卷积(non - strided convolution)层组成。

Limiiiing的博客 1027

无迹卡尔曼滤波 (UKF, Unscented Kalman Filter) 算法详解及案例分析

无迹卡尔曼滤波是一种用于估计非线性系统状态的递归算法。其目标是通过结合系统模型和观测数据,估计系统的最优状态。

闲人编程的博客 2625

USACO 1.1.4 坏掉的项链Broken Necklace 【模拟】

原题链接传送门 其实 USACO1.1USACO1.1USACO1.1 都是模拟呀! CODE: #include<iostream> #include<cstdio> using namespace std; int n,l[1110],r[1110],ans; char a[1110]; int main() { cin>>n; gets...

JA_yichao欢迎你 cnblogs账号:https://www.cnblogs.com/mayichao/ 324

基于STM32的智能快递柜设计(全部资料)

本文设计了一种基于STM32F407ZGT6的智能快递柜系统,通过模块化硬件设计和软件算法优化,实现了24小时无人值守、温湿度智能调控、远程监控等功能。系统集成OLED显示、GSM通信、舵机控制、超声波检测等模块,存取响应时间≤3秒,识别准确率达99.2%,较传统模式效率提升60%。测试表明系统运行稳定,用户满意度达92%,具有成本低、安全性高、可扩展性强等特点,为智慧物流末端配送提供了创新解决方案。未来可引入AI算法、太阳能供电等功能进一步优化系统性能。

3254968421的博客 585

usaco1.1.4 Broken Necklace 题解

【算法】模拟  【难度】★☆☆☆☆ 仍然是模拟,但要注意这个项链是环状的。有一种比较先进的办法是把这个项链载入两次,这样就可以从头到尾遍历了。注意白色的珠子不能作为起点。我的程序没有用上面的方法,直接遍历到最后在返回开头。 View Code 1 /* 2 ID: wsc5001 3 LANG: C 4 TASK: beads 5 */ 6 #include<...

aigai2119的博客 178

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

1.1.4 USACO Broken Necklace

https://train.usaco.org/usacoprob2?a=dGHkTLUWEXj&S=beads Broken Necklace You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white...

yanyanwenmeng的博客 626

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

[USACO 1.1.4] Broken Necklace

[题目描述] Broken Necklace 破碎的项链 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3,珠子是随意安排的。 这里是 n=29的二个例子: 第一和第二个珠子在图片中已经被作记号。  图片 A 中的项链可以用下面的字符串表示:  brbrrrbbbrrrrrbrrbbrbbbbrrrrb .  假如你要在一些点打破项链,展开成一条直线,然后从一端开

_Nirvana 442

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

【USACO1.1_4】★Broken Necklace 破碎的项链

/* ID: letgoto1 PROG: beads LANG: C++ */ #include #include #include using namespace std; int main() { ofstream fout("beads.out"); ifstream fin ("beads.in"); string str = ""; unsigne

zwcwu31的专栏 2079
下一篇: usaco1.2.1 Milking Cows
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值