1 条题解

  • 0
    @ 2026-9-30 12:57:32

    C++ :

    
    #include <cstdio>
    /* USACO Broken Necklace:n 个切口(i-1 | i)。切口两侧分别向外数:
       颜色由第一次遇到的非 w 决定,w 视为通配;两侧总数不超过 n。 */
    static char s[1005];
    int main(){
        int n;
        if(scanf("%d", &n) != 1) return 0;
        scanf("%s", s);
        int best = 0;
        for(int i = 0; i < n; i++){
            int c1 = 0; char col1 = 0;
            for(int j = 0; j < n; j++){
                char c = s[(i + j) % n];
                if(c == 'w') c1++;
                else if(col1 == 0){ col1 = c; c1++; }
                else if(c == col1) c1++;
                else break;
            }
            int c2 = 0; char col2 = 0;
            for(int j = 0; j < n; j++){
                char c = s[(i - 1 - j + 2 * n) % n];
                if(c == 'w') c2++;
                else if(col2 == 0){ col2 = c; c2++; }
                else if(c == col2) c2++;
                else break;
            }
            int tot = c1 + c2;
            if(tot > n) tot = n;
            if(tot > best) best = tot;
        }
        printf("%d\n", best);
        return 0;
    }
    
    
    • 1

    信息

    ID
    2821
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者