Usaco1.1broken NECKLACE[DP as Death]

Source: Internet
Author: User

Title Description

You have a necklace (3<=n<=350) made of N red, white, or blue beads, which are arranged randomly. Here are two examples of n=29:

The first and second beads have been marked in the picture.

The necklace in picture A can be represented by the following string:

Brbrrrbbbrrrrrbrrbbrbbbbrrrrb

If you want to break the necklace at some point, expand into a straight line, and then start collecting the same color beads from one end until you meet a different color bead and do the same thing at the other end (the colors may be different from those collected before). Determine where the necklace should be broken to collect the maximum number of beads.

For example, in the necklace in picture A, the break necklace between bead 9 and bead 10 or bead 24 and bead 25 can collect 8 beads.

What does the white bead mean?

Also included in some necklaces are white beads (piece B) shown.

When collecting beads, a white bead that is encountered can be seen as red or blue.

The string containing the Gaultheria necklace will consist of three symbols R, B and W.

Write a program to determine the maximum number of beads that can be collected from a given necklace.

Input/output format

Input format:

Line 1th: N, number of beads

Line 2nd: A string of length n, each character is R, B, or W.

Output format:

Input and Output Sample input example # #:
Wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
Sample # # of output:
11
Description

The title translation comes from Nocow.

Usaco Training Section 1.1

----------------------------------

Search is too boring, and began to use DP to die, linear complexity can be;

Just make sure it's not all the same

F: Starting from I d: to I end

0: Red 1: Blue

Find a cutoff point for R and B, from which you can start a pleasant scan update.

Because it's a loop, I've got a loop tag, and I'm looping it anyway.

[PS]: Looks like someone else's time to search and I almost, N is too small, alas

////main.cpp//usaco1.1////Created by ABC on 16/8/14.//copyright©2016 Year ABC. All rights reserved.//#include<iostream>#include<cstdio>#include<cmath>using namespacestd;Const intn= -;intn,ans=1;CharC[n];intf[n][2],d[n][2],only=0;//Only 0--->all same//0 Red 1 BlueInlineintNxtinti) {    return(i+1)%N;} InlineintLstinti) {    return(I-1+n)%N;}voidinit () { for(intI=0; i<n;i++)if(c[i]=='R') only=1; if(only==0)return;  for(intI=0; i<n;i++)if(c[i]=='b') only=1; if(only==0)return; intst=0;  for(intI=1; i<n;i++)if(ABS (c[i]-c[i-1])== -) {st=i-1; Break;} BOOLloop=0;  for(inti=st;loop==0|| I!=st;i=lst (i)) {//printf ("For1%d\n", i);loop=1; if(c[i]=='R') f[i][0]=F[NXT (i) [0]+1, f[i][1]=0; if(c[i]=='b') f[i][1]=F[NXT (i) [1]+1, f[i][0]=0; if(c[i]=='W') f[i][0]=F[NXT (i) [0]+1, f[i][1]=F[NXT (i) [1]+1; } St++;loop=0;  for(inti=st;loop==0|| I!=ST;I=NXT (i)) {//printf ("For2%d\n", i);loop=1; if(c[i]=='R') d[i][0]=d[lst (i) [0]+1, d[i][1]=0; if(c[i]=='b') d[i][1]=d[lst (i) [1]+1, d[i][0]=0; if(c[i]=='W') d[i][0]=d[lst (i) [0]+1, d[i][1]=d[lst (i) [1]+1; }    }intMainintargcConst Char*argv[]) {scanf ("%d",&N);  for(intI=0; i<n;i++) cin>>C[i];    Init (); if(only==0) ans=N; Else  for(intI=0; i<n;i++){            intT=max (f[i][0]+d[lst (i) [1],f[i][1]+d[lst (i) [0]); Ans=Max (ans,t); } cout<<ans; }

Usaco1.1broken NECKLACE[DP as Death]

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.