LA 4394 String painter

原创 2016年06月01日 14:59:48

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?


题意:给定两个长度相等,只有小写字母组成字符串A和B,每步可以把A的一个连续子串刷成同一个字母,问至少需要多少步才能把A变成B。


分析:看了题解才会做,设f[i]为前i个字符刷成B的最小次数,则当b[i] = b[i-1]时,f[i] = f[i-1],否则i这个位置至少刷一次;每次涂刷可以用一个区间表示[i,j],而且b[i] == b[j],否则无意义,设dp[i][j]表示i刷到j的最少次数,b[i] == b[i+1]时,dp[i][j] = dp[i+1][j],否则dp[i][j] = min(dp[i+1][k]+dp[k+1][j]) 其中 b[i] == b[k].


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAXN 2147483646
using namespace std;
int f[105],dp[105][105];
char a[105],b[105];
int main()
{
	while(~scanf("%s",a+1))
	{
		scanf("%s",b+1);
		int n = strlen(a+1);
		memset(f,0,sizeof(f));
		memset(dp,0,sizeof(dp));
		for(int i = n;i;i--)
		{
			dp[i][i] = 1;
			for(int j = i+1;j <= n;j++)
			{
				dp[i][j] = dp[i+1][j] + (b[i] == b[i+1] ? 0:1);
				for(int k = i+1;k <= j;k++) 
				 if(b[k] == b[i]) dp[i][j] = min(dp[i][j],dp[i+1][k]+dp[k+1][j]);
			}
		}
		for(int i = 1;i <= n;i++) 
		 if(b[i] == a[i]) f[i] = f[i-1];
		 else
		 {
		 	f[i] = MAXN;
		 	for(int j = i;j;j--)
		 	 if(b[j] == b[i]) f[i] = min(f[i],f[j-1]+dp[j][i]);
		 }
		cout<<f[n]<<endl;
	}	
} 


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

la 4394 string painter 区间dp

开始练dp了,要好好练啊。 这是一道区间dp的题目,每次对序列的一个区间操作,区间dp有一个特点是,对于某一个区间,如果你没有对这个区间进行操作,而对这个区间的子区间进行操作,那么这个区间就没有必要...

hdoj 2476 String painter

题目大意: 给出两个字符串a,b,将a串变为b串,每次可以将连续的一个子串改成任意的一个字母,问最少需要操作多少次。 (字符串长度不会超过100) 题解: 其实如果a串是空串的话,我们可以...

String Painter LA4394

应该有更低的复杂度 #include #include #include #include #include #include #include #include #incl...

LA4394

此题我一开是想想dp【i】【j】表示从i位置到j位置最少变换次数。但是我不知到怎么处理中间过程。即我要如何变换才知道是已经变到当前状态来 也就是说 我二维的情况存不下我前面的子状态。然后搜了下...

Educational Codeforces Round 12 E. Beautiful Subarrays

One day, ZS the Coder wrote down an array of integers a with elements a1,  a2,  ...,  an. A subarr...

Uva 11134 Fabled Rooks

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_proble...

Hdu-5573 Binary Tree(贪心构造)

The Old Frog King lives on the root of an infinite tree. According to the law, each node should conn...

Codeforces Round #283 (Div. 2) D. Tennis Game

Petya and Gena love playing table tennis. A single match is played according to the following rules:...

ICPCCamp 2017-Day1 E.Lowest Common Ancestor(树链剖分/lct)

题意:给一棵带权树,问每个点和所有标号小于它的点的带权lca和。 分析:这种lca和可以看成是两段权值不同的链的差,这样我们用lct来维护树上每段链的带权重量和,每次插入一个点。 ...

Codeforces Round #355 (Div. 2) D. Vanya and Treasure

Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)