最近在做usaco的题目,想趁着最近没什么事情做掉这些题
后面还想做什么再说吧,感觉大四把usaco刷掉就已经足够了,貌似没什么时间做别的

题目粘贴如下

  1. Broken Necklace
  2. 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:
  3. 1 2 1 2
  4. r b b r b r r b
  5. r b b b
  6. r r b r
  7. r r w r
  8. b r w w
  9. b b r r
  10. b b b b
  11. b b r b
  12. r r b r
  13. b r r r
  14. b r r r
  15. r r r b
  16. r b r r r w
  17. Figure A Figure B
  18. r red bead
  19. b blue bead
  20. w white bead
  21. The beads considered first and second in the text that follows have been marked in the picture.
  22. 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 .
  23. 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).
  24. Determine the point where the necklace should be broken so that the most number of beads can be collected.
  25. Example
  26. 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.
  27. 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 will include the three symbols r, b and w.
  28. Write a program to determine the largest number of beads that can be collected from a supplied necklace.
  29. PROGRAM NAME: beads
  30. INPUT FORMAT
  31. Line 1: N, the number of beads
  32. Line 2: a string of N characters, each of which is r, b, or w
  33. SAMPLE INPUT (file beads.in)
  34. 29
  35. wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
  36. OUTPUT FORMAT
  37. A single line containing the maximum of number of beads that can be collected from the supplied necklace.
  38. SAMPLE OUTPUT (file beads.out)
  39. 11
  40. OUTPUT EXPLANATION
  41. Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
  42. wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
  43. ****** *****
  44. rrrrrb bbbbb <-- assignments
  45. 5 x r 6 x b <-- 11 total

我很弱的做了好久这个题目,最后思路如下
首先是从最开始向后找找到第一个非w的点,记为last,然后进行以下三个过程
1.从该点向后找直到找到第一个不同颜色的点,记为next
2.从该点向前找直到找到第一个不同颜色的点
3.从next向后找直到找到第一个与next所在点颜色不同的点
这样就能遍历到所有与last所在点相关的节点了,我们用tempMax去跟踪,没经过一个点就加1,如果大于之前记录的最大值max就更新max的值

这个算法应该是O(N)的,能走3次的节点就是那些w点了,其他的有颜色的点都是走2次的
usaco上给的算法我没看,先做实验室的作业吧
代码:

  1. /*
  2. ID:haohual1
  3. PROG:beads
  4. LANG:C++
  5. */
  6. #include <fstream>
  7. #include <iostream>
  8. #include <string>
  9. using namespace::std;
  10. int main()
  11. {
  12. ifstream fin("beads.in");
  13. ofstream fout("beads.out");
  14. int N;
  15. int max(0),tempMax(0);
  16. char necklace[400];
  17. int num[400]={0};
  18. int count(0);
  19. char lastBead;
  20. int lastNum;
  21. bool stop(false);
  22. fin>>N;
  23. for (int i=0;i<N;i++)
  24. fin>>necklace[i];
  25. int start,i;
  26. for (start=0;necklace[start]=='w';start++);
  27. int last=start;
  28. int next;
  29. //分为三段 从last开始到next不一致为止 从last前面开始向前倒数到不一致为止 从next开始到不一致为止 然后next变为last
  30. //断点皆为r或者b
  31. while (!stop)
  32. {
  33. int j;
  34. //过程1
  35. for (j=last;;j++)
  36. {
  37. if (j==N) j=0;
  38. if (necklace[j]==necklace[last]||necklace[j]=='w')
  39. {
  40. tempMax++;
  41. if (tempMax==N) {fout<<N<<endl;return 0;}
  42. continue;
  43. }
  44. break;
  45. }
  46. next=j;
  47. //过程2
  48. for (j=last-1;;j--)
  49. {
  50. if (j==-1) j=N-1;
  51. if (necklace[j]==necklace[last]||necklace[j]=='w')
  52. {
  53. tempMax++;
  54. continue;
  55. }
  56. break;
  57. }
  58. //过程3
  59. for (j=next;;j++)
  60. {
  61. if (j==N) {j=0;stop=true;}
  62. if (necklace[j]==necklace[next]||necklace[j]=='w')
  63. {
  64. tempMax++;
  65. continue;
  66. }
  67. break;
  68. }
  69. if (tempMax>max) max=tempMax;
  70. tempMax=0;
  71. last=next;
  72. }
  73. fout<<max<<endl;
  74. fin.close();
  75. fout.close();
  76. return 0;
  77. }

在chinaunix上被吞了,打击我热情T_T

【usaco】beads相关推荐

  1. jzoj 1594: 【USACO】The Chivalrous Cow(骑士牛)( 待加入)

    1594: [USACO] 题目描述 Farmer John traded one of his cows for a cow that Farmer Don called 'The Knight' ...

  2. 2190: 【USACO】Farmer John has no Large Brown Cow

    2190: [USACO]Farmer John has no Large Brown Cow 时间限制: 1.000 Sec  内存限制: 64 MB 提交: 16  解决: 12 [命题人:][下 ...

  3. 2191: 【USACO】Crowded Cows

    2191: [USACO]Crowded Cows 时间限制: 1.000 Sec  内存限制: 64 MB 提交: 26  解决: 19 [命题人:][下载数据: 90] 提交状态报告 题目描述 ...

  4. 【USACO】双数? 单数?

    [USACO]双数? 单数? 题意分析: 这道题的题意还是比较明了的,就是读入一个很大的数,判断它是奇数还是偶数. 解题思路: 因为我们知道,任意一个数最后一位是奇数,这个数就是奇数,偶数,同理. 这 ...

  5. 1584: 【USACO】双数? 单数?

    1584: [USACO]双数? 单数? 时间限制: 1.000 Sec  内存限制: 64 MB 提交: 6305  解决: 2829 [命题人:][下载数据: 70] 提交状态报告 题目描述 B ...

  6. 【USACO】iCow播放器

    [USACO]iCow播放器 题目: 被无止境的农活压榨得筋疲力尽后,Farmer John打算用他在MP3播放器市场新买的iCow来听些音乐,放松一下.FJ的iCow里存了N(1 <= N & ...

  7. 【USACO】贝茜的晨练计划

    [USACO]贝茜的晨练计划 题目: 奶牛们打算通过锻炼来培养自己的运动细胞,作为其中的一员,贝茜选择的运动方式是每天进行N(1 <= N <= 10,000)分钟的晨跑. 在每分钟的开始 ...

  8. 【USACO】青铜莲花池[2]

    前言 搜索到这篇文章的朋友,那么很巧了,我们多半是一个学校的,为什么呢?因为这道题叫白银莲花池.. 题目 [问题描述] FJ建造了一个美丽的池塘,用于让奶牛们锻炼.这个长方形的池子被分割成了 M 行和 ...

  9. 【USACO】2017 December Contest, Platinum题解

    [比赛经历] 大概顺利满分了,就是T2的代码比较难调. T2能够直观地反映出GDB和输出调试结合的优越性. [T1]Standing Out from the Herd [题目链接] 点击打开链接 [ ...

最新文章

  1. python 笔记:函数
  2. java 动态创建实例_java – 有没有办法动态创建类(而不是实例)?
  3. foldable bike
  4. 吴恩达 coursera ML 第十六课总结+作业答案
  5. 使用mac m1跑fortran代码hello world
  6. 设计模式(一):工厂方法
  7. 一张有趣的图--《teach yourself c++ in 21 days》
  8. mysql-电商库演练1-创建数据-基本查询练习
  9. python神经网络案例——CNN卷积神经网络实现mnist手写体识别
  10. 免费的文本分析 文本挖掘软件工具(第一部分)
  11. Qt开发笔记:OpenSSL库介绍、windows上mingw32版本的OpenSSL编译模块化
  12. Hessian Matrix(海森矩阵)
  13. 码农和程序员之间的5个关键差异
  14. 没有超级英雄?自己做一个java漫威英雄手办商城系统
  15. Word解决:以下内容无法保存在未启用宏的文档中: ●VBA工程 请单击“否“返回”另存为“对话框,然后在文件类型“下拉框中选择一种启用宏的文件类型。是否继续将其另存为未启用宏的文档?
  16. php原生检测用户,php代码在线测试_php检测用户在线状态的实例代码
  17. cadence SPB17.4 - orcad - exprot sch PDF
  18. 转载《由于这台计算机没有远程桌面客户端访问许可证,远程会话被中断》的解决方案
  19. 激励人生成功的10句经典中英文
  20. C语言|鼠标点击开始

热门文章

  1. hMailServer邮件服务器配置(超详细,附坑解决办法)
  2. Google Play开发者账号被封,账号关联原因分析
  3. 从0开始学杂项 第二期:隐写分析(1) 直接附加
  4. C语言编程-7_5 整数三位分隔
  5. 将RGB数据转为图片的MATLAB代码
  6. 图片可隐藏zip和mp3文件,只需修改后缀名,这个GitHub项目火了
  7. CPU使用率到100%,有哪些因素影响?
  8. PyCharm安装教程、Anaconda安装配置教程(超级详细)
  9. Metasploit工具学习(二)
  10. ChatGPT列出全球最强的10个国家,中国排名意外