您的位置:首页 > 其它

USACO1.1.4 Broken Necklace 破碎的项链

2013-11-25 15:58 323 查看
一.题目要求

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.
original 'split'
v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
******|*****
rrrrrb|bbbbb  <-- assignments
5 x r  6 x b  <-- 11 total


二.遇到的问题

1.最开始的思路是分为两类,最开始如果是‘b’或‘w’怎么样,如果是‘r’或‘w’怎么样,在这个整体思路下纠结了很久,最后发现本质上是有逻辑问题的,因为w只能和b连在一起,遇到w之后就进入了第一个判断体系下,所以怎么的都不对。

2.后来想分三类讨论,如果第一个是r 如果是b 如果是w,可是最后发现还是特别的麻烦,就一直纠结在算法里出不来。

3.然后看了看题解,看到了大体思路如下:

    一、用%计算来作为下标值,从n到2n循环,相当于把两个数组拼接

    二、如果有连续相等的,索引值加一,继续向下寻找,直到找到两个连续不相等的索引值

    三、如果当前索引值对应的字符与下一索引值对应的字符不相等,若1.当前为b,则symbol1=b,2.当前为r,则symbol1=r,3.当前为w,则symbol1=下一个字符的相反,symbol2与symbol1相反(相反的意思为‘r’‘b’相反)

    四、检测到相邻不相同之后,前后搜索,增加num的值。

    五、最后考虑一个特殊情况,如果整条链子只有一个颜色,那么是一直不进入循环中的if语句的,num数值不会变化。因此,当num=0时,设定num=这个数组长度。

4.还有注意一个问题,只有用new 创建一个动态数组时才能删除,直接char  ch【n】不能用delete

5.个人对你特别无语,这个程序改了好久,总是在小细节处出错,关键还是逻辑没有理清楚,两个循环的循环范围是什么很重要,判断的时候==写成=这种错误怎么还可以再犯而且怎么都检查不出来。

三.代码

/*

ID:grj11292

PROG:beads

LANG:C++

*/

#include<iostream>

#include<fstream>

using namespace std;

int maxNum(char *beads,int n);

int main()

{

    ifstream fin("beads.in");

    ofstream fout("beads.out");

    int beadsNum;

    fin>>beadsNum;

    char *beadsColor;

    beadsColor=new char[beadsNum+1];

    fin>>beadsColor;

   // fout<<beadsColor<<endl;

   // fout<<beadsNum<<endl;

    fout<<maxNum(beadsColor,beadsNum)<<endl;

    delete [] beadsColor;

    fin.close();

    fout.close();

    return 0;

}

int maxNum(char *beads,int n)

{

    int i,j,k,maxN=0,maxNtmp;

    char symbol1,symbol2;

    for(i=n;i<2*n;++i)

    {

        if(beads[i%n]!=beads[(i+1)%n])

        {

            symbol1=(beads[i%n]!='w')?beads[i%n]:((beads[(i+1)%n]=='b')?'r':'b');

            symbol2=(symbol1=='b')?'r':'b';

            maxNtmp=0;

            for(j=i;j>i-n;--j)

            {

                if(beads[j%n]==symbol1 || beads[j%n]=='w')maxNtmp++;

                else break;

            }

            for(k=i+1;k<j+n+1;++k)

            {

                if(beads[k%n]==symbol2 || beads[k%n]=='w')maxNtmp++;

                else break;

            }

            if(maxNtmp>maxN)maxN=maxNtmp;

        }

    }

    if(maxN==0)maxN=n;

   // if(maxN>n)maxN=n;

    return maxN;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: