[UVa1625]颜色的长度

Cars painted in different colors are moving in a row on the road as shown in Figure 1. The color of eachcar is represented by a single character and the distance of two adjacent cars is assumed to be 1. Figure1 shows an example of such cars on the road. For convenience, the numbers in Figure 1 represent thelocations of each car.
Figure 1. Cars in different colors on the road
For any color c, location(c) represents set of the locations of the cars painted in color c and colorlength L(c) is defined as follows:
L(c) = max location(c) − min location(c)

For example, according to Figure 1, location(G) = {1, 5, 6}, location(Y ) = {2, 7}, location(B) ={3}, and location(R) = {4, 8}. Hence the color length of each color and the sum of the color lengthsare as follows.

Color G Y B R Sum

L(c) 5 5 0 4 14

In Gyeongju City, almost all the roads including the main street of the city were constructed morethan 500 years ago. The roads are so old that there are a lot of puddles after rain. Visitors havecomplained about the bad condition of the roads for many years. Due to the limited budget, the mayorof the city decided to repair firstly the main street of the city, which is a four-lane road, two lanes foreach direction.

However, since the main street is a backbone of the city, it should not be blocked completely whileit is under repair, or it is expected that serious traffic jams will occur on almost all the other roads inthe city. To allow cars to use the main street during the repair period, the city decided to block onlytwo lanes, one lane for each direction. Hence, the cars in the two lanes for each direction should mergeinto a single lane before the blocked zone.

For instance, as shown in Figure 2, cars in the two lanes merge into a single lane as shown in Figure3. To differentiate the cars in the same color, a unique identifier is assigned to each car.

Figure 2. Cars moving in two lanes before merging

Figure 3 shows two different merging scenarios after merging the cars from the two lanes. As shownin Figure 3, cars in the two lanes do not necessarily merge one by one from each lane. The distancebetween two adjacent cars after merging is also assumed 1.

After merging (Scenario 1):

After merging (Scenario 2):

Figure 3. Two different merging scenarios

For each merging scenario shown in Figure 3, the color length for each color and the sum of thecolor lengths are as follows:

Color G Y B R Sum

L(c): Scenario1 7 3 7 2 19

L(c): Scenario2 1 7 3 1 12

As you can imagine, there are many different ways of merging other than the two examples shownin Figure 3.

Given two character strings which represent the color information of the cars in the two lanes beforemerging, write a program to find the sum of color lengths obtained from the character string, which isthe color information of cars after merging, such that the sum of color lengths is minimized.

Input

Your program is to read from standard input. The input consists of T test cases. The number of testcases T is given in the first line of the input. Each test case consists of two lines. In the first line, acharacter string of length n (1 ≤ n ≤ 5, 000) that is the color information of the cars in one lane beforemerging is given. In the second line, a character string of length m (1 ≤ m ≤ 5, 000) that is the colorinformation of the cars in the other lane is given. Every color is represented as an uppercase letter inEnglish, hence the number of colors is less than or equal to 26.

Output

Your program is to read from standard input. Print exactly one line for each test case. The line shouldcontain the sum of color lengths after merging the cars in the two lanes optimally as described above.The following shows sample input and output for two test cases.

Sample Input

2

AAABBCY

ABBBCDEEY

GBBY

YRRGB

Sample Output

10

12


题意:

输入两个长度分别为n和m(n,m≤5000)的颜色序列,要求按顺序合并成同一个序列,即每次可以把一个序列开头的颜色放到新序列的尾部。

例如,两个颜色序列GBBY和YRRGB至少有两种合并结果:GBYBRYRGB和YRRGGBBYB。对于每个颜色c来说,其跨度L(c)等于最大位置和最小位置之差。例如,对于上面两种合并结果,每个颜色的L(c)和所有L(c)的总和如图所示。

Color  G  Y B R Sum

L(c): Scenario1 7 3 7 2 19

L(c): Scenario2 1 7 3 1 12

找到一种合并方式,使得所有L(c)的总和最小。


题解:

dp[i][j]: 表示第一列取到i, 第二列取到j, 的最小长度
dp[i][j]=min(dp[i-1][j]+w[i-1][j],dp[i][j-1]+w[i][j-1])
预处理每种颜色在两个序列中出现的位置s[i][0/1]和e[i][0/1]
将所有已出现但未结束的字符的长度+1
用w[i][j]储存第一列取到i, 第二列取到j时, 所有已出现但未结束的字符的数量


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=5010;
int T, n, m;
int s[30][2], e[30][2], dp[N][N], w[N][N];
char cn[N], cm[N];

void Pre() {
	scanf( "%s%s", cn+1, cm+1 );
	n=strlen(cn+1);
	m=strlen(cm+1);
	memset( s, INF, sizeof s );
	memset( e, -1, sizeof e );
	for( int i=1; i<=n; i++ ) {
		int ncol=cn[i]-'A'+1;
		if( s[ncol][0]==INF ) s[ncol][0]=i;
		e[ncol][0]=i;
	}
	for( int i=1; i<=m; i++ ) {
		int mcol=cm[i]-'A'+1;
		if( s[mcol][1]==INF ) s[mcol][1]=i;
		e[mcol][1]=i;
	}
}

int main() {
	for( scanf( "%d", &T ); T; T-- ) {
		Pre();
		for( int i=0; i<=n; i++ )
			for( int j=0; j<=m; j++ ) {
				if( !i && !j ) continue;
				int vn=INF, vm=INF;
				if(i) vn=dp[i-1][j]+w[i-1][j];//从n序列中取
				if(j) vm=dp[i][j-1]+w[i][j-1];//从m序列中取
				dp[i][j]=min( vn, vm );
				if(i) {
					w[i][j]=w[i-1][j];
					if( s[ cn[i]-'A'+1 ][0]==i && s[ cn[i]-'A'+1 ][1]>j ) w[i][j]++;
					if( e[ cn[i]-'A'+1 ][0]==i && e[ cn[i]-'A'+1 ][1]<=j ) w[i][j]--;
				}
				else if(j) {
					w[i][j]=w[i][j-1];
					if( s[ cm[j]-'A'+1 ][1]==j && s[ cm[j]-'A'+1 ][0]>i ) w[i][j]++;
					if( e[ cm[j]-'A'+1 ][1]==j && e[ cm[j]-'A'+1 ][0]<=i ) w[i][j]--;
				}
			}
		printf( "%d\n", dp[n][m] );
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值