USACO 1.1 Broken Necklace

O(n) search. I totally messed it up. My code seems like shit!


/*
    ID: xlyleon1
    PROG: beads
    LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
char beads[400];
int N;

bool check()
{
    bool blue = false, red = false;
    for(int i=0;i<N;i++)
    {
        if(beads[i] == 'r')
            red = true;
        else if(beads[i] == 'b')
            blue = true;
    }
    return (!blue || !red);
}

void reduceWhite(int firstnotwhite)
{
    char lastcolor = beads[firstnotwhite];
	for(int i=(firstnotwhite+1)%N;i!=firstnotwhite;i=(i+1)%N)
	{
        //cout << "I: " << i << endl;
		if(beads[i] == 'w')
		{
            int j;
			for(j=(i+1)%N;j!=firstnotwhite;j=(j+1)%N)
                if(beads[j] != 'w')
                {
                    if(beads[j] == lastcolor)
                        for(int k=i;k!=j;k=(k+1)%N)
                            beads[k] = lastcolor;
                    i = j;
                    break;
                }
            if(j == firstnotwhite)
            {
                if(lastcolor == beads[firstnotwhite])
                    for(int k=i;k!=firstnotwhite;k=(k+1)%N)
                        beads[k] = lastcolor;
                return;
            }
		}
        lastcolor = beads[i];
        //cout << "lastcolor: " << lastcolor <<" i: " << i <<endl;
	}
}

int computemax(int firstnotwhite, ostream& ostr)
{
    int flen = 0, slen = 1, wlen1 = 0, wlen2 = 0;
    int nextindex, firstlen, firstwlen = 0, result = -1;
    //cout << "firstnotwhite: " << firstnotwhite << endl;
    if(firstnotwhite == 0)
    {
        int i;
        for(i=N-1;i>0;i--)
            if(beads[i] != beads[firstnotwhite])
                break;
        //cout << "i: " << i << endl;
        firstnotwhite = (i+1)%N;
    }
    //cout << "firstnotwhite: " << firstnotwhite << endl;
    int i;
    for(i=(firstnotwhite+N-1)%N;i!=firstnotwhite;i=(i+N-1)%N,wlen1++)
        if(beads[i] != 'w')
            break;
    int j;
    for(j=(firstnotwhite+1)%N;j!=firstnotwhite;j=(j+1)%N,slen++)
        if(beads[j] != beads[firstnotwhite])
             break;
    int k;
    for(k=j;;k=(k+1)%N,wlen2++)
        if(beads[k] != 'w')
            break;
    firstwlen = wlen2;
    //cout << "firstwlen: " << firstwlen << endl;
    slen += wlen2;
    firstlen = slen;
    nextindex = k;
    //cout << "firstlen: " << firstlen << endl;

    //cout << "first nextindex: " << nextindex << endl;
    int c = 0;
    while(1)
    {
        flen = slen + wlen1;
        wlen1 = wlen2;
        //cout << "nextindex: " << nextindex << endl;
        if(nextindex == firstnotwhite)
        {
            if((flen + firstlen) > result)
                result = flen + firstlen;
            if(c == 1)
                result -= firstwlen;
            //cout << "result: " << result << endl;
            //cout << flen << " " << firstlen << endl;
            break;
        }
        slen = 1;
        wlen2 = 0;
        int i;
        for(i=nextindex+1;beads[i]==beads[nextindex];i++,slen++);
        int j;
        for(j=i;j<N&&beads[j]=='w';j++,wlen2++);
        slen += wlen2;
        nextindex = j%N;

        if((flen + slen) > result)
            result = flen + slen;
        c++;
        //cout << "result: " << result << endl;
    }
    return result;
}

int main()
{
    ifstream istr("beads.in");
    ofstream ostr("beads.out");

    istr >> N;
    istr.get();
    for(int i=0;i<N;i++)
    	istr >> beads[i];

    if(check())
    {
        ostr << N << endl;
        return 0;
    }

    int firstnotwhite;
    for(firstnotwhite=0;firstnotwhite<N;firstnotwhite++)
        if(beads[firstnotwhite] != 'w')
            break;
    reduceWhite(firstnotwhite);
    for(firstnotwhite=0;firstnotwhite<N;firstnotwhite++)
        if(beads[firstnotwhite] != 'w')
            break;
    //for(int i=0;i<N;i++)
        //cout << beads[i];
    //cout << endl;

    ostr << computemax(firstnotwhite, ostr) << endl;

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值