답안 #928902

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
928902 2024-02-17T07:51:09 Z Ghulam_Junaid Necklace (Subtask 1-3) (BOI19_necklace1) C++17
0 / 85
1 ms 348 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int base = 727;
const int mod = 1e9 + 7;
const int N = 3005;

unordered_map<int, int> ind;
pair<int, int> ans[N];
int pwr[N];

int main(){
    pwr[0] = 1;
    for (int i = 1; i < N; i ++)
        pwr[i] = (1ll * base * pwr[i - 1]) % mod;

    string s, t;
    cin >> s >> t;

    int n = s.size();
    int m = t.size();

    for (int i = 0; i < m; i ++){
        int hash = 0;
        for (int j = i; j < m; j ++){
            hash = (1ll * base * hash + t[j]) % mod;
            ind[hash] = i;
        }
    }

    int l = 0;
    int r = n + 1;

    while (r - l > 1){
        int len = (l + r) / 2;
        bool good = 0;

        for (int i = 0; i + len - 1 < n; i ++){
            int hash = 0;
            for (int j = i; j < i + len; j ++)
                hash = (1ll * base * hash + s[j]) % mod;
            
            if (ind.find(hash) != ind.end()){
                ans[len] = {i, ind[hash]};
                good = 1;
                break;
            }
            for (int j = i; j < i + len - 1; j ++){
                hash = (-(1ll * s[j] * pwr[len - 1]) + hash) % mod;
                hash += mod;
                hash %= mod;

                hash = (1ll * hash * base + s[j]) % mod;
                if (ind.find(hash) != ind.end()){
                    ans[len] = {i, ind[hash]};
                    good = 1;
                    break;
                }
            }

            if (good) break;
        }

        if (good)
            l = len;
        else
            r = len;
    }

    vector<pair<int, pair<int, int>>> save;
    save.push_back({l, ans[l]});

    reverse(s.begin(), s.end());

    l = 0;
    r = n + 1;

    while (r - l > 1){
        int len = (l + r) / 2;
        bool good = 0;

        for (int i = 0; i + len - 1 < n; i ++){
            int hash = 0;
            for (int j = i; j < i + len; j ++)
                hash = (1ll * base * hash + s[j]) % mod;
            
            if (ind.find(hash) != ind.end()){
                ans[len] = {i, ind[hash]};
                good = 1;
                break;
            }
            for (int j = i; j < i + len - 1; j ++){
                hash = (-(1ll * s[j] * pwr[len - 1]) + hash) % mod;
                hash += mod;
                hash %= mod;

                hash = (1ll * hash * base + s[j]) % mod;
                if (ind.find(hash) != ind.end()){
                    ans[len] = {i, ind[hash]};
                    good = 1;
                    break;
                }
            }

            if (good) break;
        }

        if (good)
            l = len;
        else
            r = len;
    }

    save.push_back({l, {l + ans[l].first - 1, ans[l].second}});
    sort(save.begin(), save.end());

    pair<int, int> output = save[1].second;
    // cout << save[1].first << endl;
    cout << output.first << " " << output.second;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 348 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -