2020 HDU多校 第二场 12-String Distance(DP + 序列自动机)

题目链接: 12-String Distance

Description

题意:给出串a, b, 求给定询问长度的a串与b串的距离

For two strings S and T, you can do the following operation for arbitrary number of times: Select a string S or T, insert or delete a character at any position. The distance between two strings S and T is defined as the minimum number of operations to make S and T equal.

You will be given two strings A[1…n],B[1…m] and q queries.

In each query, you will be given two integers li and ri (1≤li≤r~~≤n), you need to find the distance between the continous substring A[li…ri] and the whole string B.

Input

  • The first line of the input contains a single integer T (1≤T≤10), the number of test cases.
  • For each case, the first line of the input contains a string A consists of n (1≤n≤100000) lower-case English letters.
  • The second line of the input contains a string B consists of m (1≤m≤20) lower-case English letters.
  • The third line of the input contains a single integer q (1≤q≤100000), denoting the number of queries.
  • Then in the following q lines, there are two integers li,ri (1≤li≤ri≤n) in each line, denoting a query.

Output

For each query, print a single line containing an integer, denoting the answer.

Sample Input

1
qaqaqwqaqaq
qaqwqaq
3
1 7
2 8
3 9

Sample Output

4
2
0

Method

  • 由题意可知插入操作能够达到的删除操作就可以完成,所以显然是要求a, b 串的最长公共子序列(LCS),最后用 a.size() + b.size() - 2*LCS 即可得到最终结果;
  • 但是普通的求LCS的求法复杂度是O(n*m),但是题目给定的需要求值的串是a串的子串,且询问 小于 1e5,重复求LCS的复杂度过大,但是使用 序列自动机 预处理a串中的字符即可解决;
  • 蒟蒻哭泣,又到了知识盲区了

Code

详见注释

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

using namespace std;
#pragma GCC optimize(2)
#define ll long long
const int Max = 100005;
const int mod = 1e9+7;
template<typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }

int T, n, l, r, len, m;
char a[Max], b[Max];
int g[Max][30], dp[30][30];

int solve()
{
	int ans=0;
    for(int i=0; i<=m; i++)			//初始化dp数组 
    {
        for(int j=1; j<=m; j++)
            dp[i][j] = Max-1;
        dp[i][0] = l;
    }
    for(int i=1; i<=m; i++)			//更新LCS 
        for(int j=1; j<=i; j++)
            dp[i][j] = min(dp[i-1][j], g[dp[i-1][j-1]][b[i]-'a']+1);
    for(int i=1; i<=m; i++)			//找到最长的公共子序列长度 
        for(int j=1; j<=i; j++)
            if(dp[i][j]<=r+1)
                ans = max(ans, j);
    printf("%d\n", m+r-l+1-2*ans);
}

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        memset(g, mod, sizeof(g));
        scanf("%s%s", a+1, b+1);
        len = strlen(a+1);
        m = strlen(b+1);
        for(int i=0; i<=25; i++)		//初始化g数组 
            g[len+1][i] = 1e9;
        for(int i=len; i>=1; i--)		//记录a串中字符出现位置 
        {
            for(int j=0; j<=25; j++)
                g[i][j] = g[i+1][j];
            g[i][a[i]-'a'] = i;
        }
        scanf("%d", &n);
        while(n--)
        {
            scanf("%d%d", &l, &r); 
			solve();
        }
    }
    return 0;
}

代码参考LilyWhite巨佬的 博客


蒟蒻一只,欢迎指正

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值