博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2476 String painter(区间DP,较难)
阅读量:4037 次
发布时间:2019-05-24

本文共 2022 字,大约阅读时间需要 6 分钟。

1、

2、题目大意:

有两个字符串长度相同,现在有一个painter,一次可以把第一个字符串中的一段区间内的所有字母都换成同一个字母(这个字母可以是任意一个),问最少执行多少次操作,才能将第一个字符串转换成第二个字符串

3、解题思路:

dp[i][j]表示i-j区间内的最少次数

先操作第二个字符串,我们先假设第i个字符的位置需要喷刷一次,对于所有的dp[i][j]=dp[i+1][j]+1、

在i-j区间内,如果有第k个跟第i个相同,那么就可以将i-j区间借助k分成两部分dp[i][j]=min(dp[i+1][k]+dp[k+1][j]);

处理完第2个字符串后,我们就要看第一个字符串究竟需要喷刷多少次了,用ans[i]记录0-i区间第二个字符串得出的喷刷次数,如果第一个字符串的i位置与第二个字符串的i位置相同,那么这个位置就不用喷刷了,ans[i]=ans[i-1],如果不相同,就要就要借助一个位于o-i区间内的变量来分割开

4、题目:

String painter

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

Total Submission(s): 1365    Accepted Submission(s): 585

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
zzzzzfzzzzzabcdefedcbaababababababcdcdcdcdcdcd

 

Sample Output
67

 

Source

 

Recommend
lcy   |   We have carefully selected several similar problems for you: 

 

5、AC代码:

#include
#include
#include
using namespace std;#define N 105char s1[N],s2[N];int dp[N][N];int ans[N];int main(){ while(scanf("%s%s",s1,s2)!=EOF) { int len=strlen(s1); memset(dp,0,sizeof(dp)); for(int j=0; j
=0; i--) { dp[i][j]=dp[i+1][j]+1; for(int k=i+1; k<=j; k++) { if(s2[i]==s2[k]) dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k+1][j]); } } } for(int i=0; i

 

 

转载地址:http://qgddi.baihongyu.com/

你可能感兴趣的文章
[茶余饭后]10大毕业生必听得歌曲
查看>>
gdb调试命令的三种调试方式和简单命令介绍
查看>>
C++程序员的几种境界
查看>>
VC++ MFC SQL ADO数据库访问技术使用的基本步骤及方法
查看>>
VUE-Vue.js之$refs,父组件访问、修改子组件中 的数据
查看>>
Vue-子组件改变父级组件的信息
查看>>
Python自动化之pytest常用插件
查看>>
Python自动化之pytest框架使用详解
查看>>
【正则表达式】以个人的理解帮助大家认识正则表达式
查看>>
性能调优之iostat命令详解
查看>>
性能调优之iftop命令详解
查看>>
非关系型数据库(nosql)介绍
查看>>
移动端自动化测试-Windows-Android-Appium环境搭建
查看>>
Xpath使用方法
查看>>
移动端自动化测试-Mac-IOS-Appium环境搭建
查看>>
Selenium之前世今生
查看>>
Selenium-WebDriverApi接口详解
查看>>
Selenium-ActionChains Api接口详解
查看>>
Selenium-Switch与SelectApi接口详解
查看>>
Selenium-Css Selector使用方法
查看>>