UVA 1625 Color Length (计数DP+DP预处理+思维)

Cars painted in different colors are moving in a row on the road as shown in Figure 1. The color of each car is represented by a single character and the distance of two adjacent cars is assumed to be 1. Figure 1 shows an example of such cars on the road. For convenience, the numbers in Figure 1 represent the locations 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 color length L(c) is defined as follows: L(c) = maxlocation(c)−minlocation(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 lengths are 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 more than 500 years ago. The roads are so old that there are a lot of puddles after rain. Visitors have complained about the bad condition of the roads for many years. Due to the limited budget, the mayor of the city decided to repair firstly the main street of the city, which is a four-lane road, two lanes for each direction. However, since the main street is a backbone of the city, it should not be blocked completely while it is under repair, or it is expected that serious traffic jams will occur on almost all the other roads in the city. To allow cars to use the main street during the repair period, the city decided to block only two lanes, one lane for each direction. Hence, the cars in the two lanes for each direction should merge into 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 Figure 3. 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 shown in Figure 3, cars in the two lanes do not necessarily merge one by one from each lane. The distance between 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 the color lengths are as follows:
Color G Y B R Sum L(c): Scenario 1 7 3 7 2 19 L(c): Scenario 2 1 7 3 1 12
As you can imagine, there are many different ways of merging other than the two examples shown in Figure 3. Given two character strings which represent the color information of the cars in the two lanes before merging, write a program to find the sum of color lengths obtained from the character string, which is the 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 test cases T is given in the first line of the input. Each test case consists of two lines. In the first line, a character string of length n (1 ≤ n ≤ 5,000) that is the color information of the cars in one lane before merging is given. In the second line, a character string of length m (1 ≤ m ≤ 5,000) that is the color information of the cars in the other lane is given. Every color is represented as an uppercase letter in English, 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 should contain 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

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define maxn 5010
#define ll long long
#define INF 10000000
using namespace std;

char s1[maxn],s2[maxn];
int b1[26],e1[26];
int b2[26],e2[26];
int cnt[maxn][maxn],dp[maxn][maxn];

/*
题目大意:给定两个字符串序列,要求最终合并成一个字符串,
且该字符串的指标函数最小,指标函数的定义是每个字母距离函数之和,
每个字母距离函数的定义是出现的最大距离。

关键逻辑:当把一个颜色移到最终序列前,需要把之前的所有已经出现但未结束的颜色长度加一,
所以需要一个数组cnt计数,即在(i,j)状态有多少个颜色已经开始但尚未结束。

那么计数数组的DP逻辑便是,如果当前第一段i位字符是刚好开始但第二段未开始的情况则加一
如果是在第一段刚好结束但第二段已经结束则减一。
同理对第二段也是如此。

然后利用得到的cnt数组,对dp进行计数。
为什么是对dp数组进行比较时用的是cnt[i-1][j],和cnt[i][j-1]?不应该是cnt[i][j]嘛?
因为对dp(i,j)来说,dp(i-1,j)和dp(i,j-1)两种方式到达的dp(i,j)肯定是不一样的指标,
移动的字符不一样,所以对于两种情况肯定附加不一样的指标。

其实也有数学语言覆盖的思想来解释。。。。
*/
int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s1+1);
        scanf("%s",s2+1);
        int len1=strlen(s1+1),len2=strlen(s2+1);

        memset(b1,0xf,sizeof(b1));
        memset(e1,0xff,sizeof(e1));///这样的初始化是为了应付有些字符只存在于一个串的情况
        for(int i=1;i<=len1;i++)
        {
            int tp=s1[i]-'A';
            b1[tp]=min(b1[tp],i);
            e1[tp]=max(e1[tp],i);
        }

        memset(b2,0xf,sizeof(b2));
        memset(e2,0xff,sizeof(e2));
        for(int i=1;i<=len2;i++)
        {
            int tp=s2[i]-'A';
            b2[tp]=min(b2[tp],i);
            e2[tp]=max(e2[tp],i);
        }
        
        ///动态规划中超时的大忌
        //memset(cnt,0,sizeof(cnt));
        //memset(dp,0,sizeof(dp));
        cnt[0][0]=0;
        for(int i=0;i<=len1;i++)
            for(int j=0;j<=len2;j++)
        {
            if(!i&&!j) continue;
            int t1=INF,t2=INF;
            if(i) t1=dp[i-1][j]+cnt[i-1][j];
            if(j) t2=dp[i][j-1]+cnt[i][j-1];
            dp[i][j]=min(t1,t2);

            if(i)
            {
                int tp1=s1[i]-'A';
                cnt[i][j]=cnt[i-1][j];
                if( b1[tp1] == i && b2[tp1]>j ) cnt[i][j]++;///如果在i处开始端但在第二段未开始,则计数加一
                if( e1[tp1] == i && e2[tp1]<=j ) cnt[i][j]--;///如果在i处结束端但在第二段未结束,则计数减一。
            }
            else
            {
                 int tp2=s2[j]-'A';
                 cnt[i][j]=cnt[i][j-1];
                 if(b2[tp2] == j && b1[tp2]>i ) cnt[i][j]++;
                 if(e2[tp2] == j && e1[tp2]<=i ) cnt[i][j]--;
            }
        }
        printf("%d\n",dp[len1][len2]);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值