码迷,mamicode.com
首页 > 其他好文 > 详细

Usaco 1.1.4 Broken Necklace

时间:2017-10-25 11:24:18      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:代码   int   orm   nec   closed   config   maximum   utc   stream   

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

标签:代码   int   orm   nec   closed   config   maximum   utc   stream   

原文地址:http://www.cnblogs.com/wykjx1314/p/7727075.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!