标签:处理 ber format sample cti put algorithm iostream figure
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.
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 can include any of 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.
Line 1: | N, the number of beads |
Line 2: | a string of N characters, each of which is r, b, or w |
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
A single line containing the maximum of number of beads that can be collected from the supplied necklace.
11
Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
Two necklace copies joined here v wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ******|***** rrrrrb|bbbbb <-- assignments 5xr .....#|##### 6xb 5+6 = 11 total
就是暴力,然而开始的时候有很多细节没注意到。比如起始点如果是w,比如当flag>n时的处理……总之WA了好几遍才A。而且觉得代码写的很复杂。
1 #include <iostream> 2 #include <cmath> 3 #include <cstring> 4 #include <cstdio> 5 #include <cstdlib> 6 #include <algorithm> 7 using namespace std; 8 int a[355]; 9 int main() 10 { 11 freopen("beads.in","r",stdin); 12 freopen("beads.out","w",stdout); 13 int n; 14 scanf("%d",&n); 15 for(int i=1;i<=n;i++) 16 { 17 char s0; 18 cin>>s0; 19 //if(s0==‘ ‘) {i--;continue;} 20 if(s0==‘b‘) a[i]=2; 21 else if(s0==‘r‘) a[i]=1; //这里如果不是 else if而是 if就会错 22 else a[i]=0; 23 } 24 int maxn=0; 25 for(int i=1;i<=n;i++) 26 { 27 int flag=i,color=a[i]; 28 int cnt=0; 29 if(color==0) //起点为白 30 { 31 int j=flag,c=0; 32 while(a[j]==0) 33 { 34 j--; 35 if(j<1) j=n; 36 c++; 37 if(c==n) {printf("%d\n",n);return 0;} //全部为白 38 } 39 color=a[j]; 40 } 41 while(a[flag]==color || a[flag]==0) //往前找 42 { 43 cnt++; 44 flag--; 45 if(flag<1) flag=n; 46 if(cnt==n) break; 47 } 48 flag=i+1; 49 if(i+1>n) {color=a[1];flag=1;} 50 else color=a[i+1]; 51 if(color==0) 52 { 53 int j=flag,c=0; 54 while(a[j]==0) 55 { 56 j++; 57 if(j>n) j=1; 58 c++; 59 if(c==n) {printf("%d\n",n);return 0;} 60 } 61 color=a[j]; 62 } 63 while(a[flag]==color || a[flag]==0) //往后找 64 { 65 if(cnt==n) break; 66 cnt++; 67 flag++; 68 if(flag>n) flag=1; 69 } 70 maxn=max(maxn,cnt); 71 } 72 printf("%d\n",maxn); 73 //system("pause"); 74 return 0; 75 }
标签:处理 ber format sample cti put algorithm iostream figure
原文地址:http://www.cnblogs.com/lulala/p/7208358.html