usaco 1.1 Broken Necklace

来源:互联网 发布:csmhuan实时数据 编辑:程序博客网 时间:2024/05/21 18:32

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 beadsLine 2:a string of N characters, each of which is r, b, or w

SAMPLE INPUT (file beads.in)

29wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

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                       ****** *****                       rrrrrb bbbbb  <-- assignments                       5 x r  6 x b  <-- 11 total



模拟题啊模拟题。。。
所以说模拟题是弱项啊弱项。。。
这道题做了我几个小时啊。。。纠结
为了这道题重新写了几遍的代码。。。
总算是过了~
就是代码有点长:)


代码:
View Code
  1 /*   2 ID: jings_h1  3 PROG: beads   4 LANG: C++   5 */    6 #include<iostream>  7 #include<stdio.h>  8 #include<string.h>  9 using namespace std; 10 char a[400]; 11 bool vis[400]; 12 int tempsum; 13 int n; 14 int frontsearch(int p,char v){ 15     int j; 16     for(j=p;j<n;j++){ 17             if(((a[j]==v)||(a[j]=='w'))&&(vis[j]==false)){ 18                   tempsum++; 19                   vis[j]=true; 20                   } 21             else{ 22                   break; 23                   } 24                   } 25     if(j>=n) 26         return 0; 27     return 1; 28 } 29 int backsearch(int p,char v){ 30     int k; 31     for(k=p;k>=0;k--){ 32             if((a[k]==v||a[k]=='w')&&vis[k]==false){ 33                   tempsum++; 34                   vis[k]=true; 35                   } 36             else{ 37                   break; 38                   } 39                   } 40     if(k<0) 41         return 0; 42     return 1; 43 } 44  45 int getres(int p,char v){ 46     int res=0; 47     tempsum=0; 48     int re=frontsearch(p,v); 49     res+=tempsum; 50     tempsum=0; 51     if(re==0){ 52        frontsearch(0,v); 53        res+=tempsum; 54        tempsum=0; 55        } 56     return res; 57 } 58 int getres2(int p,char v){ 59     int res=0; 60     tempsum=0; 61     int re2=backsearch(p,v); 62     res+=tempsum; 63     tempsum=0; 64     if(re2==0){ 65        backsearch(n-1,v); 66        res+=tempsum; 67        tempsum=0; 68        } 69     return res; 70 } 71  72 int main(){ 73     FILE *fin = fopen("beads.in","r");   74     FILE *fout = fopen("beads.out","w");   75         fscanf(fin,"%d",&n); 76         fscanf(fin,"%s",a);   77     //    scanf("%s",a); 78         int sum=0; 79         80         for(int i=0;i<n;i++){  81                  tempsum=0; 82                  memset(vis,false,sizeof(vis)); 83                  int temp=i-1; 84                  if(temp<0){ 85                       temp=n-1; 86                       } 87                  if(a[i]=='w'&&a[temp]=='w'){ 88                       int temp1=0,temp2=0,temp3=0,temp4=0; 89                        memset(vis,false,sizeof(vis)); 90                       temp1+=getres(i,'r'); 91                       temp1+=getres2(temp,'r'); 92                       memset(vis,false,sizeof(vis)); 93                       temp2+=getres(i,'b'); 94                       temp2+=getres2(temp,'b'); 95                        memset(vis,false,sizeof(vis)); 96                       temp3+=getres(i,'r'); 97                       temp3+=getres2(temp,'b'); 98                        memset(vis,false,sizeof(vis)); 99                       temp4+=getres(i,'b');100                       temp4+=getres2(temp,'r');101                       sum=sum>temp1?sum:temp1;102                       sum=sum>temp2?sum:temp2;103                       sum=sum>temp3?sum:temp3;104                       sum=sum>temp4?sum:temp4;105                  //     cout<<"1"<<" "<<i<<" "<<sum<<endl;106                       }107                  else if(a[i]=='w'||a[temp]=='w'){108                       int temp5=0,temp6=0,temp7=0,temp8=0;109                       if(a[i]=='w'){110                           temp5+=getres(i,'r');111                           temp5+=getres2(temp,a[temp]);112                            memset(vis,false,sizeof(vis));113                           temp6+=getres(i,'b');114                           temp6+=getres2(temp,a[temp]);115                           sum=sum>temp5?sum:temp5;116                           sum=sum>temp6?sum:temp6;117                           }118                       else if(a[temp]=='w'){119                            temp7+=getres2(temp,'r');120                            temp7+=getres(i,a[i]);121                            memset(vis,false,sizeof(vis));122                            temp8+=getres2(temp,'b');123                            temp8+=getres(i,a[i]);124                            sum=sum>temp7?sum:temp7;125                            sum=sum>temp8?sum:temp8;126                            }127                  //     cout<<"2"<<" "<<i<<" "<<sum<<endl;128                            }129                  else{130                            int temp9=0;131                            temp9+=getres2(temp,a[temp]);132                            temp9+=getres(i,a[i]);133                            sum=sum>temp9?sum:temp9;134                        //    cout<<"3"<<" "<<i<<" "<<sum<<endl;135                            }136                //  cout<<sum<<endl;137                            }138       //  printf("%d\n",sum);139         fprintf(fout,"%d\n",sum);  140     return 0;141 }142                       143