【USACO】PROB Broken Necklace

来源:互联网 发布:网络女主播视频直播 编辑:程序博客网 时间:2024/06/05 02:45
Broken Necklace

You have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red, others blue, and others white, arranged at random. Here are two examples for n=29:

                1 2                               1 2            r b b r                           b r r b          r         b                       b         b         r           r                     b           r        r             r                   w             r       b               r                 w               w      b                 b               r                 r      b                 b               b                 b      b                 b               r                 b       r               r                 b               r        b             r                   r             r         b           r                     r           r           r       r                         r       b             r b r                             r r w            Figure A                         Figure B                        r red bead                        b blue bead                        w white bead

The beads considered first and second in the text that follows have been marked in the picture.

The configuration in Figure A may be represented as a string of b's and r's, where b represents a blue bead and r represents a red one, as follows: brbrrrbbbrrrrrbrrbbrbbbbrrrrb .

Suppose you are to break the necklace at some point, lay it out straight, and then collect beads of the same color from one end until you reach a bead of a different color, and do the same for the other end (which might not be of the same color as the beads collected before this).

Determine the point where the necklace should be broken so that the most number of beads can be collected.

Example

For example, for the necklace in Figure A, 8 beads can be collected, with the breaking point either between bead 9 and bead 10 or else between bead 24 and bead 25.

In some necklaces, white beads had been included as shown in Figure B above. When collecting beads, a white bead that is encountered may be treated as either red or blue and then painted with the desired color. The string that represents this configuration will include the three symbols r, b and w.

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

PROGRAM NAME: beads

INPUT FORMAT

Line 1:N, the number of beadsLine 2:a string of N characters, each of which is r, b, or w

SAMPLE INPUT (file beads.in)

29wwwbbrwrbrbrrbrbrwrwwrbwrwrrb

OUTPUT FORMAT

A single line containing the maximum of number of beads that can be collected from the supplied necklace.

SAMPLE OUTPUT (file beads.out)

11

OUTPUT EXPLANATION

Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.
wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb                       ****** *****                       rrrrrb bbbbb  <-- assignments                       5 x r  6 x b  <-- 11 total
思路:
首先是如何表示项链。第一个想法是使用循环链表,但是考虑到需要构造一个相应的数据结构,就作罢了。
最简单的还是数组,在这里需要考虑如何用数组来表示这种“循环结构”。
在此,将原单串项链复制了2次组成一个新的数组,仅对中间的size长度进行操作,即可得到相同的效果。
要计算重复元素的个数,首先就是要找到重复元素的计数点。在此构造一个相应方法,明明为findnext(char[],int)。第一个参数为要查找的字符数组,第二个参数为入口位置。首先是找到离入口位置最近的(包括入口位置)的非'w'元素的位置,然后再向后查找到与该非'w'元素不同的非'w'元素的位置的上一个位置,作为查找重复元素的入口,记为position。
在利用traverse方法来计算position之前以及position+1之后的相同元素的个数总和。
最主要的是要注意各个方法中的边界。
代码:
import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class beads{public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new FileReader("beads.in"));FileWriter out = new FileWriter("beads.out");int size = Integer.parseInt(br.readLine());                //拼接一个项链字符串String beads = br.readLine();StringBuffer necklace = new StringBuffer(beads);necklace.append(beads);necklace.append(beads);char[] chain = necklace.toString().toCharArray();int start = size;//first element records the next split position;the second element records the largest amount of this round;int[] result = new int[2];                //查找第一个分裂点start = findnext(chain,start);int max = 0;if(start<size*2){while(start>=size&&start<=size*2){result = traverse(chain,start);start = result[0];                                //if result[1]>size that means the longest chars is the necklace itselfif(max<result[1]&&result[1]<=size){max = result[1];}}}else{max = size;}out.write(max+"\n");out.flush();out.close();br.close();}//@Desc:Calculate the number of duplicate elements before position and after position+1        //@Return:int[0] next split position;int[1] the length of duplicate elementspublic static int[] traverse(char[] c,int position){int[] result = new int[2];result[1]=2;int start = position;while(true){if(c[position+1]==c[start-1]){break;}else{start--;result[1]++;}}start = position+1;while(true){if((char)('r'+'b'-c[position+1])==c[start+1]){break;}else{start++;result[1]++;}}//result[1]--;//find next split pointresult[0] = findnext(c,start);return result;}//split point conditions://@Desc:find next split position        //@Return:c.length or the positionpublic static int findnext(char[] c,int start){while(start<c.length&&c[start]=='w'){start++;}if(start==c.length){return c.length;}char tmp = c[start];while(true){if(start>=c.length-1||c[start+1]==(char)('b'+'r'-tmp)){break;}else{start++;}}return  start;}}
附上usaco上提供的参考答案(思路一致,改用java重写了一遍):
import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class beads2 {public static void main(String[] args) throws IOException{BufferedReader br = new BufferedReader(new FileReader("beads.in"));FileWriter out = new FileWriter("beads.out");int size = Integer.parseInt(br.readLine());String beads = br.readLine();StringBuffer necklace = new StringBuffer(beads);necklace.append(beads);char[] cbeads = necklace.toString().toCharArray();int max =0;for(int i =0;i<size;i++){int state;if(cbeads[i]=='w')state =0;else{state = 1;}int count = 0;int j = i;for(;j<size+i&&state<=2;){if(cbeads[j]=='w'||cbeads[j]==cbeads[i]){j++;count++;}else{state++;count++;}}if(max<count)max = count;}out.write(max+"\n");out.flush();out.close();br.close();}}

其中还提到了一个O(N)的算法,采用了动态规划。

	
				
		
原创粉丝点击