Broken Necklace(模拟)


 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 比较得出最大值,最后输出最大值即可算出。

Broken Necklace(模拟)_第1张图片
    first and wrong:

/*
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:

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	int N,i,length,j,temp,max,sum,k,FROM;
	char bead[700];
	char flag,flag1;
	memset(bead,0,sizeof(bead));
	scanf("%d",&N);
	scanf("%s",bead);
//printf("\n%s\n",bead);
//输出当时的珠子情况
    length=strlen(bead);
    
	temp=0;
    flag=bead[0];
    
	for(i=0;i<length;i++)//该操作为检查是否整个字符串中的字符都相等
      if(bead[i]==flag) temp++;
    if(temp==length)  printf("%d\n",length);
    
	else
    {
	
	for(i=length;i<2*length;i++)
      bead[i]=bead[i-length];
    bead[i]='\0';	  
//printf("\n%s\n",bead);
//将整个字符串在后面再补一次
	i=0;
    while(bead[i]=='w') i++;
    flag=bead[i];
    while(bead[i]==flag||bead[i]=='w') i++;
//    printf("i=%d\n",i);
//输出当时i的位置,从这个位置开始向前搜索
    j=i;     //初始值要定好
    max=0;
    flag=bead[i];
    FROM=i;
    while(j<(length-1)+FROM) //(length-1)+FROM为讲前面不符合的字符串后补后的长度
    {
//    	printf("from=%d,%c\n",i,flag);  //测试此时i位置的字符是什么
    	while(bead[j]=='w'||bead[j]==flag) j++;
    	flag1=bead[j];
    	k=j;
    	while(bead[j+1]==flag1||bead[j+1]=='w')  j++;
//    	printf("to=%d,%c\n",j,flag1);   //测试此时j位置的字符是什么
    	sum=j-i+1;
//    	printf("sum=%d\n",sum);        //测试此时符合题意的字符串为多少
    	if(max<sum) max=sum;
    	if(j!=k) i=k;
    	else i=j;
    	flag=bead[i];   //出错点:记录这时候的字符是什么,避免一下语句变成w字符
    	while(bead[i-1]=='w') i--;
//    	printf("from=%d\n",i);       //测试此时i为多少
//    	system("pause");       //每循环一次,停止一次查看此时所有输出情况
    }
	printf("%d\n",max);
       
    }
}
 AC finally:
/*
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,i,length,j,temp,max,sum,k,FROM;
	char bead[700];
	char flag,flag1;
	memset(bead,0,sizeof(bead));
	fscanf(fin,"%d",&N);
	fscanf(fin,"%s",bead);
        length=strlen(bead);
	temp=0;
        flag=bead[0];
        for(i=0;i<length;i++)
            if(bead[i]==flag) temp++;
        if(temp==length)  fprintf(fout,"%d\n",length);
	else
      {
	
	for(i=length;i<2*length;i++)
        bead[i]=bead[i-length];
        bead[i]='\0';	    
	i=0;
        while(bead[i]=='w') i++;
        flag=bead[i];
        while(bead[i]==flag||bead[i]=='w') i++;
        j=i;
        max=0;
        flag=bead[i];
        FROM=i;
            while(j<(length-1)+FROM)
            {
       	     while(bead[j]=='w'||bead[j]==flag) j++;  
             //第一次循环找到第一个与bead[i]和’w'都不同的字符
    	     flag1=bead[j];                           
             //记录这个不同的字符(此时是第一个)
    	     k=j; //用k记录这个位置(记录的不是字符是位置)
    	     while(bead[j+1]==flag1||bead[j+1]=='w')  j++;  
             //第二次循环找到最后一个与bead[j]或者‘w'相同的字符
    	     sum=j-i+1;       //此时可以确定长度大小
    	     if(max<sum) max=sum;   //并且比较此时长度大小
    	     if(j!=k) i=k;           
//判断j是否进行了第二次循环,就是判断bead[j]是不是大于1个(也可以是‘w')
    	     else i=j;       
//是则启用k记录的那个数,不是则i=j(其实可以不判断直接k=i也行,bead[j]或者’w'有多少个也无所谓)
    	     flag=bead[i];   
//先记住这时候的字符bead[i]因为i前面可能是’w'运行一下句子 就会变成bead[i]=‘w'那就不是我们考虑的对象了
    	     while(bead[i-1]=='w') i--;   //判断初始位置i前面有没有’w'
            }
         fprintf(fout,"%d\n",max);  
        }
exit(0);
}
    总结:
    如果思路不是非常清晰真的是写不出来,太弱了实在是,思路本来是正确的,但是因为没有考虑到特殊情况,然后一直怀疑自己是不是想错了。没想到最后改了个判断条件就AC了,题意不难理解,但是很容易把自己给绕进去,考虑问题不够仔细,并且Debug能力实在太弱,错的没发现出来,反而把对的给改错了,来来回回,兜兜转转,总算给做出来了。还有,定义变量最好用有意义的名字命名,不然真的会很混乱,哪个变量是干嘛都要弄得清清楚楚。为什么写不出代码,是因为想问题每一步都没想仔细,当然就写不出来,所以最好还是先把思路给理清楚。 
    另外,AC这道题后,自己总结一遍也很有必要,重温一下当时是怎么想的,再把所有思路给理清一遍,这样,才能真正学到心里去。

你可能感兴趣的:(模拟)