PKU JudgeOnline 3401

String reduction
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 551 Accepted: 138

Description

There is a string of characters 'a' and 'b' with the length of no more than 255 characters. You can perform the substring reduction on the initial string in the following way: a substring "a*a" or "b*b" (where * (asterisk) denotes any character) can be reduces to a substring "*".

The task is to achieve a string of minimal possible length after several substring reductions.

Input

The input contains the initial string.

Output

The output contains a single line with the minimal possible length.

Sample Input

aab

Sample Output

3

Source

Northeastern Europe 2001, Western Subregion

 

原来我自己的做法一直是错的,知识掌握不够全面,还要运用到局部最优算法,没学过。。传说 中的贪心算法。

 

下面是在百度HI找到的一个算法:

 

 

问问AC的人才知道a*a or b*b变成的*不是以前的*了,可以是任意的*
    faint,这不是在赌对题的认识么...

    如果这样那么可以得到贪心做法,只要存在str[i] == str[i+2],结果就只与str长度有关
    可以用数学归纳法或者调整法证明这个性质

 

只是一直不明白,为什么我输入abcdddd答案是1。

好汗,但最终,人家AC了我没有。

下面是通过的代码(写得不是很规范,用C的语法来实现C++)

参考网址:http://hi.baidu.com/brabt_king/blog/item/6e7857c3802fab33e5dd3b19.html

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值