P1203 [USACO1.1] Broken Necklace Broken neck.pdf, p1203usaco1.1
Description
You have a necklace consisting of N red, white, or blue beads (3 <= N <= 350). The beads are randomly arranged. Here are two examples of n = 29:
The first and second beads have been marked in the image.
The necklace in Image A can be represented by the following string:
Brbrrrbbbrrrrrbrrbbrbbbbrrrrb
If you want to break the necklace at some points, expand it into a straight line, and then collect beads of the same color from one end until you encounter a different color beads, do the same thing on the other end (the color may be different from the one collected earlier ). Determine where to break the necklace to collect the maximum number of beads.
For example, in the necklace in Figure A, you can collect 8 beads by interrupting the necklace Between beads 9 and beads 10 or between beads 24 and beads 25.
What do white beads mean?
Some necklaces also contain white beads (slice B.
When beads are collected, a white bead may be treated as red or blue.
A string containing white beads will contain 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:
Row 3: N, number of beads
Row 2nd: a string of N characters. Each character is r, B, or w.
Output Format:
Output a line of an integer that indicates the maximum number of beads that can be collected from the given necklace.
Input and Output sample
Input example #1:
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb
Output sample #1:
11
Description
The question translation is from NOCOW.
USACO Training Section 1.1
Since both copies are used as copies
Then I will launch a linked list.
Record the location of each vertex, the location of the previous vertex, and the location of the next vertex (where the positions of 1 and n must be specially determined)
In this way, a ring is formed in the true sense.
Then search for different ones.
Note that the last ans must be <= n,
The third point is more difficult and requires special judgment.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 char c; 7 int n; 8 struct node 9 {10 int data; 11 int nxt; 12 int last; 13 int num; 14} a [1, 1001]; 15 int dfs1 (int p) 16 {17 int tot = 1; 18 for (int I = a [p]. last; I! = P; I = a [I]. last) 19 {20 if (a [I]. data = a [p]. data | a [I]. data = 3) 21 tot ++; 22 else break; 23} 24 return tot; 25} 26 int dfs2 (int p) 27 {28 int tot = 1; 29 for (int I = a [p]. nxt; I! = P; I = a [I]. nxt) 30 {31 if (a [I]. data = a [p]. data | a [I]. data = 3 | (I = a [p]. nxt & a [I]. data! = A [p]. data & a [p]. data = 3) 32 // comparison of the third test point: 33 tot ++; 34 else break; 35} 36 return tot; 37} 38 int main () 39 {40 scanf ("% d", & n); 41 for (int I = 1; I <= n; I ++) 42 {43 cin> c; 44 a [I]. num = I; 45 if (I = 1) 46 a [I]. last = n; 47 else48 a [I]. last = I-1; 49 if (I = n) 50 a [I]. nxt = 1; 51 else52 a [I]. nxt = I + 1; 53 if (c = 'R') 54 a [I]. data = 1; // red 55 else if (c = 'B') 56 a [I]. data = 2; // blue 57 else if (c = 'W') 58 a [I]. data = 3; // white 59} 60 int ans =-1; 61 int flag = 0; 62 for (int I = 1; I <= n; I ++) 63 {64 if (a [I]. data! = A [a [I]. nxt]. data) 65 {66 flag = 1; 67 int now = dfs1 (I); 68 now = now + dfs2 (a [I]. nxt); 69 ans = max (ans, now); 70} 71} 72 if (flag = 0 | ans> n) ans = n; 73 printf ("% d", ans); 74 return 0; 75}