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 beadThe 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 beads Line 2: a string of N characters, each of which is r, b, or w SAMPLE INPUT (file beads.in)
29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrbOUTPUT FORMAT
A single line containing the maximum of number of beads that can be collected from the supplied necklace.SAMPLE OUTPUT (file beads.out)
11OUTPUT EXPLANATION
Consider two copies of the beads (kind of like being able to runaround the ends). The string of 11 is marked.wwwbbrwrbrbrrbrbrwrwwrbwrwrrb wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ****** *****
就是说从一串项链,找到一个断点,往两边去,到下一个颜色不一样的珠子。
比如 “rrbb(断开)rrbb” 往左:bb 往右:rr
由于项链是圈,所以把字符串 “+=” 一遍即可解决循环
/*
ID:artwalk1
PROG:beads
LANG:C++
*/
/*
* 1.1.Broken_Necklace.cpp
* 2011/08/05
*/
#include <fstream>
#include <string>
using namespace std;
int find_max_beads( string str, int n );
inline int max( int a, int b );
int main(int ac, char** av)
{
ifstream fin("beads.in");
// get n & str "from beads.in"
int n;
string str;
fin >> n >> str;
ofstream fout("beads.out");
fout << find_max_beads( str, n ) << endl;
fin.close();
fout.close();
return 0;
}
int find_max_beads( string str, int n ) {
string necklace = str;
necklace += str;
int len = n << 1;
char bead_color = 0;
int W, bead, oldnum, newnum, maxnum;
W = bead = oldnum = newnum = maxnum = 0;
for ( int i = 0; i != len; ++i ) {
if ( 'w' == necklace[i] ) {
++W;
++newnum;
} else if ( bead_color == necklace[i] ){
++newnum;
W = 0;
} else {
maxnum = max( maxnum, oldnum + newnum );
oldnum = newnum - W;
newnum = W + 1;
W = 0;
bead_color = necklace[i];
}
}
maxnum = max( maxnum, oldnum + newnum );
return ( maxnum < n ? maxnum : n );
}
inline int max( int a, int b ) {
return ( a > b ? a : b );
}
今天这是玩我吗,先是下午usaco死活上不去,晚上了提交了,发文章,csdn又挂了,擦!
一开始写的那个,算是无药可救了。虽然很努力的想保持清晰,但情况复杂超出意料。不断对新情况debug后,发现思路已经乱了,更别说代码了。
实在受不了了google了下,不得不说,上面解法太神奇了。
正好晚上看到MIT的《Introduction to Computer Science and Programming》的第11讲《Testing and Debugging》,正好说到要点
上来不要就直接在IDE中找错
最给力的工具是:
1、print
2、reading
正确的方法:
1、想想为什么出现这个结果(而不是为什么和答案不同)、思路清晰后,有利于发现类似的bug
2、用二分法print
目的:
不是找出一个bug,而是没有bug(感觉有点绝对,《代码大全》说不可能没bug,尽量~尽量~)
Here are the test data inputs:
------- test 1 ---- 29 wwwbbrwrbrbrrbrbrwrwwrbwrwrrb ------- test 2 ---- 3 rrr ------- test 3 ---- 77 rwrwrwrwrwrwrwrwrwrwrwrwbwrwbwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrwr ------- test 4 ---- 17 wwwwwwwwwwwwwwwww ------- test 5 ---- 50 bbrrrbrrrrrrrrbrbbbrbrrbrrrrbbbrbrbbbbbrbrrrbbrbbb ------- test 6 ---- 8 rrwwwwbb ------- test 7 ---- 200 rrrrrrrrrrrrrrrrrrrrbbbbbbbbbbbbbbbbbbbbrrrrrrrrrrrrrrrrrrrrbbbbbbbbbbbbbbbbbbbbrrrrrrrrrrrrrrrrrrrrbbbbbbbbbbbbbbbbbbbbrrrrrrrrrrrrrrrrrrrrbbbbbbbbbbbbbbbbbbbbrrrrrrrrrrrrrrrrrrrrbbbbbbbbbbbbbbbbbbbb ------- test 8 ---- 350 rrbbrbbbwbwwbwbbbbwwrrbbwbrwbrwbbbrbrwrwbrwwwrrbbrrwrbbrwbwrwwwrbrwwwwwrwbwwwrrbrrbbbrbrbbbrbbbrbbwbbbbbrbrrbrwwbrrrrwbwrwrbbwbwrbrbrwwbrrbwbrwwbwwwbrbwrwbwbrbbbwrbwwrrrbwbwbbbbbrrwwwrbrwwrbbwrbbrbbrbwrrwwbrrrbrwbrwwrbwbwrrrbwrwrrbrbbwrwrbrwwwrwbwrrwwwwrrrwrrwbbwrwwrwrbwwbwrrrrbbwrbbrbwwwwwbrbbrbbrbrwbbwbwwbbbbbwwwrbwwbbbwrwwbbrrwrwbwrrwwwrrrwrrwww ------- test 9 ---- 333 rwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwbrwb
失败的这个也贴出来做个纪念:
/*
ID:artwalk1
PROG:beads
LANG:C++
*/
/*
* 1.1.Broken_Necklace.cpp
* 2011/08/05
*/
#include <fstream>
#include <string>
//#include <algorithm>
//#include <iostream>
using namespace std;
int find_max_beads( string str );
inline bool compare_beads_color( char necklace_color, char & color );
inline int max( int max1, int max2 );
int reset_i( string necklace, int i );
struct bead {
char color;
int max;
};
int main(int ac, char** av)
{
ifstream fin("beads.in");
// get n & str "from beads.in"
int n;
string str;
fin >> n >> str;
// find_max_beads
// string nacklacer = str;
// string nacklaceb = str;
// replace(nacklacer.begin(), nacklacer.end(), 'w', 'r');
// replace(nacklaceb.begin(), nacklaceb.end(), 'w', 'b');
ofstream fout("beads.out");
// fout << max( find_max_beads(nacklacer),
// find_max_beads(nacklaceb) )<< endl;
fout << find_max_beads( str ) << endl;
// close files
fin.close();
fout.close();
return 0;
}
int find_max_beads( string str ) {
/* the maximum of number of beads
* that can be collected from the supplied necklace.
*/
// cout << str << endl;
string necklace = str;
necklace += str;
bead one_bead; // for compare
one_bead.color = str[0];
one_bead.max = 0;
int maxold = 1;
int maxnew = 1; // save last continuous necklace number
// int posmax = 0;
int posnew = 0;
int posold = 0;
// cout << necklace << endl;
int len = necklace.size();
for ( int i = 1; i != len; ++i ) {
// if ( compare_beads_color( necklace[i], one_bead.color ) ) {
if ( necklace[i] == one_bead.color
|| one_bead.color == 'w' || necklace[i] == 'w' ) {
if ( necklace[i] != 'w' ) {
one_bead.color = necklace[i];
// wpos = i;
}
++maxnew;
} else {
one_bead.color = necklace[i];
posold = posnew;
posnew = i;
one_bead.max = max( one_bead.max, maxold + maxnew );
/* if ( one_bead.max < (maxold + maxnew) ) {
posmax = pos;
}
*/
maxold = maxnew;
maxnew = 1;
//--i; // this is to cancel out ++i
if ( posold != posnew ) {
i = posold;
i = reset_i( necklace, i );
}
// i = wpos;
// pos = i; // color is change, save pos
}
}
return one_bead.max == 0 ? str.size() : one_bead.max;
// return posmax;
}
int reset_i( string necklace, int i ) {
while ( necklace[i-1] == 'w' ) {
--i;
}
return i;
}
inline bool compare_beads_color( char necklace_color, char & color ) {
// return necklace_color == color ? true : false;
if ( necklace_color == color || color == 'w' ) {
if ( necklace_color != 'w' ) {
color = necklace_color;
}
return true;
} else {
return false;
}
}
inline int max( int max1, int max2 ) {
return max1 > max2 ? max1 : max2;
}