第一层第四题:打断项链

时间:2022-09-08 11:24:54

Broken Necklace

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

                12                               1 2

            r b br                           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 br                             r r w

            FigureA                         Figure B

                       r red bead

                       b blue bead

                       w white bead

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

The configuration in Figure A may be represented as a string of b's andr'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 ofa different color, and do the same for the other end (which might not be of thesame color as the beads collected before this).

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

Example

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

In some necklaces, white beads had been included as shown in Figure Babove. When collecting beads,a white bead that is encountered may betreated as either red or blue and then painted with the desired color. Thestring that represents this configuration can include any of the three symbolsr, b and w.

Write a program to determine the largest number of beads that can becollected 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 beadsthat 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 ableto runaround the ends). The string of 11 is marked.

                Twonecklace copies joined here

                             v

wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

                      ******|*****

                       rrrrrb|bbbbb  <-- assignments

                  5xr .....#|#####  6xb

 

                       5+6 = 11 total

 

       题目大意是有一条项链,上面的每个珠子从1到n编号,每个珠子有w(White,白色)、r(Red,红色)、b(Blue,蓝色),你可以从其中某两个珠子间打断这条项链,这样子项链就变成了一条直线(或者说字符串更贴切些),那么我们从这条直线的两端中的一端开始拿珠子,拿走的珠子的颜色必须与这一端最开始的珠子的颜色相同(白色可以被视为任意颜色,若这一端开始的颜色为白色,则认为这一端被取走的珠子的颜色为白色的珠子向内遍历找到的第一颗有颜色的珠子,例如当一条项链被打断并拉直后为wwwrwwbrbbww,则认为从左端取走的珠子的颜色应为红色,则左端最多可以拿走wwwrww共6颗珠子)。那么问题来了:当项链两端都取的时候(珠子不可以重复取),这条项链最多能被取走几条珠子?(例如刚刚举出的例子中,左边最多可以取wwwrww共6颗珠子,右边可以取bbww共4颗珠子,则这条项链可以取走10颗珠子。而当项链拉直后是这种情况时:rrrrrr,这条项链只能被取走6颗珠子)。

       输入分两行,第一行是一个整数n,表示项链的长度,第二行是一个长度为n的字符串,由b,r,w构成,描述项链的颜色。

       (注意:项链的打断方式不一定要按照题目的来,例如:题目输入的字符串为wbrrb,你也可以将它打断为rbwbr,题目要求的本来就是问如何打断项链使能拿到的珠子尽可能多)。

       输出仅一个数字,为项链最多能够被拿走几颗珠子。

       不知道是楼主英语不好还是什么,USACO的题目向来不好看懂,楼主之前的解释也都是七零八落的。。。这题其实一般看懂算法就出来了。枚举从哪里打断项链,再模拟一遍取珠子的的过程,记录下取走的珠子的个数,取其中的最大值即可。

       这里有一个技巧,题目中也有讲,就是将表示项链的字符串复制一遍,接到原字符串后面,为什么呢?请诸位看官继续看。

第一层第四题:打断项链

       这是题目给出的字符串,我们将它复制一遍得到:

第一层第四题:打断项链

       这时当我们从项链的i处打断时(如图),打断后的项链即可表示为从i到i+n-1处的连续一段字符串。此时我们再模拟就简单多了。今后遇上有环形的题目第一个要考虑的就是这个。

第一层第四题:打断项链

       附上代码,供诸位批阅:

/*
ID:su1q2d21
LANG:C++
TASK:beads
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define maxlen 400*2
using namespace std;
char necklace[maxlen];
bool taken[maxlen];
char tmp[maxlen];
int main()
{
freopen("beads.in","r",stdin);
freopen("beads.out","w",stdout);
int n;
int i,j;
int ans;
int op;
int tans;
ans=0;
scanf("%d",&n);
scanf("%s",necklace);
strcpy(tmp,necklace);
strcat(necklace,tmp);
for(i=0;i<n;i++){
tans=0;
memset(taken,0,sizeof(taken));
op=i;
while(necklace[op]=='w') op++;
j=i;
while(j<i+n){
if(necklace[j]==necklace[op] || necklace[j]=='w'){
tans++;
taken[j]=true;
}
else break;
j++;
}
op=i+n-1;
while(necklace[op]=='w') op--;
j=i+n-1;
while(j>=i){
if((necklace[j]==necklace[op] || necklace[j]=='w') && !taken[j]){
tans++;
taken[j]=true;
}
else break;
j--;
}
ans=max(ans,tans);
}
printf("%d\n",ans);
return 0;
}