[区间dp]Hdu2467 String painter

来源:互联网 发布:中医客户档案软件 编辑:程序博客网 时间:2024/05/21 15:43

String painter

Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u
Submit

Status

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

题目大意:给两个串,问串1变成串2最少需要多少次,改变可以连续一段变成一样的字母

有一种方法是:
直接用空白串来刷出s2的最小次数
dp[i][j],则为刷出2串【i,j】的最小次数,,
dp[i][j]=dp[i+1][j]+1;
一个一个变
if(s2[i]==s2[k])
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j])
当s2[i]==s2[k]时,就只需要刷i+1,k..刷k时顺便把i也一起了结了;
ans[i]初值是dp[1][i]
if(s1[i]==s2[i])ans[i]=ans[i-1]
ans[i]=min(ans[i],ans[k]+dp[k+1][j])

这里有两点自己写的时候疑问的地方:
1.循环事外层为什么是逆序循环;
2.为什么dp[i][j]的初始答案是从dp[i+1][j]推来的而不是dp[i][j+1](后一种会错)

具体解答在代码下面。

代码

#include<iostream>#include<cstdio>#include<ctime>#include<cstdlib>#include<cmath>#include<cstring>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endif#define INF 0x3f3f3f3f#define clock CLOCKS_PER_SEC#define cle(x) memset(x,0,sizeof(x))#define maxcle(x) memset(x,127,sizeof(x))#define mincle(x) memset(x,-1,sizeof(x))#define minn(x1,x2,x3) min(x1,min(x2,x3))#define cop(a,x) memcpy(x,a,sizeof(a))#define FROP "hdu"#define C(a,b) next_permutation(a,b)#define LL long long#define smin(x,tmp) x=min(x,tmp)using namespace std;const int N=105;char s1[N],s2[N];int dp[N][N];//把空白串变成1串的次数int ans[N];//2串前i位变成1串的次数int main(){    freopen(FROP".in","r",stdin);    freopen(FROP".out","w",stdout);    while(scanf("%s%s",s1+1,s2+1)!=EOF)    {        cle(dp);        int len=strlen(s1+1);        for(int i = len ; i >= 1; i--)            for(int j = i ; j <= len ; j++)            {                dp[i][j]=dp[i+1][j]+1;                for(int k = i+1; k <= j; k++)                    if(s2[i]==s2[k])                        dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);            }        ans[0]=0;        for(int i = 1; i<= len; i++)            ans[i]=dp[1][i];        for(int i = 1; i <= len; i++)        {            if(s1[i]==s2[i])            ans[i]=ans[i-1];            for(int j = 1; j < i; j++)                ans[i]=min(ans[i],ans[j]+dp[j+1][i]);        }        printf("%d\n",ans[len]);    }    return 0;}

1.dp时外层循环起点,逆序循环,因为dp[i][j]是从i+1,k+1层推来
次层终点顺序,,同理
内层枚举中间节点
2.第二点我也不知道啊,,,为什么,,为什么,往大神给我解答。。。QAQ,,感觉含义也差不多,,不过要WA

0 0
原创粉丝点击