题目:

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. springboot autoconfig

    springboot自动配置的核心思想是:springboot通过spring.factories能把main方法所在类路径以外的bean自动加载 springboot starter验证 我在spr ...

  2. DROP INDEX - 删除一个索引

    SYNOPSIS DROP INDEX name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP INDEX 从数据库中删除一个现存的索引. 要执 ...

  3. axure的基本使用方法(侧边导航栏的制作)

    1.创建一个动态面板control 2.在home中创建动态面板homepage和movepage并且完成布局 3.给home添加移动事件 4.给按钮添加点击事件 5.大功告成

  4. tcp 高性能服务, netty,mqtt

    1. io 线程不要有比较长的服务. 全部异步化. [1] netty 权威指南上只是说业务复杂时派发到业务线程池种. 共用的线程池最好都轻量. 多层线程池后, 下层的可以进行隔离. 这个是 mqtt ...

  5. zabbix3.2安装手册

    Alexei Vladishev创建了Zabbix项目,当前处于活跃开发状态,Zabbix SIA提供支持. Zabbix是一个企业级的.开源的.分布式的监控套件 Zabbix可以监控网络和服务的监控 ...

  6. Qt概念和快捷键

    Qt概念和快捷键 Qt简介        1.Qt的由来和发展 Qt是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程 ...

  7. python入门:用户登录,三次错误机会

    #!/usr/bin/env python # -*- coding:utf-8 -*- #用户登录,三次机会重试 #主要分为两个部分,一部分是写三次循环,一部分写用户输入 #用户登录的实现,循环3次 ...

  8. hive sql 学习笔记

    1.coalesce 语法: COALESCE ( expression [ ,...n ] ) 参数: expression 任何类型的表达式. 返回类型: 返回数据类型优先级最高的 express ...

  9. 2018 Python开发者大调查:Python和JavaScript最配?

    在2018年秋季,Python软件基金会与JetBrains发起了年度Python开发者调查. 报告的目的是寻找Python领域的新趋势,帮助开发者深入了解2018年Python开发者的现状. 该报告 ...

  10. GoF23种设计模式之行为型模式之策略模式

    传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 1概述           定义一系列算法,把它们一个个都封装起来,并且让它们可以相互 ...