1 条题解

  • 0
    @ 2025-4-7 21:19:29

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <map>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const ll MOD = 1000000007;
    const ll P = 10000019;
    const ll MAXN = 1010;
    ll powP[MAXN], H1[MAXN] = {0}, H2[MAXN] = {0};
    vector<pair<int, pair<int, int> > > pr1, pr2;
    void init(int len) {
        powP[0] = 1;
        for(int i = 1; i <= len; i++) {
            powP[i] = (powP[i - 1] * P) % MOD;
        }
    }
    void calH(ll H[], string &str) {
        H[0] = str[0];
        for(int i = 1; i < str.length(); i++) {
            H[i] = (H[i - 1] * P + str[i]) % MOD;
        }
    }
    int calSingleSubH(ll H[], int i, int j) {
        if(i == 0) return H[j];
        return ((H[j] - H[i - 1] * powP[j - i + 1]) % MOD + MOD) % MOD;
    }
    void calSubH(ll H[], int len, vector<pair<int, pair<int, int> > > &pr) {
        for(int i = 0; i < len; i++) {
            for(int j = i; j < len; j++) {
                int hashValue = calSingleSubH(H, i, j);
                pr.push_back(make_pair(hashValue, make_pair(i, j)));
            }
        }
    }
    pair<int, int> getMax() {
        pair<int, int> ans = make_pair(0, -1);
        for(int i = 0; i < pr1.size(); i++) {
            for(int j = 0; j < pr2.size(); j++) {
                if(pr1[i].first == pr2[j].first) {
                    if(pr1[i].second.second - pr1[i].second.first > ans.second - ans.first) {
                        ans = pr1[i].second;
                    }
    //                ans = max(ans, pr1[i].second);
                }
            }
        }
        return ans;
    }
    int main() {
        string str1, str2;
        getline(cin, str1);
        getline(cin, str2);
        init(max(str1.length(), str2.length()));
        calH(H1, str1);
        calH(H2, str2);
        calSubH(H1, str1.length(), pr1);
        calSubH(H2, str2.length(), pr2);
        pair<int, int> ans = getMax();
        if(ans.second - ans.first == 0) cout << "false" << endl;
        else cout << str1.substr(ans.first, ans.second - ans.first + 1) << endl;
        return 0;
    }
    
    
    • 1

    信息

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