[UVa1625]颜色的长度
2017-07-18 09:26
330 查看
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时, 所有已出现但未结束的字符的数量
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 , w ; char cn , cm ; 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 [m] ); } return 0; }
相关文章推荐
- UVA1625 颜色的长度(color length)(重庆一中高2018级信息学竞赛测验10) 解题报告
- 颜色的长度(uva 1625)
- 动态规划(颜色的长度,uva 1625)
- UVA 1625 Color Length 颜色的长度 (预处理+dp)
- UVa 1625 颜色的长度
- UVA1625颜色的长度
- uva1625 颜色的长度
- Uva 1625,颜色的长度
- 【UVa1625】颜色的长度
- UVa - 1625 Color Length 颜色的长度 动态规划 重庆一中高2018级竞赛班第十次测试 2016.9.16 Problem 2
- iOS 自定义UITextField中的光标颜色、长度或高度
- UVA1625 线性DP
- css知识笔记(四)——代码简写、颜色值、长度值
- UVA 1625 Color Length
- UVa 350 Pseudo-Random Numbers (伪随机数的循环长度)
- Uva1625 线性DP
- UVA1625 - Color Length (DP)
- uva 1625——Color Length
- UVA 497 Strategic Defense Initiative【最长严格递增子序列长度及打印】
- UVa 11404 回文子序列(LCS求最长回文串长度)