1. 程式人生 > >Usaco 1.1.4 Broken Necklace

Usaco 1.1.4 Broken Necklace

symbols -a mbo lose like pic cnblogs 代碼 容易

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