You have a necklace (3 <= N <= 350) by N or blue beads red, white composition, the beads are arranged at random. Here are two examples of n = 29:
The first and second beads in the picture have been marked.
A picture of the necklace can be represented by the following string:
brbrrrbbbrrrrrbrrbbrbbbbrrrrb
If you want to break the necklace at some point, expand into a straight line, and then began to collect the same color of beads from one end until you hit a different color beads at the other end to do the same thing (color may vary with the previously collected in this ). Where should the necklace break determined to collect the greatest number of number of sub. Example For example, in the picture A necklace, the beads can be collected to 8, necklace interrupted between 9 and bead 10 or bead 24 and bead 25 beads. In some necklace, beads including a white image as shown in B. When collecting beads, a white bead may be encountered are as red as blue can be. Performance necklace string will include three symbols r, b, and w. Write a program to determine can be collected from the supplied necklace maximum number of beads.
The first pass: The most straightforward
Ideas:
1, through each character encounters 'w' of the pass, 'r' and 'b' perform operations
2, the character for performing an operation, in two directions, because two out of chain scission; for each direction, further comprising two sub-direction, while loop chain crawl
1 #include <iostream>
2 #include <cstring>
3 using namespace std;
4 char arr[360];
5 int n;
6 void solve()
7 {
8 //Init
9 memset(arr,0,360*sizeof(char));
10 cin>>n;
11 cin>>arr;
12 //solveIt
13 int j,Max=0;
14 for(int i=0; i<n; i++)
15 {
16 if(arr[i]=='w')
17 continue;
18 //pass 'w',from 'r'and'b'
19 int max1=0,ct=1;
20 char ch = arr[i];
21 j = i;
22 bool jdg = true;
23 //inOrder
24 while(jdg)
25 {
26 //ct=1/ct=2
27 if(ct==1&&(arr[j]=='w'||arr[j]==ch))
28 {
29 j = (j+1)%n;
30 max1++;
31 }else if(ct==1&&arr[j]!=ch){
32 j = (i==0)?(n-1):(i-1);
33 ct++;
34 if(arr[j]=='w'||arr[j]==ch){
35 max1++;
36 j = (j==0)?(n-1):(j-1);
37 }
38 else
39 {
40 max1++;
41 ch = arr[j];
42 j = (j==0)?(n-1):(j-1);
43 }
44 }else if(ct==2&&(arr[j]=='w'||arr[j]==ch))
45 {
46 max1++;
47 j = (j==0)?(n-1):(j-1);
48 }else if(ct==2&&arr[j]!=ch&&arr[j]!='w')
49 {
50 jdg = false;
51 }
52 else
53 continue;
54 //general
55 if(max1>n){
56 max1 = n;
57 jdg=false;
58 }
59 }
60 //backOrder
61 int max2=0;
62 ct=1;
63 jdg = true;
64 j = (i==0)?(n-1):(i-1);
65 ch = arr[j];
66 while(jdg)
67 {
68 if(ct==1&&(arr[j]=='w'||arr[j]==ch)){
69 j = (j==0)?(n-1):(j-1);
70 max2++;
71 }else if(ct==1&&arr[j]!=ch){
72 j = i;
73 ct++;
74 if(arr[j]=='w'||arr[j]==ch){
75 max2++;
76 j = (j+1)%n;
77 }else{
78 ch = arr[j];
79 max2++;
80 j = (j+1)%n;
81 }
82 }else if(ct==2&&(arr[j]=='w'||arr[j]==ch)){
83 max2++;
84 j = (j+1)%n;
85 }else if(ct==2&&arr[j]!='w'&&arr[j]!=ch){
86 jdg=false;
87 }
88 if(max2>n){
89 max2 = n;
90 jdg=false;
91 }
92 }
93 Max = Max>max1?Max:max1;
94 Max = Max>max2?Max:max2;
95 }
96 if(Max)
97 cout<<Max<<endl;
98 else
99 cout<<n<<endl;
100 }
101 int main()
102 {
103 solve();
104 return 0;
105 }