usaco 1.1.4 Broken Necklace 一道题可以犯多少脑残

sigh。。

这一定是老天对我好久没做题的惩罚。

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 will include 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.
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                       ****** *****
                       wwwwwb bbbbb  <-- assignments
                       5 x w  6 x b  <-- 11 total

wa了。。额,3次。

首先把sample过了,鼓鼓掌。

然后。。

3 rrr

就卡了。我得出的答案居然是7.为什么呢。。原来我是把字符串写了两次直接求,于是当字符串全一样时也就求了两次。

提交,第二组过了,然后。。。

77
rwrwrwrwrwrwrwrwrwrwrwrwbwrwbwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwr
卡了。比答案小了2.瞬间知道原因了。你妹啊!其实在改上题的时候就觉得可能这里想法太单纯了,就是当断点处是w时后面的判断。可是当时懒- -。。觉得不会有问题的吧。。混蛋!加了2*2个if语句才过。。。

然后。。顺利地到第九个。。。

333
rwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwb
毛!答案4我5!在本机上测了n久,发现190个时都没问题。难道是超数组了??可是我明明开了1000啊。

其实这个是为啥我还是不晓得- -。。但最后发现了一个问题,在最外层循环i时我用的是最开始两个字符串连接的代码,也就是i<2*n。。。改了居然就过了。。可是为什么没改为什么就过不了呢?而且前面的怎么也没问题呢啊不想去想了都要想北平了混蛋。。。。

三次过后改的又臭又长的乱码- -

/*
ID: wtff0411
PROG: beads
LANG: C++
*/
#include 
#include 
#include 
#include 

using namespace std;

int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    int n;
    int max=0;
    int temp;
    char change;
    int i,j,k;
    cin>>n;
    char bead[1000],tbeads[400];
    cin>>bead;
    //strcpy(tbeads,bead);
    //strcat(bead,tbeads);
    for(i=0;imax)
        max=temp;
    }
    //cout<

不过这是个o(n^2)的渣算法。还有两种o(n)的算法。其中一种要用到神奇的动归,还有一种很简单。。但是就是想不到啊混蛋!

copy党:

用数组bl,br,rl,rr分别记录在项链i处向左向右收集的蓝色红色珠子数。

项链是环形的,但我们只要把两个同样的项链放在一块,就把它转换成线性的了。

我们只要求出bl,br,rl,rr,那么结果就是max(max(bl[i],rl[i])+max(br[i+1],rr[i+1])) (0<=i<=2*n-1)。

我们以求bl,rl为例:

初始时bl[0]=rl[0]=0

我们从左往右计算

如果necklace[i]='r',rl[i]=rl[i-1]+1,bl[i]=0;

如果necklace[i]='b', bl[i]=bl[i-1]+1,rl[i]=0;

如果necklace[i]='w',bl[i]=bl[i-1]+1,rl[i]=rl[i-1]+1。

同理可以求出br,rr。

事实上不必使用动态规划,直接搜索亦可达到O(n)。

把两个同样的项链放在一块,从头开始用两个变量(变量)a,b记录自左方某点至目前为止可搜集到之两种颜色珠子数,取途中所出现a+b之最大值,遇颜色变换时再将b指定给a即可,代码十分简洁。

有时间(的话)去实现下吧。。。

完蛋去吧少年!


你可能感兴趣的:(acm)