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

题目粘贴如下

 

 
  
  1. Broken Necklace 
  2.  
  3. 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: 
  4.  
  5. 1 2 1 2 
  6. r b b r b r r b 
  7. r b b b 
  8. r r b r 
  9. r r w r 
  10. b r w w 
  11. b b r r 
  12. b b b b 
  13. b b r b 
  14. r r b r 
  15. b r r r 
  16. b r r r 
  17. r r r b 
  18. r b r r r w 
  19. Figure A Figure B 
  20. r red bead 
  21. b blue bead 
  22. w white bead 
  23.  
  24. The beads considered first and second in the text that follows have been marked in the picture. 
  25.  
  26. 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 . 
  27.  
  28. 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). 
  29.  
  30. Determine the point where the necklace should be broken so that the most number of beads can be collected. 
  31. Example 
  32.  
  33. 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. 
  34.  
  35. 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. 
  36.  
  37. Write a program to determine the largest number of beads that can be collected from a supplied necklace. 
  38. PROGRAM NAME: beads 
  39. INPUT FORMAT 
  40. Line 1: N, the number of beads 
  41. Line 2: a string of N characters, each of which is r, b, or w 
  42. SAMPLE INPUT (file beads.in
  43.  
  44. 29 
  45. wwwbbrwrbrbrrbrbrwrwwrbwrwrrb 
  46.  
  47. OUTPUT FORMAT 
  48. A single line containing the maximum of number of beads that can be collected from the supplied necklace. 
  49. SAMPLE OUTPUT (file beads.out
  50.  
  51. 11 
  52.  
  53. OUTPUT EXPLANATION 
  54. Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked. 
  55.  
  56. wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb 
  57. ****** ***** 
  58. rrrrrb bbbbb <-- assignments 
  59. 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.  
  10. using namespace::std; 
  11.  
  12. int main() 
  13.     ifstream fin("beads.in"); 
  14.     ofstream fout("beads.out"); 
  15.  
  16.     int N; 
  17.     int max(0),tempMax(0); 
  18.     char necklace[400]; 
  19.     int num[400]={0}; 
  20.     int count(0); 
  21.  
  22.     char lastBead; 
  23.     int lastNum; 
  24.     bool stop(false); 
  25.  
  26.     fin>>N; 
  27.     for (int i=0;i<N;i++) 
  28.         fin>>necklace[i]; 
  29.  
  30.     int start,i; 
  31.     for (start=0;necklace[start]=='w';start++); 
  32.     int last=start; 
  33.     int next; 
  34.  
  35.     //分为三段 从last开始到next不一致为止 从last前面开始向前倒数到不一致为止 从next开始到不一致为止 然后next变为last 
  36.     //断点皆为r或者b 
  37.     while (!stop) 
  38.     { 
  39.         int j; 
  40.         //过程1 
  41.         for (j=last;;j++) 
  42.         { 
  43.             if (j==N) j=0; 
  44.             if (necklace[j]==necklace[last]||necklace[j]=='w'
  45.             { 
  46.                 tempMax++; 
  47.                 if (tempMax==N) {fout<<N<<endl;return 0;} 
  48.                 continue
  49.             } 
  50.             break
  51.         } 
  52.         next=j; 
  53.         //过程2 
  54.         for (j=last-1;;j--) 
  55.         { 
  56.             if (j==-1) j=N-1; 
  57.             if (necklace[j]==necklace[last]||necklace[j]=='w'
  58.             { 
  59.                 tempMax++; 
  60.                 continue
  61.             } 
  62.             break
  63.         } 
  64.         //过程3 
  65.         for (j=next;;j++) 
  66.         { 
  67.             if (j==N) {j=0;stop=true;} 
  68.             if (necklace[j]==necklace[next]||necklace[j]=='w'
  69.             { 
  70.                 tempMax++; 
  71.                 continue
  72.             } 
  73.             break
  74.         } 
  75.         if (tempMax>max) max=tempMax; 
  76.         tempMax=0; 
  77.         last=next; 
  78.     } 
  79.      
  80.     fout<<max<<endl; 
  81.  
  82.     fin.close(); 
  83.     fout.close(); 
  84.  
  85.     return 0; 

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