您的位置:首页 > 大数据 > 人工智能

USACO TRAINING - PROB Broken Necklace

2016-04-24 23:40 155 查看
本文记录的是USACO练习题-Broken Necklace

题目

description

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.



实现

O(n^2)

/*
ID: ypc63452
LANG: C
TASK: beads
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 350
int main()
{
FILE *fin  = fopen ("beads.in", "r");
FILE *fout = fopen ("beads.out", "w");
int n = 0, i = 0, j = 0, k = 1, sum = 0, count = 1,flag = 0;
char dest ;
char beads[MAX]= {0};

fscanf(fin,"%d", &n);
fscanf(fin,"%s", beads);

for(i = 0; i < n; i++)
{
count = 1;
j = (i + n -1) % n;
k = (i+1) % n;
flag = 0;
dest = beads[i] ;
//向前找
while (k != i)
{
//如果dest的值为 'w',则dest需要重新赋值为第一个非'w'的值。
if ((flag == 0) &&(dest == 'w')&&(dest != beads[k]) )
{
flag = 1;
dest = beads[k];
}

if ((dest == beads[k]) || (beads[k] == 'w') )
{
k = (k + 1) % n;
count ++;
continue;
}
break;
}

k = (k -1 + n) % n;
flag = 0;
dest = beads[(i-1+n)%n]  ;
//向后找
while (k != j)
{
//如果dest的值为 'w',则dest需要重新赋值为第一个非'w'的值。
if ((flag == 0) &&(dest == 'w')&&(dest != beads[k]) )
{
flag = 1;
dest = beads[k];
}

if ((dest == beads[j]) || (beads[j] == 'w'))
{
j = (j - 1 +n) % n;
count ++;
continue;
}
break;
}

if (count  > sum)
{
sum = count ;
}
}
fprintf(fout, "%d\n", sum);
return 0;
}


官方答案

第一种解法(O(N^2))

#include <stdio.h>
#include <string.h>
#include <assert.h>

#define MAXN 400

char necklace[MAXN];
int len;

/*
* Return n mod m.  The C % operator is not enough because
* its behavior is undefined on negative numbers.
*/
int
mod(int n, int m)
{
while(n < 0)
n += m;
return n%m;
}

/*
* Calculate number of beads gotten by breaking
* before character p and going in direction dir,
* which is 1 for forward and -1 for backward.
*/
int
nbreak(int p, int dir)
{
char color;
int i, n;

color = 'w';

/* Start at p if going forward, bead before if going backward */
if(dir > 0)
i = p;
else
i = mod(p-1, len);

/* We use "n<len" to cut off loops that go around the whole necklace */
for(n=0; n<len; n++, i=mod(i+dir, len)) {
/* record which color we're going to collect */
if(color == 'w' && necklace[i] != 'w')
color = necklace[i];

/*
* If we've chosen a color and see a bead
* not white and not that color, stop
*/
if(color != 'w' && necklace[i] != 'w' && necklace[
a48f
i] != color)
break;
}
return n;
}

void
main(void)
{
FILE *fin, *fout;
int i, n, m;

fin = fopen("beads.in", "r");
fout = fopen("beads.out", "w");
assert(fin != NULL && fout != NULL);

fscanf(fin, "%d %s", &len, necklace);
assert(strlen(necklace) == len);

m = 0;
for(i=0; i<len; i++) {
n = nbreak(i, 1) + nbreak(i, -1);
if(n > m)
m = n;
}

/*
* If the whole necklace can be gotten with a good
* break, we'll sometimes count beads more than
* once.  this can only happen when the whole necklace
* can be taken, when beads that can be grabbed from
* the right of the break can also be grabbed from the left.
*/
if(m > len)
m = len;

fprintf(fout, "%d\n", m);
exit (0);
}


第二种解法(O(N^2))

使用的是动态规划的思想

#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

FILE *in,*out;

int main () {
in = fopen("beads.in", "r");
out = fopen ("beads.out", "w");

int n;
char tmp[400], s[800];
fscanf(in, "%d %s", &n, tmp);

strcpy(s, tmp);
strcat(s, tmp);

int left[800][2], right[800][2];
left[0][0] = left[0][1] = 0;

for (int i=1; i<= 2 * n; i++){
if (s[i - 1] == 'r'){
left[i][0] = left[i - 1][0] + 1;
left[i][1] = 0;
} else if (s[i - 1] == 'b'){
left[i][1] = left[i - 1][1] + 1;
left[i][0] = 0;
} else {
left[i][0] = left[i - 1][0] + 1;
left[i][1] = left[i - 1][1] + 1;
}
}

right[2 * n][0] = right[2 * n][1] = 0;
for (int i=2 * n - 1; i >= 0; i--){
if (s[i] == 'r'){
right[i][0] = right[i + 1][0] + 1;
right[i][1] = 0;
} else if (s[i] == 'b'){
right[i][1] = right[i + 1][1] + 1;
right[i][0] = 0;
} else {
right[i][0] = right[i + 1][0] + 1;
right[i][1] = right[i + 1][1] + 1;
}
}

int m = 0;
for (int i=0; i<2 * n; i++)
m = max(m, max(left[i][0], left[i][1]) + max(right[i][0], right[i][1]));
m = min(m, n);
fprintf(out, "%d\n", m);
fclose(in); fclose(out);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: