Broken Necklace

/*
ID: lxlenovos1
PROG: beads
LANG: C++
*/

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

typedef struct ring
{
        char a;
        struct ring *flink;
        struct ring *slink;
} ring;


ring* sring = NULL;
ring* hring = NULL;     /* the first node */
ring* ering = NULL;     /* the end node */
int fcout = 0;
int rcout = 0;
ring* bring = NULL;

void ring_pushback( ring* r, int n )
{
        static int i = 0;

        if ( i == 0 )
        {
                sring = r;
                ++i;

               ++i;
                hring = r;
        }
        else
        {
                sring->slink = r;
                r->flink = sring;

                sring = r;

                /* the end node */
                if ( i == n - 1 )
                {
                        sring->slink = hring;
                        hring->flink = sring;
                        ering = sring;
                }

                ++i;
        }

}


int get_maxresult( ring *gring )
{
        // cout the list
        ring* p = NULL;
        char tempvalue;

        fcout = 1;
        rcout = 1;
        p = gring;
        tempvalue = p->a;

        //cout << "temp1 is " << tempvalue << endl;     

        do
        {

              //tempvalue = p->a;
                p = p->slink;

                //cout  << p->a << endl; 
                if ( p->a == tempvalue || p->a == 'w' || tempvalue == 'w' )
                {
                        if ( tempvalue == 'w' )
                                tempvalue = p->a;



                        if ( 0 == fcout )
                                fcout = 1;
                        ++fcout;
                        //cout << "1" << endl;
                }
                else
                {
                        //cout << "2" << endl;
                        break;
                }

        }while( p != gring );

        if ( p == gring )
                goto __cout;

        p = gring->flink;
        tempvalue = p->a;

        //cout << "temp2 is " << tempvalue << endl;

        do
        {
              //tempvalue = p->a;
              p = p->flink;

              //cout << p->a << endl;

          if ( p->a == tempvalue || p->a == 'w' || tempvalue == 'w' )
                {
                        if ( tempvalue == 'w' )
                                tempvalue = p->a;

                        if ( 0 == rcout )
                                rcout = 1;
                        ++rcout;
                        //cout << "1" << endl;
                }
                else
                {
                        //cout << "2" << endl;
                        break;
                }

        }while( p != gring );

        return ( rcout + fcout );

        __cout:
                rcout = 0;
                fcout--;
                return ( rcout + fcout );
}


int main( void )
{

        ofstream fout( "beads.out" );
        ifstream fin( "beads.in" );

        int n, i, result;
        char temp;
        int max = 0;
        ring* g = NULL;

        //cin >> n;
        fin >> n;

        for ( i = 0; i < n; ++i )
        {
                //cin >> temp;
                fin >> temp;

                ring* bread = new ring;
                bread->a = temp;
                bread->flink = NULL;
                bread->slink = NULL;

                ring_pushback( bread, n );
        }

        //cout << "cin is end and the result is:" << endl;

        g = hring;
        for ( i = 0; i < n; ++i )
        {
                result = get_maxresult( g );
                //cout << "result is " << result << endl;

                if ( result > max )
                        max = result;

                if ( max >= n )
                {
                        max = n;
                        break;
                }

                g = g->slink;
                //cout << g->a << endl;
        }

        //cout << max << endl;
        fout << max;
        fout << endl;

        exit( 0 );
}

  

原文地址:https://www.cnblogs.com/lxgeek/p/2134233.html