Usaco 1.1.4 Broken Necklace

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Usaco 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.

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

题目大意:给一串珠子,白红蓝三种颜色,白色可做红蓝两种颜色,从中间任意一个地方砍断,往两边收集分别与砍断处两边珠子颜色一样的珠子,使得这个珠子个数最大。很容易想到,如果不是一个入门题应该是重叠两次环形变线性,然后动规玩一发。贴上来是因为,这一次重做自己开始试图直接环形上做,然后手动处理第一个和最后一个的情况,然后虽然做出了了,但是代码风格奇奇怪怪,写博为戒。

技术分享
#include<cstdio>
#include<cmath>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
#define NMAX 5002

int bl[NMAX],br[NMAX],rl[NMAX],rr[NMAX];

int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    int n,ans,tmp;
    string str;
    scanf("%d\n",&n);
    getline(cin,str);
    str=str+str;
    memset(bl,0,sizeof(bl));
    memset(br,0,sizeof(bl));
    memset(rl,0,sizeof(bl));
    memset(rr,0,sizeof(bl));
    if(str[0]==r) 
        rl[0]=1;
    else if(str[0]==b) 
        bl[0]=1;
    else 
        rl[0]=1,bl[0]=1;
    for(int i=1;i<n+n;++i)
    {
        if(str[i]==r) 
            rl[i]=rl[i-1]+1;
        else if(str[i]==b) 
            bl[i]=bl[i-1]+1;
        else 
            rl[i]=rl[i-1]+1,bl[i]=bl[i-1]+1;
    }
    for(int i=n+n-1;i>=0;--i)
    {
        if(str[i]==r) 
            rr[i]=rr[i+1]+1;
        else if(str[i]==b) 
            br[i]=br[i+1]+1;
        else 
            rr[i]=rr[i+1]+1,br[i]=br[i+1]+1;
    }
    ans=0;
    for(int i=0;i<n+n;++i)
    {
        tmp=(rl[i]>bl[i]?rl[i]:bl[i])+(rr[i+1]>br[i+1]?rr[i+1]:br[i+1]);
        ans=ans<tmp?tmp:ans;
    }
    ans=ans<n?ans:n;
    printf("%d\n",ans);
    fclose(stdin);
    fclose(stdout);
    return 0;
}
dp(O(n))
技术分享
#include<cstdio>
#include<map>
using namespace std;

inline const int read()
{
    int k=0;
    char c=getchar();
    while(c>9||c<0)
        c=getchar();
    while(c>=0&&c<=9)
    {
        k=k*10+c-0;
        c=getchar();
    }
    return k;
}

inline const void print(int x)
{
    char a[20];
    int c=0;
    while(x)
    {
        a[++c]=x%10+0;
        x/=10;
    }
    for(;c>0;c--)
       putchar(a[c]);
    putchar(\n);
}

int main()
{
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    int n=read();
    int i,j;
    map<int,char>beats;
    for(i=1;i<=n;++i)
    {
        beats[i]=getchar();
        beats[i+n]=beats[i];
    }
    int temp,num=0;
    for(i=1;i<=n;++i)
    {
        temp=0;
        int c=i;
        char d=beats[i];
        while(beats[i]==w&&c<i+n)
            beats[i]=beats[++c];
        for(j=i;j<i+n;++j)
        {
            if(beats[j]==beats[i]||beats[j]==w) ++temp;
            else break;
        }
        beats[i]=d;
        c=i+n-1;
        d=beats[i+n-1];
        while(beats[i+n-1]==w&&c>i)
            beats[i+n-1]=beats[--c];
        for(j=i+n-1;j>=i;--j)
        {
            if(beats[j]==beats[i+n-1]||beats[j]==w)  ++temp;
            else break;
        }
        beats[i+n-1]=d;
        if(temp>num)
          num=temp;
    }
    if(num>n) print(n);
    else  print(num);
    fclose(stdin);
    fclose(stdout);
    return 0;
 } 
模拟(O(n^2))

 

以上是关于Usaco 1.1.4 Broken Necklace的主要内容,如果未能解决你的问题,请参考以下文章

USACO section1.1 Broken Necklace

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

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

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

USACO Broken Necklace 题解(环展开成链,枚举)

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