http://acm.pku.edu.cn/JudgeOnline/problem?id=3401
String reduction
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 799 | Accepted: 219 |
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
题目意思是说,有一个只有'a','b'组成的字符串,可以有一种操作reduction ,可以将形如 a*a,b*b的字符变为a或者b,要求
后来突然发现这个题目有一个非常桥面简单的方法,由于a*a, b*b之类的字符可以匹配任意字符,那么我们只要从前
向后扫描,如果有相隔的两个字符相同,由于这三个字符能够匹配a,b任意字符,那么我们就可以让这种匹配一直进行
下去,从而将字符串不断的reduction ,只要注意字符串长度的奇偶性就可以了,如此我们可以得到一个很简单的方法
代码如下: