您的位置:首页 > 大数据 > 人工智能

hdu 2476 String painter 区间dp 难

2016-04-02 19:07 423 查看
一种比较笨的方法,人家都开的是二维数组,我却开了三维。


String painter

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2862    Accepted Submission(s): 1307


Problem Description

There are two strings A and B with equal length. Both strings are made up of lower case letters. Now you have a powerful string painter. With the help of the painter, you can change a segment of characters of a string to any other character you want. That is,
after using the painter, the segment is made up of only one kind of character. Now your task is to change A to B using string painter. What’s the minimum number of operations?

 

Input

Input contains multiple cases. Each case consists of two lines:

The first line contains string A.

The second line contains string B.

The length of both strings will not be greater than 100.

 

Output

A single line contains one integer representing the answer.

 

Sample Input

zzzzzfzzzzz
abcdefedcba
abababababab
cdcdcdcdcdcd

 

Sample Output

6
7

 

Source

2008 Asia Regional Chengdu

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  2480 2481 2478 2482 2475 

 

Statistic | Submit | Discuss | Note

/**==========================================
*   This is a solution for ACM/ICPC problem
*
*   @source:2008 Asia Regional Chengdu  String painter
*   @type: 区间dp
*   @author: wust_ysk
*   @blog:  http://blog.csdn.net/yskyskyer123 *   @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn= 100   ;

char a[maxn+10];
char b[maxn+10];
int dp[27][maxn+10][maxn+10];
int n,pre[maxn+5],nex[maxn+5];

void cope()
{
pre[1]=0;
for(int i=2;i<=n;i++)
{
pre[i]= b[i]==b[i-1]? pre[i-1]:i-1  ;
}
nex
=n+1;
for(int i=n-1;i>=1;i--)
{
nex[i]= b[i]==b[i+1]?nex[i+1]:i+1;
}
}
void work()
{
for(int i=1;i<=n;i++)
{
dp[0][i][i]=a[i]==b[i]? 0: 1 ;
for(int j=1;j<=26;j++)
{
dp[j][i][i]= 'a'+j-1==b[i]?0:1;
}
}
for(int add=1;add<n;add++)
{
for(int le=1;le+add<=n;le++)
{
int ri=le+add;

for(int j=0;j<=26;j++)
{
dp[j][le][ri]=ri-le+1;
for(int k=le;k<ri;k++)
{
dp[j][le][ri]=min(dp[j][le][ri],dp[j][le][k]+dp[j][k+1][ri]);
}

if(b[le]!=b[ri])  continue;
int st=nex[le],ed=pre[ri];
int x=b[le]-'a'+1;

if(st>ed)  dp[j][le][ri]=min(dp[j][le][ri],1);
else      dp[j][le][ri]=min(dp[j][le][ri], 1+dp[x][st][ed]);

}
}
}

printf("%d\n",dp[0][1]
);
}
int main()
{
while(~scanf("%s",a+1))
{
scanf("%s",b+1);
n=strlen(a+1);
cope();
work();
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: