您的位置:首页 > 其它

USACO1.1.4 Broken Necklace

2013-07-22 11:37 330 查看

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 havebeen marked in the picture.

The configuration in Figure A may be represented as a string of b'sand r's, where b represents a blue bead and r represents a red one, asfollows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Suppose you are to break the necklace at some point, lay it outstraight, and then collect beads of the same color from one enduntil you reach a bead of a different color, and do the same forthe other end (which might not be of the same color as the beadscollected
before this).

Determine the point where the necklace should be broken so that themost 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 betweenbead 24 and bead 25.

In some necklaces, white beads had been included as shown in FigureB above. When collecting beads,
a white bead that is encountered maybe treated as either red or blue and then painted with the desired color.The string that represents this configuration will include the threesymbols r, b and w.

Write a program to determine the largest number of beads that can becollected 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 whichis 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 becollected from the supplied necklace.

SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION

Consider two copies of the beads (kind of like being able to runaroundthe ends). The string of 11 is marked.
original 'split'
v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
******|*****
rrrrrb|bbbbb  <-- assignments
5 x r  6 x b  <-- 11 total

题目翻译:
你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的。 这里是 n=29 的二个例子:[code]               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
图片 A                        图片  B

r 代表 红色的珠子
b 代表 蓝色的珠子
w 代表 白色的珠子
第一和第二个珠子在图片中已经被作记号。 图片 A 中的项链可以用下面的字符串表示:brbrrrbbbrrrrrbrrbbrbbbbrrrrb . 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收集的不同)。 确定应该在哪里打破项链来收集到最大多数的数目的珠子。 例如,在图片 A 中的项链中,在珠子 9 和珠子 10 或珠子 24 和珠子 25 之间打断项链可以收集到8个珠子。 在一些项链中还包括白色的珠子(如图片B) 所示。 当收集珠子的时候,一个被遇到的白色珠子可以被当做红色也可以被当做蓝色。表现含有白珠项链的字符串将会包括三个符号 r , b 和 w 。 写一个程序来确定从一条被给出的项链可以收集到的最大珠子数目。

Input

第 1 行: N, 珠子的数目 第 2 行: 一串长度为N的字符串, 每个字符是 r , b 或 w。

Output

单独的一行包含从被供应的项链可以被收集的珠子数目的最大值。

解题思路:
思路很明显,可以采用DP来做,也可以采用穷举,本题数据规模不大,用暴力完全不是问题,关键在于输入的数据如何组成环的形状,当然,用“取模法”可以做到,
题目在说明时也给我们一个思路,就是将两条数据条连接起来,同样可以实现所有情况。由于题目告诉我们,'w'既可以做‘r'用,又可以做’b‘来用,那么,在一次遍历中,
我们记录以每一个字母为首的最长链,并用Max来保存,显然,在遍历一次(也就是复杂度为O(n))后,就可以找到最长链。
代码如下:
/*
ID:ayludon3
LANG: C++
TASK: beads
*/

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

int main()
{
//    ifstream fin ("beads.in");
//    ofstream fout ("beads.out");

int n,max=0,count,flag,i,j;
string s;
char c;
//    fin>>n>>s;
cin>>n>>s;
s+=s;       //将两个s连接起来,以实现首尾相连
//    cout<<s<<endl;
for(i=0;i<n;i++)
{
c=(char)s[i];    //获取当前s中第i个字符
if(c=='w')
flag=0;
else
flag=1;
j=i;
count=0;
while(flag<=2)      //flag<=2即是考虑从某一点前后各有多长
{
while((s[j]==c||s[j]=='w')&&j<n+i)
{
count++;
j++;
}
flag++;
c=s[j];
}
if(count>max)
max=count;
}
//    fout<<max<<endl;
cout<<max;
return 0;
}


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