USACO Section1.1PROB Broken Necklace

有点麻烦的一道模拟(官方题解好像有复杂度为$O(n)$DP的姿势?,感觉好烦,以后再细看~

在一些细节上调试了很久很久,囧RZ

/*
ID: jusonal1
PROG: beads
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
char beads[1000];
int n;
int judge(int i) {
    if(i < 1) return n;
    else if(i > n) return 1;
    else return i;
}
int find_(int i) {
    int left = judge(i);
    int right = judge(i+1);
    int s_left = judge(i);
    int s_right = judge(i+1);
    int Left = judge(i);
    int Right = judge(i+1);
    int MAX = 0;
    while(beads[left] == beads[s_left]||beads[left] == ‘w‘) {
        ++MAX;
        if(beads[s_left] == ‘w‘) s_left = judge(--s_left);
        left = judge(--left);
        if(left == Right) return MAX+1;
    }

    while(beads[right] == beads[s_right]||beads[right] == ‘w‘) {
        ++MAX;
        if(beads[s_right] == ‘w‘) s_right = judge(++s_right);
        right = judge(++right);
        if(left == right) return MAX+1;
    }
    return MAX;
}
int main () {
    freopen("beads.in","r",stdin);
    freopen("beads.out","w",stdout);
    scanf("%d",&n);
    for(int i = 1;i <= n;++i) scanf(" %c",&beads[i]);
    int ans = 0;
    for(int i = 1;i <= n;++i) {
        ans = max(ans,find_(i));
    }
    printf("%d\n",ans);
    return 0;
}
时间: 2024-10-13 00:43:14

USACO Section1.1PROB Broken Necklace的相关文章

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 Section1.1 Broken Necklace 解题报告

beads解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 输入文件:第一行N:第二行一个字符串A,长度为N.字符串中,仅有r.w.b三种字符,分别代表红.白.蓝. A代表一串项链,有三种颜

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

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

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;//值得学

洛谷 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 假如你要在一些点打破项链,展开成一条直线,然后从一端开始收集同颜色的珠子直到你遇到一个不同的颜色珠子,在另一端做同样的事(颜色可能与在这之前收

USACO Section1.4 Mother&#39;s Milk 解题报告

milk3解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------------------------------------------------------------------------------------------[题目] 有三个桶,容量分别是A.B.C,开始C桶是满的. 你可以不断将某个桶的奶倒到另一个桶里,但只允许全倒过去,或者将后者倒满,前者留下

题解 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