HDU 6774 String Distance 序列自动机优化lcs

原题链接:https://acm.hdu.edu.cn/showproblem.php?pid=6774

题意

先给你字符串S和T,我们定义两种操作

  1. 在任意串上删除一个字符
  2. 在任意串上插入一个字符

我们再定义字符串距离 ∣ s , t ∣ |s,t| s,t为通过最少操作次数使得s和t相同,每次询问一个区间[l,r],即S[l:r]和T的字符串距离。

分析

让两个串相等,其实只需要通过第一个操作一定可以最优表示出来,因此就把题目转化为求S[l:r]和T的最长公共子序列。

如果直接暴力去匹配,复杂度是 O ( q n m ) O(qnm) O(qnm),肯定会超时。这里可以用到序列自动机的优化技巧,我们预处理一个 n e [ i ] [ j ] ne[i][j] ne[i][j]表示S[i:n]中第一次出现字符j的位置。同时用 f [ i ] [ j ] f[i][j] f[i][j]表示T串前i个字符已经匹配且LCS为j时S串匹配到的位置。这个思路其实是比较绕的,而且有很多细节的地方需要处理,转移方程:
f [ i ] [ j ] = m i n ( f [ i − 1 ] [ j ] , n e [ f [ i − 1 ] [ j − 1 ] ] [ t [ i ] − a ] ) f[i][j]=min(f[i-1][j],ne[f[i-1][j-1]][t[i]-a]) f[i][j]=min(f[i1][j],ne[f[i1][j1]][t[i]a])

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ul;
typedef pair<int, int> PII;
const ll inf = 2e18;
const int N = 2e5 + 10;
const int M = 1e6 + 10;
const ll mod = 998244353;
const double eps = 1e-8;

#define lowbit(i) (i & -i)
#define Debug(x) cout << (x) << endl
#define fi first
#define se second
#define mem memset
#define endl '\n'

char s[N];
char t[N];
int ne[N][30];
int dp[30][30];
void pre(int n) {
    for (int i = n; i >= 1; i--) {
        if (i == n)
            for (int j = 0; j < 26; j++) ne[i][j] = n + 1;
        for (int j = 0; j < 26; j++) {
            ne[i-1][j] = ne[i][j];
        }
        ne[i-1][s[i]-'a'] = i;
    }
}
inline void solve() {
    int T; cin >> T; while (T--) {
        cin >> (s + 1);
        int n = strlen(s + 1);
        pre(n);
        cin >> (t + 1);
        int m = strlen(t + 1);
        int q; cin >> q; while (q--) {
            int l, r; cin >> l >> r;
            memset(dp, 0x3f, sizeof dp);
            int ans = 0;
            dp[0][0] = l-1;
            for (int i = 1; i <= m; i++) {
                for (int j = 0; j <= m; j++) {
                    if (dp[i-1][j] <= r) dp[i][j] = dp[i-1][j];
                    if (j && dp[i-1][j-1] <= r && ne[dp[i-1][j-1]][t[i]-'a'] <= r) {
                        dp[i][j] = min(dp[i][j], ne[dp[i-1][j-1]][t[i]-'a']);
                    }
                    if (dp[i][j] <= r) ans = max(ans, j);
                }
            }
            cout << r - l + 1 + m - ans * 2 << endl;
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值