"USACO1.1" Broken necklace

Source: Internet
Author: User

Test instructions

A ring necklace, there are rbw three kinds of beads, R for Red,b on behalf of the BLUE,W on behalf of White, from any position, the two ends take beads, the same end of the beads to the same color, W can be dyed to the desired color, that can be used as r can also be used as B, the maximum number of beads obtained. A maximum of 350 beads.

Analysis

You can enumerate the broken positions and then simulate the beads. You can also raise the position of a point. You can also do DP.

Code

Enumerate breakpoints

#include<cstdio> 
#include<algorithm>
#Define N 355
using namespace STD;

CharA
intB[n];
intN,ans;

int solve(int p,int dir) //p is the starting point, dir is the direction and the maximum number of
{
intlen=0;//number of beads taken, up to n beads

for(intJ=p+n; len<n; Len++,j+=dir)//j is the current position +n, the current position is j%n
{
if(B[p] && b[j%n] && b[j%n]!=b[p])
Break;
if(!b[p])//Every time the beads are in accordance with the color of the P position, if the P position is w, it is necessary to update P
p=j%n;
}
returnLen
}
int main()
{
scanf("%d", &n);
for(intI=0; i<n; i++)
{
A=getchar ();
b[i]=a==' B '?1: a==' R '?2:0;//b--1 r--2 w--0
}
for(intI=0; i<n; i++)//Enumerate breakpoints
Ans=max (Ans,solve (i,-1) +solve (i+1,1));
Ans=min (Ans,n);//may have been calculated two times, but only in the case of all
printf("%d\n", ans);
return 0;
}

a lift.

#include<cstdio> 
#include<algorithm>
#include<iostream>
#include<string>
using namespace STD;
intN,ans;
stringS
int main()
{
Cin>>n>>s;
S+=s;
for(intI=0, J; i<n; i++)//With I for the beginning of the first paragraph, not the cutting position
{
CharNow=s[i];
intlen=0;
inttimes=now==' W '?3:2;//If the current is white, then need to find three pieces of the same color or find two paragraph
J=i;
while(times--)
{
while(j<i+n&& (s[j]==now| | s[j]==' W '))
{
len++;
j + +;
}
NOW=S[J];
}
Ans=max (Ans,len);
}
cout<<ans<<endl;
return 0;
}

Dp

#include<cstdio> 
#include<iostream>
#include<algorithm>
#include<string>
#define N 720
using namespace STD;
intN,ans;
stringS
intl[n][2],r[n][2];
int main() {
Cin>>n>>s;
S+=s;
for(intI=1;i<2*n;i++) {
l[i][0]=l[i-1][0]+1;
l[i][1]=l[i-1][1]+1;
if(s[i-1]==' R ')
l[i][1]=0;
Else if(s[i-1]==' B ')
l[i][0]=0;
}
for(intI=2*n-1; i>=0; i--) {
r[i][0]=r[i+1][0]+1;
r[i][1]=r[i+1][1]+1;
if(s[i]==' R ')
r[i][1]=0;
Else if(s[i]==' B ')
r[i][0]=0;
}
for(intI=0;i<2*n;i++)
Ans=max (Ans,max (l[i][0],l[i][1]) +max (r[i][0],r[i][1]));
Ans=min (Ans,n);
cout<<ans<<endl;
return 0;
}

"USACO1.1" Broken necklace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.