题目:

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

题解:

我们先预处理出一个f[i][j],表示一个空白的串i-j部分如果要涂成第二个串一样且i率先涂上相同颜色(可以连着涂··只要保证i先匹配上)需要的最少次数···然后再更新ans[i]即可··具体看代码

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
int n,f[N][N],ans[N];
char s[N],t[N];
int main()
{
// freopen("a.in","r",stdin);
while(~scanf("%s%s",s+,t+))
{
n=strlen(s+);
memset(f,inf,sizeof(f));
memset(ans,inf,sizeof(ans));
ans[]=;
for(int i=;i<=n;i++) f[i][i]=;
for(int i=;i<n;i++)
if(t[i]==t[i+]) f[i][i+]=;
else f[i][i+]=;
for(int i=n-;i>=;i--)
for(int j=i+;j<=n;j++)
{
f[i][j]=f[i+][j]+;
for(int k=i+;k<=j;k++)
if(t[i]==t[k]) f[i][j]=min(f[i][j],f[i+][k-]+f[k][j]);//在涂上位置时顺便涂上i位置
} for(int i=;i<=n;i++)
{
if(s[i]==t[i]) ans[i]=ans[i-]; //如果这个位置已经相等就不涂
else
for(int j=;j<i;j++)
ans[i]=min(ans[i],ans[j]+f[j+][i]); //否则找到最优解
}
cout<<ans[n]<<endl;
}
return ;
}

刷题总结——String painter(hdu2476)的更多相关文章

  1. 【区间DP+好题】String painter

    https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/G [题意] 给定两个长度相同的字符串A,B.每次操作都能把A中的任意一个子段变成 ...

  2. HDU2476 String painter —— 区间DP

    题目链接:https://vjudge.net/problem/HDU-2476 String painter Time Limit: 5000/2000 MS (Java/Others)    Me ...

  3. HDU2476 String painter(DP)

    题目 String painter 给出两个字符串s1,s2.对于每次操作可以将 s1 串中的任意一个子段变成另一个字符.问最少需要多少步操作能将s1串变为s2串. 解析 太妙了这个题,mark一下. ...

  4. HDU2476 String painter

    题意 String painter Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  5. hdu2476 String painter(区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2476 Problem Description There are two strings ...

  6. uva live 4394 String painter 间隔dp

    // uva live 4394 String painter // // 问题是,在培训指导dp运动主题,乍一看,我以为只是一点点复杂 // A A磕磕磕,两个半小时后,.发现超过例子.然而,鉴于他 ...

  7. uva live 4394 String painter 区间dp

    // uva live 4394 String painter // // 这一题是训练指南上dp专题的习题,初看之下认为仅仅是稍微复杂了一点 // 就敲阿敲阿敲,两个半小时后,发现例子过了.然而自己 ...

  8. HDU 自动刷题机 Auto AC (轻轻松松进入HDU首页)

    前言: 在写这篇文章之前,首先感谢给我思路以及帮助过我的学长们 以下4篇博客都是学长原创,其中有很多有用的,值得学习的东西,希望能够帮到大家! 1.手把手教你用C++ 写ACM自动刷题神器(冲入HDU ...

  9. 手把手教你用C++ 写ACM自动刷题神器(冲入HDU首页)

    转载注明原地址:http://blog.csdn.net/nk_test/article/details/49497017 少年,作为苦练ACM,通宵刷题的你 是不是想着有一天能够荣登各大OJ榜首,俯 ...

随机推荐

  1. codeforce Gym 100570B ShortestPath Query (最短路SPFA)

    题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...

  2. 禁止按键F5和禁止鼠标右键菜单 js代码

    <script language="javascript"> //禁止按键F5 document.onkeydown = function(e){ e = window ...

  3. 用promise封装ajax

    首先贴代码 var ajaxOptions = { url: 'url', method: 'GET', async: true, data: null, dataType: 'text', } fu ...

  4. JSON.stringify(value[, replacer[, space]])

    1. JSON.stringify(value[, replacer[, space]]);value 必需 对象或数组,需要转换成json字符串的数据replacer 可选 函数或数组space 可 ...

  5. ubuntu : 无法安全地用该源进行更新,所以默认禁用该源。

    sudo apt update报错: 无法安全地用该源进行更新,所以默认禁用该源. 1.检查是否是网络出了问题,修改DNS:114.114.114.114,8.8.8.8 断开网卡再重新连接,成功! ...

  6. C++:100阶乘数组输出

    #include <iostream> using namespace std; int main(){ int i =1; int a[2048]={0}; while(i !=101) ...

  7. linux系统装载ELF过程

    参考:程序员的自我修养 fork -->execve() //----kenerl space--------------- sys_execve() /*arch\i386\kernel\pr ...

  8. LeetCode(138) Copy List with Random Pointer

    题目 A linked list is given such that each node contains an additional random pointer which could poin ...

  9. Linux学习-服务器硬件数据的收集

    以系统内建 dmidecode 解析硬件配备 系统有个名为 dmidecode 的软件,它可以解析 CPU 型号.主板型号与内存相 关的型号等等~ [root@study ~]# dmidecode ...

  10. python小数据池,代码块深入剖析

    小数据池 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建更多的对象 缓存:int,str,bool int:缓存范围-5~256 str:    1.长度小于等于1,直接缓存 2.长度大于 ...