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