UVA 4394 - String painter(字符串区间DP)

String painter

                          Time Limit:3000MS    Memory Limit:0KB    64bit
IO Format:
%lld & %llu

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

题意:给定两个长度相等的字符串,记为 A, B, 均只包含小写字母。每次可以把一个连续字串 “刷” 成相同的一个字母,问至少需要几次才能够把 A 变为 B。

思路:

假设一个空串要刷成目标串的极端情况(题意保证不存在空串,可以假设为所有字母均与目标串不同)。

用 dp[ i ][ j ]  表示把 A 串的 i-j 区间刷成目标串 B 的 i-j 所需要的最少步数,首先初始化所有的 dp[ i ][ i ] 为 1 。

dp[ i ][ j ] = dp[ i+1 ][ j ] + (strB[ i ] == strB[ j ]?0:1); 如果strB[ i ] == strB[ j ],则 i,j 这两个位置可以同时刷。

同理,对于k=(i+1...j )如果str[k]==str[i] , 则dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]),,因为刷i的时候可以与k同时刷。

上面是对初始串与目标串完全不同的情况。

如果有部分的不同:

ans[ i ] 表示将 strA[ 1...i ] 刷成 strB [ 1...i ]的最小步数,

if   strA[i]==strB[i],    f[ i ] = f[ i-1 ];

else   f[ i ] = min( f[ i ],    f [ j ]+dp[ j+1 ][ i ]) ,1 <= j <= i-1

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 110;
int dp[MAXN][MAXN];
int f[MAXN];
char strA[MAXN], strB[MAXN];

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    int n;
    while(scanf("%s %s", strA+1, strB+1) != EOF)
    {
        n = strlen(strA+1);
        for(int i = 0; i <= n; i++)
            dp[i][i] = 1;
        for(int len = 2; len <= n; len++)
        {   // 枚举区间长度
            for(int i = 1, j = len; j <= n; i++, j++)
            {
                dp[i][j] = dp[i+1][j]+(strB[i]==strB[j]?0:1);
                for(int k = i+1; k <= j-1; k++)
                {
                    if(strB[i] == strB[k])
                        dp[i][j] = min(dp[i][j], dp[i+1][k]+dp[k+1][j]);
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            f[i] = dp[1][i];
        }
        if(strA[1] == strB[1])
            f[1] = 0;
        for(int i = 1; i <= n; i++)
        {
            if(strA[i] == strB[i])
                f[i] = f[i-1];
            else
            {
                for(int j = 1; j <= i-1; j++)
                {
                    f[i] = min(f[i], f[j]+dp[j+1][i]);
                }
            }
        }
        cout<<f[n]<<endl;
    }
    return 0;
}</span>
时间: 2024-10-26 20:25:35

UVA 4394 - String painter(字符串区间DP)的相关文章

HDU 2476 String painter(区间DP啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 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 cha

HDU 2476 String painter(区间dp)

String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2171    Accepted Submission(s): 956 Problem Description There are two strings A and B with equal length. Both strings are made up

HDOJ 题目2474 String painter(区间DP)

String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2520    Accepted Submission(s): 1134 Problem Description There are two strings A and B with equal length. Both strings are made up

hdu2476 String painter(区间dp)

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

HDU 2476 String painter (区间DP)

题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成b的次数 ans[i]:a从(0,i)变成b(0,i)所需的最小次数 初始化ans[i]=dp[0][i] 如果a[i]==b[i],则ans[i]=ans[i-1]; 由小区间更新到大区间 1 //#pragma comment(linker, "/STACK:167772160")//

uvalive 4394 string painter (序列dp)

两个字符串,s是原串,t是目标串,一次可以把s的任意段所有字母变成同一个字母,问用最少次数将s变成t. 状态转移方程为cnt[i] = min(cnt[i], dp(j+1,i-1,t[i])+1+(j>0 ? cnt[j-1]:0)) 代码如下: #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include&

uva live 4394 String painter 区间dp

// uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己给出的数据跪了 // 交了也wa了,才发现,自己的方法是有问题的,假设是将两个串同一时候考虑 // 的话,比方: dp[i][j] 表示从i到j,s串刷成目标b串所须要的最小的花费 // 然后依据区间的端点的字符特点,进行状态转移,然而可能是我太搓了, // 发现这种状态转移是不正确的.比方edc和

uva live 4394 String painter 间隔dp

// uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他们跪在数据 // 还要wa.才发现,自己的方法是有问题的,假设是将两个串同一时候考虑 // 的话.比方: dp[i][j] 表示从i到j,s串刷成目标b串所须要的最小的花费 // 然后依据区间的端点的字符特点,进行状态转移.然而可能是我太搓了. // 发现这种状态转移是不正确的,比方edc和cde.

uva live 3516 Exploring Pyramids 区间DP

// uva live 3516 Exploring Pyramids 区间DP // // 题目大意: // // 给你一个多叉树,每个节点是一个大写字母,从根节点走,按照先序遍历的 // 原则访问,不能访问则回溯,每次记录一下节点的字符,最后得到一个字符串.现 // 在给你一个字符串,问可能符合条件的多叉树的数量. // // 解题思路: // // 区间DP,我们注意到,从根节点出发,一定会再次回到根节点,那么我们可以设 // d(i,j) 是序列i到j段形成的符合条件的多叉树的数量,则