题解:P9016 [USACO23JAN] Find and Replace G

· · 题解

P9016 [USACO23JAN] Find and Replace G

原题很清晰,直接进入分析。

题目分析

看到一个东西向另一个东西对应,很容易想到建图。

我们从字符串 s 里面的每一个字符向字符 c 连边。这里我们建一个虚点,向每一个当前没有入度的字符为 c 的点连边(可以使用一个 vector 记录此类点的编号,使用一个数组记录每个点对应的字符用于输出答案),然后再把所有字符串中的字符 s[i] 向这个点连边。

建完图后,我们在这个图上跑拓扑排序,同时使用一个数组记录每个字符最后转化成几个字符。

最后我们进行类似线段树区间查询的一个操作,详见代码。

但这样是过不了的,于是我们进行优化。注意到图中有很多链式结构,我们考虑将其使用类似并查集的操作,缩成一条边。详见下图:

Code

#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#define int long long
#define rd() read128()
#define pr(a) print128(a)
#define pc(b) putchar(b)
#define pel(c) pr(c), pc('\n')
#define psp(d) pr(d), pc(' ')
using namespace std;
int read128()
{
    int ttpttp = 0, flgflg = 1;
    char chrchr = getchar();
    if(chrchr == '-') flgflg = -1;
    while((chrchr < '0' || chrchr > '9') && chrchr != '-') chrchr = getchar();
    if(chrchr == '-') flgflg = -1, chrchr = getchar();
    while(chrchr >= '0' && chrchr <= '9') ttpttp = ttpttp * 10 + chrchr - '0', chrchr = getchar();
    return ttpttp * flgflg;
}
void print128(int ttpttp)
{
    char char_inserted_list[38];
    if(ttpttp < 0) putchar('-'), ttpttp *= -1;
    int iiiiii = 0;
    while(ttpttp > 0)
    {
        char_inserted_list[++iiiiii] = (char)(ttpttp % 10 + '0');
        ttpttp /= 10;
    }
    if(iiiiii == 0) putchar('0');
    else
    {
        for(int i = iiiiii; i >= 1; i--) putchar(char_inserted_list[i]);
    }
}// 快读
vector<int> cnt[26];
vector<int> a[1000010];
vector<int> bb[1000010];
char mp[1000010];// 字符表
int cur, l, r, n;
int dp[1000010];// 记录转化
int lst[1000010];
int ind[1000010];
void dfs(int p, __int128 sm)
{
    //psp(p), psp(sm), pel(dp[p]);
    if(dp[p] == 1)// 找到 dp[i] = 1 即返回
    {
        pc(mp[p]);
        return;
    }
    __int128 ssm = sm;
    for(int i = 0; i < bb[p].size(); i++)
    {
        if(dp[bb[p][i]] == 2e18 && ssm < r) //此时这部分一定包含答案且后面没有包含答案的部分,自证不难
        {
            dfs(bb[p][i], ssm);
            break;
        }
        if((ssm < l && ssm + dp[bb[p][i]] >= l) || (ssm >= l && ssm < r))
            dfs(bb[p][i], ssm);
        ssm += dp[bb[p][i]];
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin >> l >> r >> n;
    cnt[0].push_back(1);
    mp[1] = 'a';
    cur = 1;
    for(int i = 1; i <= n; i++)
    {
        char b;
        cin >> b;
        string s;
        cin >> s;
        if(cnt[b - 'a'].empty()) continue;
        int p;
        if(cnt[b - 'a'].size() > 1)// 建图
        {
            cur++;
            mp[cur] = b;
            for(int j = 0; j < cnt[b - 'a'].size(); j++)
                a[cur].push_back(cnt[b - 'a'][j]), ind[cnt[b - 'a'][j]]++;
            p = cur;
        }
        else p = cnt[b - 'a'][0];
        cnt[b - 'a'].clear();
        for(int j = 0; j < s.size(); j++)
        {
            a[++cur].push_back(p);
            mp[cur] = (char)s[j];
            ind[p]++;
            cnt[s[j] - 'a'].push_back(cur);
        }
    }
    queue<int> q;
    for(int i = 1; i <= cur; i++) if(ind[i] == 0) q.push(i), dp[i] = 1, lst[i] = i;
    while(!q.empty())// 拓扑
    {
        int t = q.front();
        q.pop();
        if(dp[t] == 1) mp[t] = mp[lst[t]];// 上传字符
        // lst 记录它下面一个点的编号
        for(int i = 0; i < a[t].size(); i++)
        {
            ind[a[t][i]]--;
            dp[a[t][i]] += dp[t];
            lst[a[t][i]] = t;
            if(dp[a[t][i]] > 1e18) dp[a[t][i]] = 2e18;
            // 这里使用这个操作避免出现__int128减少常数
            if(ind[a[t][i]] == 0) q.push(a[t][i]);
        }
    };
    for(int i = 1; i <= cur; i++)
        for(int j = 0; j < a[i].size(); j++)
            bb[a[i][j]].push_back(i);
    dfs(1, 0);
    return 0;
}

后记

感谢wangzhilin_lg大佬提供的指点。

有人说这个算法复杂度是假的,如果已经有 hack,请 hack 掉我并且在此题讨论版中 at 我。

另:我的做法没有完全优化,过一段时间会写一版完全优化的版本,请在那之后 hack 或者 hack 使用我的想法的人。