zoukankan      html  css  js  c++  java
  • 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 );
    }
    

      

  • 相关阅读:
    Kafka2.0服务端写入和读取流程
    Kafka2.0服务端启动源码
    Kafka2.0消费者协调器源码
    Kafka2.0消费者客户端源码分析
    [六省联考2017]分手是祝愿 题解(期望dp)
    [信息学奥赛一本通oj1741]电子速度 题解
    [CSP-S模拟测试53]题解
    [CSP-S模拟测试52]题解
    [CSP-S模拟测试ex]题解
    [CSP-S模拟测试51]题解
  • 原文地址:https://www.cnblogs.com/lxgeek/p/2134233.html
Copyright © 2011-2022 走看看