USACO section1.1 Broken Necklace

 1 /*
 2 ID: vincent63
 3 LANG: C
 4 TASK: beads 
 5 */
 6 #include <stdio.h>
 7 #include<stdlib.h>
 8 #include<string.h>
 9 int findmax(char s[],int n){
10     int i,j,count,max,l,r;
11     char left,right;
12     max=0;
13     count=0;
14     int p;
15     for(i=0,j=1;j<n;i++,j++){
16         
17         l=i;
18         r=j;
19         while(s[l]=='w'){
20             l--;
21             count++;
22         }
23         while(s[r]=='w'){
24             r++;
25             count++;
26         }
27         
28         left=s[l];
29         right=s[r];
30         
31         for(;l>=0;l--){
32             if(s[l]==left||s[l]=='w'){
33                 count++;
34             }else{
35                 break;
36             }
37         }
38         for(;r<n;r++){
39             if(s[r]==right||s[r]=='w'){
40                 count++;
41             }else{
42                 break;
43             }
44         }
45         if(count>max){
46             max=count;
47             p=i;
48             //printf("%dth count:%d\n",p,count);
49         }
50         count=0;
51     }
52     if(max>n/2){
53         return n/2;
54     }else{
55         return max;
56     }
57     
58 }
59 int main () {
60     FILE *fin  = fopen ("beads.in", "r");
61     FILE *fout = fopen ("beads.out", "w");
62     
63     int n;
64     char buf[10];
65     char res[10];
66     fgets(buf,10,fin);
67     n=atoi(buf); 
68          
69     char necklace[2*n+5];
70     char buf2[n+1];
71     
72     fgets(necklace,n+1,fin);
73     
74     strcpy(buf2,necklace);
75     
76     strcat(necklace,buf2);
77     int i;
78     /*
79     for(i=0;i<2*n;i++){
80         printf("%c",necklace[i]);
81     }
82     printf("\n");
83     */
84     int max;
85     max=findmax(necklace,2*n);
86     
87     //itoa(max,res,10);
88     sprintf(res,"%d\n",max);
89     fputs(res,fout);
90     //printf("%d\n",max);
91     
92     return 0;
93 }

 

posted @ 2016-11-25 23:23  侯胜滔  阅读(167)  评论(0编辑  收藏  举报