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.
original 'split' v wwwbbrwrbrbrrbrbrwrwwrbwrwrrb|wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ******|***** rrrrrb|bbbbb <-- assignments 5 x r 6 x b <-- 11 total
题意:
有一串珠子,有 r.b.w 三种颜色,w 可以任意变为 r 或者 b 两者之一,在某切点处,让两边珠子的颜色连续相同,求出两边颜色相同连续的最大数。
思路:
先判断字符串是不是全部都相等,如果是则输出该字符串长短;
如不是,则要进一步分析:
1.先找到第二个是 r 或者 b 的点,从这点开始向后遍历,将前面忽略的点补到后面;
2.定义两个变量 i 和 j ,每次都是从 i 开始遍历,到j结束,用 sum = j - i + 1 代表长度,再将 sum 和 max 比较;
如何实现:
j代表从 i+1 开始遍历,找到与 bead[ i ] 不同且不是 w 的第一颗珠子,需要注意的是每次找到这个 j 后,还要循环一次,移动到最后一个与 bead[ j ] 相同或者为 w 的珠子上(因为前面找到的是第一个),并且在这个循环之前要用 k 将变量 j 的位置保存起来,因为在下面的 i = j 中, i 的位置是 j 的初始位置(就是 j 再循环一次的之前)。 j 进行两次循环后,通过判断 k 是否等于 j 来进行对 i 端进行初始化,若 j != k,说明 j 有移动过,就是说当 j 第一次循环后的 bead[ j ] 不止有一个,所以此时应该 i = k ;如果 j == k ,则 i = j (说明bead[ j ]只有一个)(其实不用判断也可以,无论 j 变与没变 直接 i = k 就可以了,因为无论 j 变了还是没变,k 记录的永远是 j 的初始位置);但是这又有要注意的地方, i 初始化后,也许 i 前面含有 w 珠子,则 i 应该移到最前面 的w 的地方;
3.在循环中,通过 sum = j - i + 1 跟 max 比较得出最大值,最后输出最大值即可算出。
/* TASK:beads LANG:C ID:sum-g1 */ #include<stdio.h> #include<string.h> int main() { FILE *fin =fopen("beads.in","r"); FILE *fout=fopen("beads.out","w"); int N,flag,FROM,from,sum,max; int i,k; int temp; fscanf(fin,"%d",&N); char bead[400]; char map; memset(bead,0,sizeof(bead)); fscanf(fin,"%s",bead); flag=0; for(i=0;i<N;i++) { if(flag==0&&bead[i]!='w') {from=i;flag++;} else if(flag==1&&bead[i]!='w'&&bead[i]!=bead[from]) {FROM=i;break;} } from=FROM; map=bead[FROM]; while(bead[FROM+1]=='w'||bead[FROM+1]==map) FROM++; for(i=N;i<N+FROM+1;i++) bead[i]=bead[i-N]; bead[N+FROM+1]='\0'; flag=0; sum=0; max=0; k=0; i=from; while(i<N+FROM+1) //循环正确,但是没有考虑到全部珠子相等时候的情况 { //并且没有考虑到不相同时,刚好满足条件的为正串珠子数量的情况 i=from+1; while(bead[i]==bead[from]||bead[i]=='w') i++; sum+=i-from; from=i; flag++; if(flag%2) k=i; else { if(max<sum) max=sum; from=k; sum=0; temp=from; while(bead[temp-1]=='w') { sum++; temp--; } } } fprintf(fout,"%d\n",max); exit(0); }Debug and test: