String painter

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2792 Accepted Submission(s): 1272

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

这道题目完全没有思路,看了别人的题解。方法是先求空的字符数组变成目标字符数组,再求给定的字符数组变成目标字符数组。

虽然代码很短,确很值得我去思量,第二道区间DP题目

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h> using namespace std;
int dp[105][105];
int ans[105];
char s1[105];
char s2[105];
int main()
{
while(scanf("%s%s",s1+1,s2+1)!=EOF)
{
memset(dp,0,sizeof(dp));
int len=strlen(s1+1);
for(int j=1;j<=len;j++)
{
for(int i=j;i>=1;i--)
{
dp[i][j]=dp[i+1][j]+1;
for(int k=i+1;k<=j;k++)
{
if(s2[k]==s2[i])
dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
}
}
}
for(int i=1;i<=len;i++)
ans[i]=dp[1][i];
for(int i=1;i<=len;i++)
{
if(s1[i]==s2[i])
ans[i]=ans[i-1];
else
{
for(int j=i-1;j>=1;j--)
ans[i]=min(ans[i],ans[j]+dp[j+1][i]);
}
}
printf("%d\n",ans[len]);
}
return 0;
}

HDU 2476 String painter(区间DP)的更多相关文章

  1. HDU 2476 String painter(区间dp)

    题意: 给定两个字符串,让求最少的变化次数从第一个串变到第二个串 思路: 区间dp, 直接考虑两个串的话太困难,就只考虑第二个串,求从空白串变到第二个串的最小次数,dp[i][j] 表示i->j ...

  2. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  3. hdu 2476"String painter"(区间DP)

    传送门 https://www.cnblogs.com/violet-acmer/p/9852294.html 题意: 给定字符串A,B,每次操作可以将字符串A中区间[ i , j ]的字符变为ch, ...

  4. hdu 2476 (string painter) ( 字符串刷子 区间DP)

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

  5. HDU 2476 String painter (区间DP)

    题意:给出两个串a和b,一次只能将一个区间刷一次,问最少几次能让a=b 思路:首先考虑最坏的情况,就是先将一个空白字符串刷成b需要的次数,直接区间DP[i][j]表示i到j的最小次数. 再考虑把a变成 ...

  6. HDU 2476 String painter(记忆化搜索, DP)

    题目大意: 给你两个串,有一个操作! 操作时可以把某个区间(L,R) 之间的所有字符变成同一个字符.现在给你两个串A,B要求最少的步骤把A串变成B串. 题目分析: 区间DP, 假如我们直接想把A变成B ...

  7. hdu2476 String painter(区间dp)

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

  8. HDU2476 String painter —— 区间DP

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

  9. uva live 4394 String painter 区间dp

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

随机推荐

  1. java登录央行征信网站

    package com.entrym.crawler.test; import java.util.HashMap; import java.util.Map; import org.apache.c ...

  2. [原]unity3d GLSL无法pc上显示

    pc编写GLSL: Shader "Custom/GLSL" {    Properties {        _MainTex ("Base (RGB)", ...

  3. Java -- 异常的捕获及处理 -- throws与throw关键字

    7.2 throws 与 throw关键字 7.2.1 throws 关键字 在定义一个方法时可以使用throws关键字声明,使用throws声明的方法标识此方法不处理异常,而交给方法的调用处进行处理 ...

  4. Git Step by Step – (6) Git远程仓库

    前面文章中出现的所有Git操作都是基于本地仓库的,但是日常工作中需要多人合作,不可能一直都在自己的代码仓库工作.所以,这里我们就开始介绍Git远程仓库. 在Git系统中,用户可以通过push/pull ...

  5. flexbox子盒子flex属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Android文件系统编译出错记录

    错误1: 注意:external/protobuf/java/src/main/java/com/google/protobuf/GeneratedMessageLite.java 使用了未经检查或不 ...

  7. RabbitMQ之总结

    P:生成者,消息产生者: C:消息消费者: 红:消息队列: java实现 步骤: 创建连接 从连接中创建通道(相当于JDBC中的Statement) 通过channel声明(创建)队列.(如果队列存在 ...

  8. python --->字典 集合 学习笔记

    1.字典--->创建空字典:dict={} broa=["李宁",”耐克“,“阿迪达斯”,“鱼c工作室”] sloga=[“A”,“B”,“C”,“D”] dict={&qu ...

  9. C++ template —— tuple(十三)

    本系列博文中我们使用同类容器(如数组类型)来阐述模板的强大威力,同时,C/C++还具有包含异类对象的能力.这里的异类指的是类型不同,或者结构不同.tuple就是这样的一个类模板,它能够用于聚集不同类型 ...

  10. codeforces水题100道 第九题 Codeforces Beta Round #63 (Div. 2) Young Physicist (math)

    题目链接:http://www.codeforces.com/problemset/problem/69/A题意:给你n个三维空间矢量,求这n个矢量的矢量和是否为零.C++代码: #include & ...