zoukankan      html  css  js  c++  java
  • 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 will include 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.

    PROGRAM NAME: beads

    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
    

    OUTPUT EXPLANATION

    Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.

    wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
                           ****** *****
                           wwwwwb bbbbb  <-- assignments
                           5 x w  6 x b  <-- 11 total
    
    
    
    
    暴力做法:
    View Code
     1 /*
    2 ID:10239512
    3 PROG:beads
    4 LANG:C++
    5 */
    6
    7 #include<iostream>
    8 #include<fstream>
    9 #include<string>
    10 using namespace std;
    11
    12 ifstream fin("beads.in");
    13 ofstream fout("beads.out");
    14
    15 int f[1001],n;char a[1003];
    16
    17 int main()
    18 {
    19 int i,j,k;
    20 fin>>n;
    21 fin>>a;
    22 for(i=n;i>=1;i--)
    23 a[i]=a[i-1];
    24 for(i=1;i<=n;i++)
    25 {a[i+n]=a[i];a[i+2*n]=a[i];}
    26 a[3*n+1]='q';a[0]='q';
    27
    28 for(i=n+1;i<=2*n;i++)
    29 {
    30 j=1;
    31 while(a[i]==a[j+i]||a[j+i]=='w')
    32 j++;
    33 char s=a[i+j];
    34 while(a[i]=='w'&&(a[i+j]==s||a[i+j]=='w')) //由于英语不是很好,题目毛理解,加了a[i]=='w'判断之后就AC了
    35 j++;
    36 f[i]=j;
    37 //cout<<i-29<<" "<<f[i]<<endl;
    38 j=1;
    39 while(a[i-j]==a[i-1]||a[i-j]=='w')
    40 j++;
    41 s=a[i-j];
    42 while(a[i-1]=='w'&&(a[i-j]==s||a[i-j]=='w'))
    43 j++;
    44 f[i]+=j-1;
    45 }
    46 int ma=0;
    47 for(i=n+1;i<=2*n;i++)
    48 if(f[i]>ma) ma=f[i];
    49 fout<<min(ma,n)<<endl;
    50 //system("pause");
    51 return 0;
    52
    53 }
    DP做法:
    View Code
     1 #include <stdio.h>
    2 #include <string.h>
    3 #include <algorithm>
    4
    5 using namespace std;
    6
    7 FILE *in,*out;
    8
    9 int main () {
    10 in = fopen("beads.in", "r");
    11 out = fopen ("beads.out", "w");
    12
    13 int n;
    14 char tmp[400], s[800];
    15 fscanf(in, "%d %s", &n, tmp);
    16
    17 strcpy(s, tmp);
    18 strcat(s, tmp);
    19
    20 int left[800][2], right[800][2];
    21 left[0][0] = left[0][1] = 0;
    22
    23 for (int i=1; i<= 2 * n; i++){
    24 if (s[i - 1] == 'r'){
    25 left[i][0] = left[i - 1][0] + 1;
    26 left[i][1] = 0;
    27 } else if (s[i - 1] == 'b'){
    28 left[i][1] = left[i - 1][1] + 1;
    29 left[i][0] = 0;
    30 } else {
    31 left[i][0] = left[i - 1][0] + 1;
    32 left[i][1] = left[i - 1][1] + 1;
    33 }
    34 }
    35
    36 right[2 * n][0] = right[2 * n][1] = 0;
    37 for (int i=2 * n - 1; i >= 0; i--){
    38 if (s[i] == 'r'){
    39 right[i][0] = right[i + 1][0] + 1;
    40 right[i][1] = 0;
    41 } else if (s[i] == 'b'){
    42 right[i][1] = right[i + 1][1] + 1;
    43 right[i][0] = 0;
    44 } else {
    45 right[i][0] = right[i + 1][0] + 1;
    46 right[i][1] = right[i + 1][1] + 1;
    47 }
    48 }
    49
    50 int m = 0;
    51 for (int i=0; i<2 * n; i++)
    52 m = max(m, max(left[i][0], left[i][1]) + max(right[i][0], right[i][1]));
    53 m = min(m, n);
    54 fprintf(out, "%d\n", m);
    55 fclose(in); fclose(out);
    56 return 0;
    57 }



  • 相关阅读:
    使用node.js如何爬取网站数据
    关于@font-face的使用
    webpack通过postcss-loader添加浏览器前缀
    点击弹出 +1放大效果 -- jQuery插件
    网站CSS选择器性能讨论
    修改 上传图片按钮input-file样式。。
    insertAdjacentHTML方法示例
    css背景色半透明的最佳实践
    js实现选中文字 分享功能
    js实现滑动的弹性导航
  • 原文地址:https://www.cnblogs.com/noip/p/2368679.html
Copyright © 2011-2022 走看看