USACO 1.1 Broken Necklace

[报告]

   首先,把环打开——就是str=str+str+str(我的程序在最前面+了个'\0',这是我多年PASCAL与C++混写的习惯),然后从第N+1位到第N+N位,以那位与它前面那个之间的空隙进行枚举。并且向左搜索一次,向右搜索一次。求出要你求的东西,然后和ANS比较,求出最大的就可以拉!

   USACO 1~3 都是些比较简单的题目,我相信。

[程序]


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
ifstream fin ("beads.in");
ofstream fout ("beads.out");
long n;
string str;
long ans;
inline void init()
{
       fin >> n;
       fin >> str;
       str='\0'+str+str+str;
}
inline void calc()
{
       ans=0;
       for (long m=n+1;m<=n+n;m++)
       {
       //    cout << m << endl;
           char x='w';
           long s=0;
           for (long i=m-1;i>=1&&(str[i]=='w'||x=='w'||str[i]==x);i--,s++)
               if (str[i]!='w')
                  x=str[i];
      //     cout << x << " " << s << " | ";
           x='w';
           for (long i=m;i<=n+n+n&&(str[i]=='w'||x=='w'||str[i]==x);i++,s++)
               if (str[i]!='w')
                  x=str[i];
         //  cout << x << " " << s << endl;
           if (s>ans)
           {
              ans=s;
              if (ans>n)
              {
             

posted @ 2009-12-15 15:38  为美好世界献上珂学  阅读(88)  评论(0)    收藏  举报