1.1.4 Broken Necklace
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1.1.4 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
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define maxn 1111 6 using namespace std; 7 int c; 8 char s[400]; 9 int len; 10 int mod(int n,int m) 11 { 12 while(n<0) n+=m; 13 return n%m;//跑一圈又回去 14 } 15 int nbreak(int p,int dir) 16 { 17 int n,i; 18 if(dir>0) i=p; 19 else i=mod(p-1,len); 20 char col=‘w‘; 21 for(n=0;n<len;i=mod(i+dir,len)){ 22 if(col==‘w‘&&s[i]!=‘w‘){ 23 col=s[i]; 24 } 25 else if(col!=s[i]&&s[i]!=‘w‘) 26 break; 27 n++; 28 } 29 return n; 30 } 31 int main() 32 { 33 freopen("beads.in","r",stdin); 34 freopen("beads.out","w",stdout); 35 cin>>c; 36 scanf("%s",s); 37 int m=0; 38 int n; 39 len=strlen(s); 40 for(int i=0;i<len;i++){ 41 int n=nbreak(i,1)+nbreak(i,-1);//分左右计数 42 m=max(m,n); 43 } 44 if(m>len) m=len; 45 cout<<m<<endl; 46 return 0; 47 }
以上是关于1.1.4 Broken Necklace的主要内容,如果未能解决你的问题,请参考以下文章
Broken Necklace 坏掉的项链 USACO 模拟(易错)