题目:

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. QT 调试输出格式

    Qt调试输出格式: 1,qDebug() << qPrintable(firstNode.nodeName()) << qPrintable(firstNode.nodeVal ...

  2. python_103_属性方法例子

    class Flight(object): def __init__(self,name): self.flight_name = name def checking_status(self): pr ...

  3. tmpfs与内存盘

    如何快速的吃掉一段内存:通过tmpfs来划一片领地,再用dd去粗暴占用mount -t tmpfs -o size=512M tmpfs /mnt/demodd if=/dev/zero of=/mn ...

  4. SVN:The working copy is locked due to a previous error (二)

    之前也碰到过这种问题,但是根本问题不同,解决方案不同. 传送门:SVN:The working copy is locked due to a previous error (二) 本次错误如图: 解 ...

  5. Codeforces Round #513 (rated, Div. 1 + Div. 2)

    前记 眼看他起高楼:眼看他宴宾客:眼看他楼坍了. 比赛历程 开考前一分钟还在慌里慌张地订正上午考试题目. “诶这个数位dp哪里见了鬼了???”瞥了眼时间,无奈而迅速地关去所有其他窗口,临时打了一个缺省 ...

  6. mariadb多源主从复制错误跳过.md

    mysql 的主从错误跳过和mariadb的多源主从复制错误跳过操作不同,请注意: 更改会话的default_master_connection变量 STOP SLAVE 'slave_account ...

  7. Qt的由来和发展

    一.Qt的由来 Haavard Nord 和Eirik Chambe-Eng于1991年开始开发"Qt",1994年3月4日创立公司,早名为Quasar Technologies, ...

  8. php读取不到https的域名

    因测试环境php遇到无法正常读取到https的域名,但是域名配置了ssl证书,故做如下排查. php测试代码如下 $config['base_url'] = ''; #开启调试模式 #echo &qu ...

  9. guava笔记

    ​guava是在原先google-collection 的基础上发展过来的,是一个比较优秀的外部开源包,最近项目中使用的比较多,列举一些点.刚刚接触就被guava吸引了... ​    ​这个是gua ...

  10. leetcode-22-string

    521. Longest Uncommon Subsequence I find the longest uncommon subsequence of this group of two strin ...