蓝桥杯算法提高:项链珠子收集优化.pdfVIP

  • 0
  • 0
  • 约2.85千字
  • 约 4页
  • 2026-02-09 发布于四川
  • 举报

蓝桥杯算法提高:项链珠子收集优化.pdf

算法提高项链

时间限制:1.0s内存限制:512.0MB

问题描述

由n(1≤n≤100)个珠子组成的一个项链,珠子有红、蓝、白三种颜色,

各种颜色的珠子的安排顺序由键盘输入的字符串任意给定。蓝色用小写字母b

表示,红色用小写字母r表示,白色用小写字母w表示.

假定从项链的某处将其剪断,把它摆成一条直线。先从左端向右收集同色珠

子,遇到第一个异色珠子时停止.收集过程中,白色是一种特殊颜色,既可

以看成红色也可以看成蓝色。然后再从剩余珠子的右端向左重复上述过程。

例如:对下图一所示的项链,如果从图一中标记的位置0处剪断,则按

顺时针顺序得到wbbbwwrrbwbrrwb(如图二所示)。这时从左端开始收集可以得

到wbbbww,共6个珠子;然后从剩余珠子右端开始收集得到wb,共2个珠

子。这种剪法共可收集到6+2=8个珠子。如果从图一中标记的位置4处剪断,

则按顺时针顺序得到wwrrbwbrrwbwbbb(如图二所示)。这时从左端收集可以得

到wwrr,共4个珠子;然后从剩余珠子右端收集可以得到wbwbbb,共6个珠子。

这种剪法共可收集到4+6=10个珠子。

要求:在项链中选择合适的剪断位置,使得从左右两端收集到的珠子数目

之和最大,输出收集到的珠子数的最大值M。

imgheight=500

src=

输入格式

由小写字母b,r,w组成的字符串。此字符串记录了一个首尾相接的项链从某

处断开后,按顺时针顺序得到的珠子的直线排列。

输出格式

收集到的珠子数的最大值M

样例输入

wbbbwwrrbwbrrwb

样例输出

10

AlgorithmImprovementNecklace

TimeLimit:1.0sMemoryLimit:512.0MB

问题描述

Anecklacecomposedofn(1≤n≤100)beads,wherethebeadscanbered,blue,orwhite.

Thearrangementofthecolorsisgivenbyanarbitrarystringinputfromthekeyboard.Blueis

representedbythelowercaseletterb,redbythelowercaseletterr,andwhitebythel

owercaseletterw.

ssumethtklaceiscutatsomepontanlaidoutinastraightline.Collectbeadsi

Aathenecd

ofthesamecolorfromlefttorightuntilthefirstdifferent-coloredbeadisencountered.During

thecollectionprocess,whiteisaspecialcolorthatcanbeconsideredeitherredorblue.Then,

repeattheprocessfromtherightendoftheremainingbeadstowardstheleft.

examle,focklaceshowninFigure1,ifitiscutatposition0markedinFigure

Forprthene

1,thebeadswillbearrangedinclockwiseorderaswbbbwwrrbwbrrwb(asshowninFigure2).

Startingfromtheleftend,6beads(wbbbww)canbecollected;then,startingfromtherightend

oftheremainingbeads,2beads(wb)canbecollected.Thiscuttingmethodallowsatotalof

6+2=8beadstobecollected

文档评论(0)

1亿VIP精品文档

相关文档