USACO Section 1.1 PROB Broken Necklace之zx的恋爱

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.

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

题意:

你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的。 这里是 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
             图片 A                             图片  B
                 
                         r 代表 红色的珠子      
                             b 代表 蓝色的珠子   
                             w 代表 白色的珠子

第一和第二个珠子在图片中已经被作记号。
图片 A 中的项链可以用下面的字符串表示:

 brbrrrbbbrrrrrbrrbbrbbbbrrrrb

假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收集的不同)。 确定应该在哪里打破项链来收集到最大数目的珠子。
例如,在图片 A 中的项链中,在珠子 9 和珠子 10 或珠子 24 和珠子 25 之间打断项链可以收集到8个珠子。

白色珠子什么意思?

在一些项链中还包括白色的珠子(如图片B) 所示。
当收集珠子的时候,一个被遇到的白色珠子可以被当做红色也可以被当做蓝色。
表现含有白珠项链的字符串将会包括三个符号 r , b 和 w 。
写一个程序来确定从一条被给出的项链可以收集到的珠子最大数目。


趣味分析:首先把字符串(项链)复制一遍,这样可以解决环的问题。最大数目的珠子,这些一定包含红、绿,可能有白色的。所以在不考虑白色情况下,只要按序找连续的两种不同颜色的珠子,记录最大数目就行。把这个问题转化一下:把项链当作一个小男孩zx一生将会遇见的n个女孩(珍珠比作女孩),‘w’是名字叫“小白”的女孩,‘r'是名为’小红‘的女孩,‘b’自然就叫‘小蓝’,‘小白’很特别可以变成‘小红’或‘小蓝’。zx很花心(当然是这时题目的要求),遇见任何女孩都会当作自己女朋友(即若两个女孩不一样,zx就换女朋友了),但zx只能换两次女朋友,遇见一个女孩zx就长大一点(生存时间加一),第三次时zx将会被“更新”。问题“打破项链来收集到最大数目的珠子”即为zx最大生存时间。


/*
ID: z-x
PROG: beads
LANG: C
*/
#include<stdio.h>
#include<string.h>

int main(){
    freopen("beads.in", "r", stdin);
    freopen("beads.out", "w", stdout);
	int n, i, j, time, chance, max = 0;
	char str[1000], girlfriend;
	scanf("%d%s", &n, str);    //str是他遇见的n个女孩
	for(i = 0; i < n; i++) str[i + n] = str[i];   //加长一倍,可以成环比较
	for(i = 0; i < n; i++){   //i表示zx的初恋,zx可以选择0-n中任一位女孩作为他的初恋情人
		time = 0;         //zx出生时生存时间当然是0
		girlfriend = 'w'; //默认zx女朋友是“小白”
		chance = 2;       //zx有两次换女朋友的机会
		for(j = i; j < i + n; j++){  //从zx的初恋(i)开始,zx将会遇见n个女孩
			if(str[j] != 'w'){  //只要是“小白”(因为“小白”既可以变成“小蓝”也可以变成“小红”)zx都不会换女朋友的
				if(chance == 2){ //zx还有两次机会的时候遇到了一个不是‘小白’的女孩,即第一次换女朋友
					girlfriend = str[j]; //女朋友换成str[j]了
					chance--;  //机会减一
					time++;    //生存时间加一
					continue;  //直接看下一个女孩
				}
				if(chance == 1 && str[j] != girlfriend){ //zx还有一次机会的时候遇见了另一个女孩,即第二次换女朋友
					girlfriend = str[j];  //女朋友换成str[j]了
					time++;   //生存时间加一
					chance--; //机会减一
					continue; //下一个
				}
				if(!chance && str[j] != girlfriend){ //zx已经没有机会了,他竟然还想换女朋友!
					if(max < time) max = time;  //zx死了。。。max记录他最长的生存时间
					time = 0; chance = 2;       //zx又获得了新生 :)!
					continue;                   //如果女孩们(str)还没全都邂逅一遍,继续。。。
				}
			}
			time++;
			if(j == n + i - 1 && time > max) max = time; //若这是最后一个女孩,即zx生命到尽头,记录生存时间
		}
	}
	if(time > max) max = time;
	printf("%d\n", max);
	return 0;
}


这个"恋爱问题"究竟说明了什么,是在说“初恋慎重”?还是“红颜祸水”?或是“英雄气短儿女情长”。。。其实我也不知道。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值