题目:

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. groff - groff 文档排版系统前端

    总览 (SYNOPSIS) groff [ -abehilpstvzCENRSUVXZ ] [ -wname ] [ -Wname ] [ -mname ] [ -Fdir ] [ -Idir ] [ ...

  2. java基础—接口概念

    一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如“金丝猴是一种动物”,金丝猴从动物这个类继承,同时“金丝猴是一种值钱的东西”,金丝猴从“值钱的东西”这个类继承,同时“金丝猴 ...

  3. Luogu [P3951] 小凯的疑惑

    题目详见:[P3951]小凯的疑惑 首先说明:此题为一道提高组的题.但其实代码并没有提高组的水平.主要考的是我们的推断能力,以及看到题后的分析能力. 分析如下: 证明当k>ab-a-b时,小凯可 ...

  4. 有关SQL的一道面试题

    这是一个学生分数表 StudentName            StudySubject           SubjectScore Peter                           ...

  5. iOS 多线程编程

    参考文章: iOS多线程编程之NSThread的使用http://blog.csdn.net/totogo2010/article/details/8010231 iOS多线程编程之NSOperati ...

  6. nodejs 静态资源服务与接口代理跨域

    首先需要 npm install express 和 npm install request 代码如下: const express = require('express'); const path ...

  7. CentOS7下systemd

    配置文件: /usr/lib/systemd/system:每个服务最主要的启动脚本设置,类似于之前的/etc/init.d/ /run/systemd/system:系统执行过程中所产生的服务脚本, ...

  8. shell 练习 - 第七周

    1. 用shell实现传入进程pid, 查看对应进程/proc下CPU.内存指标 #!/bin/bash read -p "Input PID Value: " pid pid_e ...

  9. v2ex站长专访 - 100offer专访Livid:不仅仅是V站站长

    转载自: https://www.douban.com/group/topic/121611313/ 前几天上网时偶然发现v2ex站长的blog(https://livid.v2ex.com/),了解 ...

  10. vue之列表循环

    文档:https://cn.vuejs.org/v2/guide/list.html 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变, ...