复杂度为O(n2):

#include <stdio.h> #include <string.h> #include <assert.h>#define MAXN 400char 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.*/ //dir为方向 int nbreak(int p, int dir) {char color;int i, n;//用color来保存颜色color = 'w';/* Start at p if going forward, bead before if going backward */if(dir > 0)i = p;elsei = 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[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); }

官方答案2:复杂度O(n)

#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);//用0列来保存红珠子的数目,用1列来保存蓝珠子的数目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;//这里很牛X,注意向左向右都是i位置的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; }

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] = 0If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0because the length of the blue beads is 0.if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0if c = 'w' then both length of the red and length of blue beadscan 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.

Broken Necklace(USACO官方)相关推荐

  1. USACO - Chapter1 Section 1.1 - Broken Necklace

    Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在 ...

  2. 洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace

    P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 ...

  3. 破碎的项链 Broken Necklace

    破碎的项链 Broken Necklace.cpp 自 信 地 说 这 道 题 我 做 过 --------------分割线------------------ 从中间开始,然后找到第三个点(是w) ...

  4. Broken Necklace破碎的项链

    Broken Necklace破碎的项链 你有一条由 N 个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子:         ...

  5. USACO Section 1.2 Broken Necklace

    题目 题目分析 推的过程 需要避免的坑 整体代码 USACO的题解 题目 题目描述 输入描述 Line 1: N, the number of beads Line 2: a string of N ...

  6. USACO Training Section 1.1 坏掉的项链Broken Necklace

    题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A ...

  7. USACO Broken Necklace

    去掉题目的背景:就是一个环形的串中,寻找一个最长的子串,该串由前后由俩部分组成,连续的b串和连续的r串,当然,一种颜色也可以:w可转变成任意颜色: 我的思路:比较简单的思路,但是是一个复杂度O(n^2 ...

  8. 模拟/usaco 1.1.4 Broken Necklace

    题意 给你一个由r w b组成的字符串,这个串首位相接连成一个环 给出一个计算方法:选取这个环任意两个相邻元素的之间,由这个位置顺时针取连续相同元素,逆时针取连续相同元素,两个方向的元素可不同 求问能 ...

  9. USACO Broken Necklace 题解(环展开成链,枚举)

    题目大意:有一个项链,由红.蓝.白三种颜色的珠子组成,然后现在选择项链中的某一处断开,然后沿断开处的两个珠子分别查找,直至找到一个颜色不同的珠子,并统计个数(其中颜色以第一个非白色的为准,白色的珠子可 ...

最新文章

  1. Attribute在.net编程中的应用
  2. 机器学习入门必读:6种简单实用算法及学习曲线、思维导图
  3. 台式计算机cpu扣不下去,如果台式机的CPU太高怎么办_计算机的基本知识_IT /计算机_信息...
  4. mysql 主从同步不一致_涨知识!MySQL 主从同步原理原来是这样的
  5. c语言程序设计对称字符串,C语言程序设计(字符串)
  6. 如何设置oracle_home变量,oracle-如何在Ubuntu 9.x上正确设置ORACLE_HOME变量?
  7. PCI总线的含义是什么?PCI总线的主要特点是什么?
  8. 【转】简单的解释XSS攻击
  9. 使用CRT调试功能来检测内存泄漏
  10. 距离一个优秀程序员,你还差多少?
  11. 字符编码笔记:ASCII,Unicode和UTF-8(转)
  12. 云栖社区 mysql_mysql
  13. 基于python的微博舆情分析与研究—以《北京冬奥会》为关键词
  14. zynq使用lwip远程更新flash
  15. DB2数据库认证系列教程——IBM DB2认证考前必备
  16. java get/set方法好处
  17. 【Linux编程】UVC摄像头采集与显示(V4L2编程)
  18. HTML5期末大作业:商城网站设计——仿天猫在线商城(HTML和CSS实现天猫在线商城网站)...
  19. 预失真算法matlab实现,2013-全国研究生数学建模B题-seleh模型预失真MATLAB仿真代码...
  20. self和init的用法

热门文章

  1. 百味融汇的火锅宴,品一品别样热辣的鲲鹏生态
  2. 解决uniapp退出全屏视频字体变大的bug
  3. mx6 CSR8670驱动移植 基于linux3.14.58
  4. 首都师范 博弈论 3 3 1求解二人零和博弈
  5. 免杀艺术 1: 史上最全的免杀方法汇总
  6. docker容器虚拟化网络概述
  7. python: plt.cm.Set1, Set2,Set3返回颜色
  8. python绘图后保存的图片打开为什么是空白?
  9. Excel使用技巧系列之一,职场办公事半功倍
  10. 15个最好的免费开源电子商务平台