poj3401

 

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

Northeastern Europe 2001, Western Subregion

题目意思是说,有一个只有'a','b'组成的字符串,可以有一种操作reduction  ,可以将形如 a*a,b*b的字符变为a或者b,要求
给定一个字符串,求能变化后的最小的长度。
看了discuss后才AC,具体是这样做的:
我们先用F[i][j]表示能否将i,j之间的字符串变化为一个单个字符,这个是标准的动态规划,状态转移方程为:
F[i][j] =  true  if( str[i] == str[j] && F[i+1][j-1] = = true
F[i][j] =  true if(i+2 != j && (F[i+2][j] == true || F[i][j-2] ==true)
else F[i][j] = false;
然后我们用Len[i][j] 表示i,j之间的字串能够变为的最小长度,可以看出这个也是一个标准的动态规划
状态转移方程为:
Len[i][j] = 1 if(F[i][j] == true)
else Len[i][j] = min(Len[i+1][j-1] + 2,Len[i+1][j]+1,Len[i][j-1]+1)
由此我们就可以得到求这个题目的具体做法,代码如下:
 

后来突然发现这个题目有一个非常桥面简单的方法,由于a*a, b*b之类的字符可以匹配任意字符,那么我们只要从前

向后扫描,如果有相隔的两个字符相同,由于这三个字符能够匹配a,b任意字符,那么我们就可以让这种匹配一直进行

下去,从而将字符串不断的reduction ,只要注意字符串长度的奇偶性就可以了,如此我们可以得到一个很简单的方法

代码如下:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值