USACO--1.1Broken Necklace+暴力

来源:互联网 发布:网上买码用哪个软件 编辑:程序博客网 时间:2024/06/06 07:50

n只有350,所以我们可以直接枚举每个点作为打断点,然后再统计其左右同颜色珠子的最大值,然后取这些最值中的最大值。

代码如下:

/*ID: 15674811LANG: C++TASK: beads*/#include<iostream>#include<cstdio>#include<cstring>#include<fstream>using namespace std;int main(){    ofstream fout("beads.out");    ifstream fin("beads.in");    char str[1000];    int len;    while(fin>>len)    {         fin>>str+1;         int ans=0;         for(int i=1;i<=len;i++)         {             int tmp=0;             int j=i,t=i;             char ch=str[j];             while(true)             {                 if(str[j]==ch||str[j]=='w')                 {                    tmp++;                    j++;                    if(j>len)                       j-=len;                    if(j==t)                       break;                 }                 else if(str[j]!=ch)                 {                    if(ch=='w')                    {                         tmp++;                         ch=str[j];                         j++;                         if(j>len)                           j-=len;                         if(j==t)                           break;                     }                     else                        break;                 }             }             j=(i-1)==0?len:i-1;             t=j; ch=str[j];             while(true)             {                 if(str[j]==ch||str[j]=='w')                 {                    tmp++;                    j--;                    if(j==0)                       j=len;                    if(j==t)                       break;                 }                 else if(str[j]!=ch)                 {                     if(ch=='w')                     {                         tmp++;                         ch=str[j];                         j--;                         if(j==0)                           j=len;                         if(j==t)                           break;                     }                     else                        break;                 }             }             if(tmp>len)                tmp=len;             ans=max(ans,tmp);         }         fout<<ans<<endl;    } return 0;}
0 0
原创粉丝点击