USACO Broken Necklace 通过了

终于通过了,呵呵:

/*
ID: qq104801
LANG: C
TASK: beads
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* for debug only:counter
*/
void debug_dummy(void)
{
    return;
}

int n;
char k[800];

int count()
{
    int i;
    int a,b,w;
    int m;
    char c;
    memcpy(k+n,k,n);
    //printf("%s\n",k);

    a=0;
    b=0;
    w=0;
    i=0;
    m=0;
    c=‘0‘;
    while(i < (2*n))
    {
        if(k[i]==‘w‘)
        {
            b++;
            w++;
        }
        else if(k[i]==c)
        {
            b++;
            w=0;
        }
        else
        {
            if(m<(a+b))
                m=a+b;
            a=b-w;
            b=w+1;
            w=0;
            c=k[i];
        }
        //printf("%2d  %c %d %d\n",i,k[i%n],a,b);
        if(m<(a+b))
            m=a+b;
        //printf("%c-%3d-i:%2d   a:%2d   b:%2d   w:%2d\n",k[i],i,i%n,a,b,w);
        i++;
    }

    if(m>n)
        m=n;
    //printf("m:%d\n",m);
    return m;
}

main () {
    FILE *fin = fopen ("beads.in", "r");
    FILE *fout = fopen ("beads.out", "w");   

    fscanf(fin,"%d",&n);
    fscanf(fin,"%s",k);
    //printf("n:%d\n",n);
    //printf("k:%s\n",k); 

    fprintf(fout,"%d\n",count());
    fclose(fin);
    fclose(fout);
    exit (0);
}

结果:

USER: ll tom [qq104801]
TASK: beads
LANG: C

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.003 secs, 2164 KB]
   Test 2: TEST OK [0.005 secs, 2164 KB]
   Test 3: TEST OK [0.003 secs, 2164 KB]
   Test 4: TEST OK [0.005 secs, 2164 KB]
   Test 5: TEST OK [0.008 secs, 2164 KB]
   Test 6: TEST OK [0.003 secs, 2164 KB]
   Test 7: TEST OK [0.005 secs, 2164 KB]
   Test 8: TEST OK [0.005 secs, 2164 KB]
   Test 9: TEST OK [0.003 secs, 2164 KB]

All tests OK.

Your program (‘beads‘) produced all correct answers! This is your submission #7 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
29
ww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rwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwb

Keep up the good work!
Thanks for your submission!

呵呵,中途回老家呆了几天,以至于现在才通过。

时间: 2024-12-16 12:58:14

USACO Broken Necklace 通过了的相关文章

USACO Broken Necklace

这么个所谓简单的题目弄了一下午加一晚上,呵呵,怎么也算不对. 一定会有一个简单的方法. 晚上去新都回来后,又坐在电脑面前思索这个问题. 多次删除写出的眼看就要成功的代码,因为不够简洁. 突然顿悟: 1.串相联: 假设数数当前位置为pos; 2.k[pos]==‘w',不可能是最大. 3.k[pos+1]=='w',不可能是最大. 4.k[pos]=k[pos+1],不可能是最大. 5.pos==0,不能往左数. 所以, 6.只有k[pos]!=k[pos+1]才有可能数出最大: 即'r','b'

USACO Broken Necklace 题解(环展开成链,枚举)

题目大意:有一个项链,由红.蓝.白三种颜色的珠子组成,然后现在选择项链中的某一处断开,然后沿断开处的两个珠子分别查找,直至找到一个颜色不同的珠子,并统计个数(其中颜色以第一个非白色的为准,白色的珠子可以视为红色,也可以视为蓝色),要求找到的珠子个数的最大值. 分析:首先读取项链字符串,考虑到这是一个环,可以在字符串尾部再添加一个同样的项链,这样当向后寻找的时候如果需要超过头结点时候可以直接通过查找第二个项链即可,之所以向后寻找时候不必担心会多查找,是因为只要项链中存在一个颜色不同的珠子必然会停止

USACO 1.1 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

洛谷 P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 n=29 的二个例子: 第一和第二个珠子在图片中已经被作记号. 图片 A 中的项链可以用下面的字符串表示: brbrrrbbbrrrrrbrrbbrbbbbrrrrb 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

Section1.1 -- 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

题解 P1203 【[USACO1.1]坏掉的项链Broken Necklace】

[USACO1.1]坏掉的项链Broken Necklace 22892 破碎的项链 方法一:很容易想到枚举断点,再分别两头找,但是要注意很多细节 #include<iostream> #include<string> #include<cstdio> using namespace std; string s; int n,l,r,ll,rr,tmp,ans; inline int calc(int x) { ll=s[x],rr=s[x+1],l=x-1,r=x+2

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;

USACO 1.1 Broken Necklace(USACO官方)

本题我不会写,囧,看了官方的代码,算法复杂度为O(n^2),学习下: /* ID:twd30651 PROG:beads LANG:C++ */ #include<iostream> #include<fstream> #include<string.h> using namespace std; int N; char s[400]; int len; int mod(int n,int m) { while(n<0)n+=m; return n%m;//值得学