Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

r[0] = p[0] = 0
If c = 'r' then r[p+1] = r[p] + 1 and b[p+1] = 0
because the length of the blue beads is 0.
if c = 'b' then b[p+1] = b[p] + 1 and r[p+1] = 0
if c = 'w' then both length of the red and length of blue beads
can be longer.
so r[p+1] = r[p]+1 and b[p+1] = b[p] + 1.

The number of beads that can be collected in breaking point p
is then max(left[r[p]], left[b[p]]) + max(right[r[p]], right[b[p]]).
And the maximum from this value is answer for the problem.


/*

ID:chsc4691
LANG:C++
PROB:beads
*/
#include 
<fstream>
using namespace std;

ifstream cin( 
"beads.in" );
ofstream cout( 
"beads.out" );

string necklace;
int    leftt[800][2];
int    rightt[800][2];
int    n, ans;

void
__read__()
{
    cin 
>> n
         
>> necklace;
}

void
__precalc__()
{
    necklace 
+= necklace;
    
forint i = 1; i <= n * 2; i++ )
    {
        
if( necklace[i - 1== 'r' )
        {
            leftt[i][
0= leftt[i - 1][0+ 1;
            leftt[i][
1= 0;
        }
        
if( necklace[i - 1== 'b' )
        {
            leftt[i][
1= leftt[i - 1][1+ 1;
            leftt[i][
0= 0;
        }
        
if( necklace[i - 1== 'w' )
        {
            leftt[i][
0= leftt[i - 1][0+ 1;
            leftt[i][
1= leftt[i - 1][1+ 1;
        }
    }
    
forint i = n * 2 - 1; i >= 0; i-- )
    {
        
if( necklace[i] == 'r' )
        {
            rightt[i][
0= rightt[i + 1][0+ 1;
            rightt[i][
1= 0;
        }
        
if( necklace[i] == 'b' )
        {
            rightt[i][
1= rightt[i + 1][1+ 1;
            rightt[i][
0= 0;
        }
        
if( necklace[i] == 'w' )
        {
            rightt[i][
1= rightt[i + 1][1+ 1;
            rightt[i][
0= rightt[i + 1][0+ 1;
        }
    }
}

int
max( 
int x, int y )
{
    
if( x > y )
        
return x;
    
else
        
return y;
}

void
__dp__()
{
    
forint i = 1; i <= n * 2; i++ )
        ans 
= max( ans, max( leftt[i][0], leftt[i][1] ) + max( rightt[i][0], rightt[i][1] ) );
    
if( ans > n )
        ans 
= n;
}

void
__outp__()
{
    cout 
<< ans << endl;
}

int
main()
{
    __read__();
    __precalc__();
    __dp__();
    __outp__();

    
return 0;
}

posted on 2009-09-15 20:53 mad4alcohol 阅读(287) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理