USACO Broken Necklace

1. 算法:从一个节点开始,假如是r,如果下一个是r或者w,继续,一直到下一个为b为止,然后从b开始看下一个,如果是b或者w,继续,如果是r停止,处理“环”的问题,用了求余,可以使结尾的下一个变成第一个,但是要注意,最后结果不能大于n。

2. 这是O(n^2) 的复杂度吧。


/*
ID: dollarzhaole
PROG: beads
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    ofstream fout ("beads.out");
    ifstream fin ("beads.in");
    int n, i, j, sum, max = 0;
    int sum_w = 0, sum_r = 0, sum_b = 0;
    char tmp;
    string str;
    fin >> n >> str;
    for (i = 0; i < n; i++)
    {
        if (str[i] == 'w')
            sum_w++;
        else if (str[i] == 'r')
            sum_r++;
        else if (str[i] == 'b')
            sum_b++;
    }
    if (sum_b + sum_w == n || sum_r + sum_w == n)
        fout << n << endl;
    else
    {
        for (i = 0; i < n; i++)
        {
            sum = 0;
            if (str[i] == 'r' || str[i] == 'b')
            {
                j = i;
                while(str[j%n] == str[i] || str[j%n] == 'w')
                {
                    sum++;
                    j++;
                }
                while(str[j%n] != str[i] || str[j%n] == 'w')
                {
                    sum++;
                    j++;
                }
            }
            else if (str[i] == 'w')
            {
                j = i;
                while (str[j%n] == 'w')
                {
                    sum++;
                    j++;
                }
                tmp = str[j%n];
                while (str[j%n] == tmp || str[j%n] == 'w')
                {
                    sum++;
                    j++;
                }
                while (str[j%n] != tmp || str[j%n] == 'w')
                {
                    sum++;
                    j++;
                }
            }
            if (sum >= n)   sum = n;
            if (max < sum)  max = sum;
        }
        fout << max << endl;
    }
    return 0;
}


以下是参考代买,复杂度是O(n)。

/*Dynamic Programming is good method for solving this problem in O(N). If we consider two copies of the string we easy transform cyclic configuration of the necklace to linear. Now we can compute for each breaking point how many beads of the same color can be collected on the left and on the right from the breaking point. I show how we can compute it only for the left side. For right side it is analogical. Let r[p] and b[p] be the number of red / blue beads that can be collected, when necklace is broken in point p. If we know this and color of next bead (c) we can compute r[p+1] and b[p+1].
 r[0] = p[0] = 0
 If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0
        because the length of the blue beads is 0.
 if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0
 if c = 'w' then both length of the red and length of blue beads
             can be longer.
so r[p+1] = r[p]+1 and b[p+1] = b[p] + 1.
The number of beads that can be collected in breaking point p is then max(left[r[p]], left[b[p]]) + max(right[r[p]], right[b[p]]). And the maximum from this value is answer for the problem.
*/
#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;
}


posted on 2012-04-05 20:39  赵乐ACM 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/dollarzhaole/archive/2012/04/05/3188950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值