1.1.4 USACO Broken Necklace

https://train.usaco.org/usacoprob2?a=dGHkTLUWEXj&S=beads

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 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.

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.

                Two necklace copies joined here
                             v
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                       ******|*****
                       rrrrrb|bbbbb  <-- assignments
                   5xr .....#|#####  6xb

                        5+6 = 11 total

題目描述

你有一條由 nn 個紅色的,白色的,或藍色的珠子組成的項鍊,珠子是隨意安排的。 這裏是 n=29n=29 的兩個例子:

第一和第二個珠子在圖片中已經被作記號。

圖片 A 中的項鍊可以用下面的字符串表示:

brbrrrbbbrrrrrbrrbbrbbbbrrrrb

假如你要在一些點打破項鍊,展開成一條直線,然後從一端開始收集同顏色的珠子直到你遇到一個不同的顏色珠子,在另一端做同樣的事(顏色可能與在這之前收集的不同)。 確定應該在哪裏打破項鍊來收集到最大數目的珠子。

例如,在圖片 A 中的項鍊中,在珠子 99 和珠子 1010 或珠子 2424 和珠子 2525 之間打斷項鍊可以收集到 88 個珠子。

白色珠子什麼意思?

在一些項鍊中還包括白色的珠子(如圖片B) 所示。

當收集珠子的時候,一個被遇到的白色珠子可以被當做紅色也可以被當做藍色。

表現含有白珠項鍊的字符串將會包括三個符號 rbw 。

寫一個程序來確定從一條被給出的項鍊可以收集到的珠子最大數目。

輸入格式

第一行一個正整數 nn ,表示珠子數目。 第二行一串長度爲 nn 的字符串, 每個字符是 r , b 或 w

輸出格式

輸出一行一個整數,表示從給出的項鍊中可以收集到的珠子的最大數量。

輸入輸出樣例

輸入 #1複製

29 
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

輸出 #1複製

11

說明/提示

【數據範圍】
對於 100\%100% 的數據,3\le n \le 3503≤n≤350

題目翻譯來自NOCOW。

USACO Training Section 1.1

思路:(暴力解法)這種解法對空間要求比較高,空間限制嚴格時不適用。

爲了對首尾字符的情況進行計算,對原字符串(往左、往右)拓展兩倍,對原字符串的每一個字符進行枚舉。假設從第一個字符、第二個字符、第三個字符....第n個字符斷開得到可以收集到的最大珠子數目sum.然後對n種情況下的sum求最大值。不過需要注意一種特殊情況,即全爲w的情況,sum的值最大不能超過n,特判一下即可。

對於每一個字符,如上圖所示,假設從第一個字符開始斷開,以第一個字符r爲分界點,將字符串分爲左邊一部分:rbwrr和右邊一部分:bwrrbwr.

針對左邊一部分rbwrr:

(1)如果遇到白色的珠子,數量直接sum+1;

(2)如果遇到不是白色的珠子:

情況1: 第一次遇到不是白色的珠子,數量sum+1,記錄下第一個不是白色珠子的顏色。

情況2:不是第一次遇到白色的珠子。如果珠子的顏色和前面記錄的顏色一隻,sum+1;如果顏色不一致,可以結束尋找了。

右邊的情況同理於左邊的情況。

 

 

/*
ID:L
PROG: beads
LANG: C++ 
*/ 
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
char a[1100];
int main()
{
	freopen("beads.in","r",stdin);
	freopen("beads.out","w",stdout);
	int n;
	cin >> n;
	char c;
	for(int i = 1; i <= n; ++i)//將字符串擴展兩倍 
	{
		cin >> c;
		a[i] = c; a[n+i] = c; a[2*n + i] = c;
	}
	//printf("%s\n",a);
//	for(int i = 1; i <= 3*n; ++i) cout << a[i];
//	cout << endl;
	int sum=0, ans = 0;//sum用來記錄每次最大的數量,ans記錄最終最大的數量 
	for(int i = n+1; i <= 2*n; ++i)//對每一個珠子進行枚舉,假設從第一個、第二個....第n個珠子斷開時的收集珠子最大數量 
	{
		sum = 0;
		int l = i, r = i+1;
		char k;
		bool b = 0;
		for(int j = l; j >= 1; j--)//從左邊開始找 
		{
		//	bool b = 0;
			if(a[j] == 'w') sum++;//如果遇到白色的珠子直接相加 
			else if(b == 0) //如果第一個珠子不是白色,記錄下它的顏色 
			{
				k = a[j];//記錄第一個不是白色的珠子顏色 
				sum++;
				b = 1;//區分是否是第一個珠子 
			}
			else if(a[j] != k) break;//如果後面的珠子和前面珠子顏色不一樣,就不繼續尋找了 
			else if(a[j] == k) sum++;//如果後面的珠子和前面珠子顏色一致,繼續尋找 
		}
	//	cout << i << "left" << sum << endl;
		char kr;
		bool br = 0;
		for(int j = r; j <= 3*n; j++)//從右邊開始找 
		{
			if(a[j] == 'w') sum++;
			else if(br == 0) 
			{
				kr = a[j];
				sum++;
				br = 1;
			}
			else if(a[j] != kr) break;
			else if(a[j] == kr) sum++;
		}
		if(sum >= n) sum = n;
		ans = max(sum,ans);
	//	cout << i << ":=" << ans << endl;
	}
	cout << ans << endl;
	return 0;
 } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章