답안 #928944

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
928944 2024-02-17T08:40:24 Z Ghulam_Junaid Necklace (Subtask 1-3) (BOI19_necklace1) C++17
0 / 85
9 ms 604 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
 
const ll base = 1e6 + 1;
const ll mod = 1e9 + 9;
const ll N = 3005;
 
unordered_map<ll, ll> ind;
pair<ll, ll> ans[N];
ll pwr[N];
 
int main(){
    pwr[0] = 1;
    for (ll i = 1; i < N; i ++)
        pwr[i] = (1ll * base * pwr[i - 1]) % mod;
 
    string s, t;
    cin >> s >> t;
 
    ll n = s.size();
    ll m = t.size();
 
    for (ll i = 0; i < m; i ++){
        ll hash = 0;
        for (ll j = i; j < m; j ++){
            hash = (1ll * base * hash + t[j]) % mod;
            ind[hash] = i;
        }
    }
 
    ll l = 0;
    ll r = n + 1;
 
    for (int len = n; len >= 1; len--){
        l = len;
        bool good = 0;
 
        for (ll i = 0; i + len - 1 < n; i ++){
            ll hash = 0;
            for (ll 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 (ll 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) break;
    }
 
    assert(l > 0);
 
    vector<pair<ll, pair<ll, ll>>> save;
    int st = ans[l].second;
 
    save.push_back({l, ans[l]});
 
    reverse(s.begin(), s.end());
 
    l = 0;
    r = n + 1;
 
    for (int len = n; len > 0; len --){
        l = len;
        bool good = 0;
 
        for (ll i = 0; i + len - 1 < n; i ++){
            ll hash = 0;
            for (ll 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 (ll 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) break;
    }
 
    assert(l > 0);
 
    save.push_back({l, {n - (ans[l].first + l - 1) - 1, ans[l].second}});
    sort(save.begin(), save.end());
 
    pair<ll, ll> output = save[1].second;
    cout << save[1].first << endl;
    cout << output.first << " " << output.second << endl;
}

Compilation message

necklace.cpp: In function 'int main()':
necklace.cpp:34:8: warning: variable 'r' set but not used [-Wunused-but-set-variable]
   34 |     ll r = n + 1;
      |        ^
necklace.cpp:72:9: warning: unused variable 'st' [-Wunused-variable]
   72 |     int st = ans[l].second;
      |         ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 9 ms 596 KB Output is correct
5 Incorrect 9 ms 600 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 9 ms 596 KB Output is correct
5 Incorrect 9 ms 600 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 5 ms 604 KB Output is correct
4 Correct 9 ms 596 KB Output is correct
5 Incorrect 9 ms 600 KB Output isn't correct
6 Halted 0 ms 0 KB -