题解:CF2242D Two Digit Strings

· · 题解

简单题,比 C 简单。

:::info[记号]{open} 记原串为 s 和 t,记答案串为 \text{ans}。

记 \text{len}_{s} 为字符串 s 的长度。

我们考虑观摩这个形式,然后发现最终的答案串必然每一个字符都对应原串的一个连续区间合并而得。

不妨设答案串中第 i 个字符来自 s 中的 \text{ls}_i \sim \text{rs}_i,t 中的 \text{lt}_i \sim \text{rt}_i。

那么有 \sum\limits_{x=\text{ls}_i}^{\text{rs}_i} s_x \equiv \sum\limits_{x=\text{lt}_i}^{\text{rt}_i} t_x \equiv \text{ans}_i \pmod{10}。

然后观察得到 \forall 1 \leq i \leq \text{len}_{\text{ans}} - 1,有 \text{rs}_i + 1 = \text{ls}_{i + 1},\text{rt}_i + 1 = \text{lt}_{i + 1},并且 \text{ls}_1 = \text{lt}_1 = 1,\text{rs}_{\text{len}_{\text{ans}}} = \text{len}_{s},\text{rt}_{\text{len}_{\text{ans}}} = \text{len}_{t}。

通俗易懂的将,就是答案串每个字符的 \text{ls}_i \sim \text{rs}_i 将 s 分成了互不相交且不断递增的连续段。

同理,答案串每个字符的 \text{lt}_i \sim \text{rt}_i 将 t 分成了互不相交且不断递增的连续段。

我们考虑前缀和。

那么 \text{ps}_i = \sum\limits_{j = 1}^{i} s_j,\text{pt}_i = \sum\limits_{j = 1}^{i} t_j。

于是限制是 \text{ps}_{\text{rs}_i} - \text{ps}_{\text{ls}_i - 1} \equiv \text{pt}_{\text{rt}_i} - \text{pt}_{\text{lt}_i - 1} \equiv \text{ans}_i \pmod{10}。

然后注意到 \text{ls}_i - 1 = \text{rs}_{i - 1},\text{lt}_i - 1 = \text{rt}_{i - 1}(我们定义 \text{rs}_0 = \text{rt}_0 = 0)。

于是,我们发现 \text{ps}_{\text{rs}_i} - \text{ps}_{\text{rs}_{i - 1}} \equiv \text{pt}_{\text{rt}_i} - \text{pt}_{\text{rt}_{i - 1}} \equiv \text{ans}_i \pmod{10}。

然后 \text{ans}_i 是我们自己定的。

所以梳理一下,我们发现,\forall 1 \leq i \leq \text{len}_{\text{ans}},有 \text{ps}_{\text{rs}_i} = \text{ps}_{\text{rt}_i}。

于是判断 \text{ps}_{\text{len}_s} 是否等于 \text{pt}_{\text{len}_t} 之后求 \text{ps} 和 \text{pt} 的 LCS(最长公共子序列)即可。

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
bool begin_mem;
const int N = 5e3 + 5, M = 1e6 + 5;
const int inf = 1e9, mod = 998244353;
const ll INF = 1e18;

int n, m, s[N], t[N];

void init(){
}
void solve(){
    {
        string _;
        cin >> _;
        n = _.size();
        for (int i = 1; i <= n; i ++ ) s[i] = _[i - 1] - '0';
    }
    {
        string _;
        cin >> _;
        m = _.size();
        for (int i = 1; i <= m; i ++ ) t[i] = _[i - 1] - '0';
    }

    for (int i = 1; i <= n; i ++ ) s[i] = (s[i - 1] + s[i]) % 10;
    for (int i = 1; i <= m; i ++ ) t[i] = (t[i - 1] + t[i]) % 10;
    if (s[n] != t[m]){
        cout << "-1\n";
        return ;
    }

    vector <vector <int>> f(n + 1, vector <int> (m + 1, 0));
    for (int i = 1; i <= n; i ++ )
        for (int j = 1; j <= m; j ++ ){
            f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            if (s[i] == t[j]) f[i][j] = max(f[i - 1][j - 1] + 1, f[i][j]);
        }

    if (f[n][m] == 0) f[n][m] = -1;
    cout << f[n][m] << "\n";
}

bool end_mem;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int T = 1;
    cin >> T;
    while (T -- ) init(), solve();

    cerr << "Memory AwA " << fixed << setprecision(13) << abs(&begin_mem - &end_mem) / 1024.0 / 1024.0 << " Mb.\n";
    cerr << "Memory AwA " << fixed << setprecision(13) << abs(&begin_mem - &end_mem) / 1024.0 / 1024.0 / 1024.0 << " Gb.\n";
}

/*



*/

后记

vp 这一场,评价是在 C 蠕动了一个小时还剩 10 分钟看 D 直接秒杀。