USACO section1.1 Broken Necklace

这道题理解了题目的含义就很简单了;

如果题目改成每次只统计一端,遇到不同时开始统计另一端,并且只有相邻两次统计的(可以转化)为同一种颜色时,才能累加,难度会很高,可能要DP了;

/*

PROG: beads

LANG: C++

*/

# include <cstdio>

# include <cstring>



# define N 350 + 10



int n;

char s[N];



int count(char *s, int k)

{

    int ret = 0;



    int i = k;

    int j = (k-1+n) % n;



    char cur;

    while(ret < n && s[i] == 'w') {++ret; i = (i+1)%n;}

    cur = s[i];

    while (ret < n && (s[i]==cur || s[i]=='w')) {++ret; i = (i+1)%n;}

    while (ret < n && s[j] == 'w') {++ret; j = (j+n-1)%n;}

    cur = s[j];

    while (ret < n && (s[j]==cur || s[j]=='w')) {++ret; j = (j-1+n)%n;}



    return ret;

}



int main()

{

    freopen("beads.in", "r", stdin);

    freopen("beads.out", "w", stdout);



    int max = 0;

    scanf("%d%s", &n, s);

    for (int i = 0; s[i]; ++i)

    {

        int cur = count(s, i);

        if (cur > max) max = cur;

    }

    printf("%d\n", max);



    fclose(stdin);

    fclose(stdout);



    return 0;

}

/**/

你可能感兴趣的:(USACO)