Broken Necklace

it2022-05-05  165

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.

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

29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT

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

SAMPLE OUTPUT

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

题意理解:

这道题看的我起初是有点懵的,不知道到底是要得到一个什么值,当然可能是英语能力的问题。后面研究它的8是如何得来的,就明白了,大概的意思是这样:有一串串了N颗珠子(其中颜色有红、蓝、白)项链,要从一个地方剪开这条项链变成一条直链,从直链的两端开始收集珠子,白色珠子可以被视为是红色或者蓝色的,遇到不同颜色的就停止,最后把两边所得的值加起来,就是收集的总珠子数目,求这个数的最大值。

设计算法:

起初还没看EXPLANATION的想法是这道题就等同于不剪开项链求其中一段(该段只有红、蓝、白两种颜色,且红、蓝只有一个分割处)的珠子数的最大值,但是想来并没有什么可以表示一个环的办法,后面不经意看到了解释,确实是个妙计,把一个直链再加上它本身就可以达到遍历环的效果了,然后直接暴力搜索。

C++编写:

原始代码(能过,稍微有点复杂):

/* ID: your_id_here TASK: beads LANG: C++ */ #include<iostream> #include<cstdio> #include<string> #include<algorithm> using namespace std; int solve(int n,string a) { a+=a; int max_len=1; for(int i=0;i<n;i++) { int max1=1; char t=a[i]; if(t == 'w') { int rec=0; for(int j=i+1;j<n+i;j++) { if(a[j] == t || a[j] == 'w') { max1++; } else { rec++; if(rec<3) { max1++; t=a[j]; } else break; } } } else { int rec=0; for(int k=i+1;k<n+i;k++) { if(a[k] == t || a[k] == 'w') { max1++; } else { rec++; if(rec<2) { max1++; t=a[k]; } else break; } } } max_len=max(max_len,max1); } cout<<max_len<<endl; } int main() { freopen("beads.in","r",stdin); freopen("beads.out","w",stdout); int N; string necklace; cin>>N>>necklace; solve(N,necklace); return 0; }

之后去看了一下别人的代码,确实是可以优化的:

/* ID: your_id_here TASK: beads LANG: C++ */ #include<iostream> #include<cstdio> #include<string> #include<algorithm> using namespace std; int solve(int n,string a) { int max_len=0,count,rec; a+=a; for(int i=0;i<n;i++) { char t=a[i]; if(t=='w') rec=0; else rec=1; count=1; int j=i+1; while(rec<3) { while(j<n+i && (a[j] == t || a[j] == 'w')) { count++; j++; } rec++; t=a[j]; } max_len=max(max_len,count); } cout<<max_len<<endl; } int main() { freopen("beads.in","r",stdin); freopen("beads.out","w",stdout); int N; string necklace; cin>>N>>necklace; solve(N,necklace); return 0; }

最新回复(0)