首页 > 其他 > 详细

USACO 1.1 Broken Necklace (beads)

时间:2014-02-18 15:06:37      阅读:390      评论:0      收藏:0      [点我收藏+]
/*
ID: haolink1
PROG: beads
LANG: C++
*/
//#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
    ifstream fin ("beads.in");
    unsigned int num;
    fin >> num;
    string beads;
    fin >> beads;
    if(beads.length() != num){
        cout<<"error"<<endl;
        return 0;
    }
    int max_num = 0;
    //Brute force !
    for(unsigned int i = 0;i < num;i++){
        int cur_num = 0;
        int diff = 0;
        //just choose a arbitrary char different from ‘w‘ ‘b‘ and ‘r‘;
        char sample = ‘n‘;
        //we transform the bidirectional collection in to unidirectional,
        //and when the beads‘color difference occur twice, the collection stop;
        for(unsigned int j = 0;j < num;j++){
            if(beads[(i+j)%num]!=‘w‘ && beads[(i+j)%num]!=sample){
                sample = beads[(i+j)%num];
                diff++;
                if(diff<=2)
                    cur_num++;
                else break;
            }else
                cur_num++;
        }
        max_num=(cur_num>max_num)?cur_num:max_num;
    }
    ofstream fout ("beads.out");
    fout<<max_num<<endl;
    return 0;
}

USACO 1.1 Broken Necklace (beads)

原文:http://blog.csdn.net/damonhao/article/details/19362697

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!