1 条题解

  • 0
    @ 2026-11-2 10:56:23

    相等子串构造(Many Equal Substrings) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, k;
        if (!(cin >> n >> k)) return 0;
        string t;
        cin >> t;
        vector<int> pi(n, 0);
        for (int i = 1; i < n; ++i) {
            int j = pi[i - 1];
            while (j > 0 && t[i] != t[j]) j = pi[j - 1];
            if (t[i] == t[j]) ++j;
            pi[i] = j;
        }
        int border = pi[n - 1];
        string suffix = t.substr(border);
        string ans = t;
        for (int i = 1; i < k; ++i) ans += suffix;
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8574
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者