UVa1625 - Color Length

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 de ned as follows:
L(c) = max location(c) min location(c)
For example, according to Figure 1, location(G) = f1; 5; 6g, location(Y ) = f2; 7g, location(B) =
f3g, and location(R) = f4; 8g. 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 rstly 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 identi er 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 nd 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 rst line of the input. Each test case consists of two lines. In the rst 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
我有话说:
我们可以定义d(i,j)表示两个序列分别已经取了i,j个元素。还需要多少费用。因为考虑到题目问题如果记录下每种颜色出现的位置,状态转移就会非常复杂。所以我们采取的策略是不是等到一种颜色移动完后再算,而是逐步累加。就是当把一个颜色移到最终序列前,把所有“已经出现但还没有结束”的颜色长度代价都加1.

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int maxn = 5000 + 5;
const int INF = 1000000000;
char p[maxn], q[maxn]; //初始状态输入的字符串
int sp[26], sq[26], ep[26], eq[26]; //sp[]表示每种颜色在p串中的开始下标,ep[]表示结束下标
int d[2][maxn], c[2][maxn]; // c[i][j]:在目前出现在新产生的串中未完结的颜色的数目。
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",p+1);
        scanf("%s",q+1);
        int n=strlen(p+1);
        int m=strlen(q+1);
        for(int i=1;i<=n;i++)p[i]-='A';
        for(int i=1;i<=m;i++)q[i]-='A';
        for(int i=0;i<26;i++)
        {
            sp[i]=sq[i]=INF;
            ep[i]=eq[i]=0;
        }
        for(int i=1;i<=n;i++)
        {
            sp[p[i]]=min(sp[p[i]],i);
            ep[p[i]]=i;
        }
        for(int i=1;i<=m;i++)
        {
            sq[q[i]]=min(sq[q[i]],i);
            eq[q[i]]=i;
        }
        int t=0;

        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=m;j++)
            {
                if(!i&&!j)continue;
                int v1=INF,v2=INF;
                if(i)v1=d[t^1][j]+c[t^1][j];//移动p串首字母到新串队尾,因为采取的是双进程策略。可以用t^1读取另一个状态下的数据,即q移动了j个颜色的状态。这样可以轮流使用数组
                if(j)v2=d[t][j-1]+c[t][j-1];//移动q串首字母到新串队尾
                d[t][j]=min(v1,v2);
                if(i)//预判移动p[i]会增加或减少的代价
                {
                    c[t][j]=c[t^1][j];//这时候q还是只移动了前j个颜色
                    if(sp[p[i]]==i&&sq[p[i]]>j)c[t][j]++;
                    if(ep[p[i]]==i&&eq[p[i]]<=j)c[t][j]--;
                }else if(j){
                    c[t][j]=c[t][j-1];
                    if(sq[q[j]]==j&&sp[q[j]]>i)c[t][j]++;
                    if(eq[q[j]]==j&&ep[q[j]]<=i)c[t][j]--;
                }
            }
            t^=1;
        }
        printf("%d\n",d[t^1][m]);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值