• 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
    ",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("
    ");
    83     */
    84     int max;
    85     max=findmax(necklace,2*n);
    86     
    87     //itoa(max,res,10);
    88     sprintf(res,"%d
    ",max);
    89     fputs(res,fout);
    90     //printf("%d
    ",max);
    91     
    92     return 0;
    93 }
  • 相关阅读:
    Linux用户态程序计时方式详解
    我的代码重构经验
    基于链表的C语言堆内存检测
    C语言内存使用的常见问题及解决之道
    1151 LCA in a Binary Tree (30 分)
    1150 Travelling Salesman Problem (25 分)
    1149 Dangerous Goods Packaging (25 分)
    1148 Werewolf
    1155 Heap Paths (30 分)
    1154 Vertex Coloring (25 分)
  • 原文地址:https://www.cnblogs.com/houshengtao/p/6103322.html
Copyright © 2020-2023  润新知