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 }
时间: 2024-10-12 16:26:40

1.1.4 Broken Necklace的相关文章

Section1.1 -- Broken Necklace

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

洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

USACO 1.1 Broken Necklace

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

P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

题解 P1203 【[USACO1.1]坏掉的项链Broken Necklace】

[USACO1.1]坏掉的项链Broken Necklace 22892 破碎的项链 方法一:很容易想到枚举断点,再分别两头找,但是要注意很多细节 #include<iostream> #include<string> #include<cstdio> using namespace std; string s; int n,l,r,ll,rr,tmp,ans; inline int calc(int x) { ll=s[x],rr=s[x+1],l=x-1,r=x+2

USACO section1.1 Broken Necklace

1 /* 2 ID: vincent63 3 LANG: C 4 TASK: beads 5 */ 6 #include <stdio.h> 7 #include<stdlib.h> 8 #include<string.h> 9 int findmax(char s[],int n){ 10 int i,j,count,max,l,r; 11 char left,right; 12 max=0; 13 count=0; 14 int p; 15 for(i=0,j=1;

USACO 1.1 Broken Necklace(USACO官方)

本题我不会写,囧,看了官方的代码,算法复杂度为O(n^2),学习下: /* ID:twd30651 PROG:beads LANG:C++ */ #include<iostream> #include<fstream> #include<string.h> using namespace std; int N; char s[400]; int len; int mod(int n,int m) { while(n<0)n+=m; return n%m;//值得学

Broken Necklace 坏掉的项链 USACO 模拟(易错)

1004: 1.1.4Broken Necklace 坏掉的项链 时间限制: 1 Sec  内存限制: 128 MB提交: 11  解决: 9[提交] [状态] [讨论版] [命题人:外部导入] 题目描述 1.1.4Broken Necklace 坏掉的项链 (beads.pas/c/cpp) 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 1 2 1 2 r b b r b r r b r b b b

USACO&lt;s1.1.4:Broken Necklace&gt;字符串

题意:一串环形的字符串代表一串项链,该字符串包含b,r,w,分别代表蓝色,红色,白色,白色可任意转换为红色或蓝色.问在哪一个点处打来项链的环,分别向左边和右边计算红色珠子串或蓝色珠子串,蓝红珠子数目总和的最大值为结果~ KEY:输入的字符串是直线型的,要把它模拟成环形,我起初的做法是用循环把数组一直往前移,这就相当于在环形里操作.还有一个做法是,strcat那段字符串,然后用mod运算也能模拟.这是我在这道题学到的最关键的~然后,就是很简单的问题了,if判断就行了.开始wa是有两处错,一是把n-