이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll base = 727;
const ll mod = 1e9 + 7;
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 = min(n, m) + 1;
while (r - l > 1){
ll len = (l + r) / 2;
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)
l = len;
else
r = len;
}
vector<pair<ll, pair<ll, ll>>> save;
save.push_back({l, ans[l]});
reverse(s.begin(), s.end());
l = 0;
r = min(n, m) + 1;
while (r - l > 1){
ll len = (l + r) / 2;
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)
l = len;
else
r = len;
}
save.push_back({l, {l + ans[l].first - 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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |