This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template<class A, class B>
ostream& operator<<(ostream& os, const pair<A, B> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const vector<T> &v) {
os << '{';
bool osu = 1;
for(auto &i : v) { if(!osu) os << ','; os << i; osu = 0; }
os << '}';
return os;
}
template<class T>
ostream& operator<<(ostream& os, const deque<T> &v) {
os << '{';
bool osu = 1;
for(auto &i : v) { if(!osu) os << ','; os << i; osu = 0; }
os << '}';
return os;
}
template<class T,long long unsigned int size>
ostream& operator<<(ostream& os, const array<T,size> &arr) {
os << '{';
bool osu = 1;
for(auto &i : arr) { if(!osu) os << ','; os << i; osu = 0; }
os << '}';
return os;
}
#define ll long long
#define ull unsigned long long
#define lll __int128
#define rep(i,n,N) for(int i=(n);i<=(N);++i)
#define For(i,n,N) for(int i=(n);i< (N);++i)
#define rap(i,n,N) for(int i=(n);i>=(N);--i)
#define rip(i,n,N,V) for(int i=(n);i<=(N);i+=V)
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
//#define le left
//#define ri right
#define fi first
#define se second
//#define ff fi.fi
//#define fs fi.se
//#define sf se.fi
//#define ss se.se
#define lc (id<<1)
#define rc ((id<<1)|1)
#define debug(x) cout << #x << " -> " << (x) << endl
#define out(x,y) cout << ">> " << (x) << " " << (y) << endl
#define ou3(x,y,z) cout << ">> " << (x) << " " << (y) << " " << (z) << endl
#define mems(x,y) memset(&x,y,sizeof x)
#define memsv(x,y) memset(&x[0], y, sizeof(x[0]) * x.size())
#define popcount __builtin_popcountll
#define clz(x) (1<<(31-__builtin_clz(x)))
#define clzll(x) (1LL<<(63-__builtin_clzll(x)))
#define all(x) x.begin(),x.end()
#define rsort(x) sort(x), reverse(x)
#define revsort(a,b) sort(a,b), reverse(a,b)
#define PQ(T) priority_queue<T>
#define PQr(T) priority_queue<T, vector<T>, greater<T>>
#define permute(x) while(next_permutation(all(x)))
#define distinct(x) x.erase(unique(all(x)),x.end())
#define vi vector<int>
#define vvi vector<vi>
#define vii vector<pii>
#define vll vector<ll>
#define vlll vector<pll>
#define vvll vector<vll>
#define ari(x) array<int,x>
#define arll(x) array<ll,x>
#define ard(x) array<double,x>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plll pair<ll,pll>
#define pllll pair<pll,pll>
#define piii pair<int,pii>
#define piiii pair<pii,pii>
#define psi pair<string,int>
#define endl '\n'
const int MAX = 3e3+5;
const ll MOD = 2078628311;
const ll MOD2 = 2010405347;
const ll INF = 2e18;
const int dr[]={1,-1,0,0,1,1,-1,-1};
const int dc[]={0,0,-1,1,1,-1,1,-1};
const double pi = acos(-1);
const double EPS = 1e-9;
const int block = 2e3;
inline ll pw(ll x,ll y,ll md=MOD){ll ret=1;x%=md;while(y){if(y&1)ret=ret*x%md;x=x*x%md,y>>=1;}return ret;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int K = 26;
int cv[K][K][K][K];
int n,m;
ll xx[MAX],yy[MAX],x[MAX],y[MAX],hs[MAX],key = 724729;
map<ll,vii> z;
map<int,int> g;
string s,t;
ari(3) solve(){
rep(i,1,n)x[i] = (x[i-1]*key + s[i-1] + 69) % MOD;
rep(i,1,m)y[i] = (y[i-1]*key + t[i-1] + 69) % MOD;
rep(i,4,n)xx[i] = xx[i-1] + cv[s[i-4]][s[i-3]][s[i-2]][s[i-1]];
rep(i,4,m)yy[i] = yy[i-1] + cv[t[i-4]][t[i-3]][t[i-2]][t[i-1]];
int le,ri,mid,res,val;
bool st;
int a,b;
le = 1, ri = min(n,m);
while(le<=ri){
mid = le+ri>>1;
g.clear();
rep(i,mid,n)g[(x[i]+x[i-mid]*hs[mid])%MOD] = i;
st = 0;
rep(i,mid,m){
val = (y[i]+y[i-mid]*hs[mid])%MOD;
if(g.count(val)){
a = g[val]-mid, b = i-mid;
st = 1;
break;
}
}
if(st)res = mid, le = mid+1;
else ri = mid-1;
}
if(res==1)return {res, a, b};
int l1, l2;
l1 = res+1, l2 = min({n,m,res*2});
ll v;
ll vv;
vii::iterator it;
rap(k,l2,l1){
z.clear();
if(k==3){
rep(i,k,n){
v = (ll)cv[s[i-3]][s[i-2]][s[i-1]][0]+
cv[s[i-2]][s[i-1]][s[i-3]][0]+
cv[s[i-1]][s[i-3]][s[i-2]][0];
z[v].pb({(x[i]+x[i-k]*hs[k])%MOD, i});
}
rep(i,k,m){
v = (ll)cv[t[i-3]][t[i-2]][t[i-1]][0]+
cv[t[i-2]][t[i-1]][t[i-3]][0]+
cv[t[i-1]][t[i-3]][t[i-2]][0];
if(z.count(v))return {k, z[v][0].se-k, i-k};
}
}
else {
rep(i,k,n){
v = xx[i]-xx[i-k+3]+cv[s[i-3]][s[i-2]][s[i-1]][s[i-k]]+
cv[s[i-2]][s[i-1]][s[i-k]][s[i-k+1]]+
cv[s[i-1]][s[i-k]][s[i-k+1]][s[i-k+2]];
z[v].pb({(x[i]+x[i-k]*hs[k])%MOD, i});
}
for(auto &i:z)sort(all(i.se));
rep(i,k,m){
v = yy[i]-yy[i-k+3]+cv[t[i-3]][t[i-2]][t[i-1]][t[i-k]]+
cv[t[i-2]][t[i-1]][t[i-k]][t[i-k+1]]+
cv[t[i-1]][t[i-k]][t[i-k+1]][t[i-k+2]];
if(z.count(v)){
if(k==4)return {k, z[v][0].se-k, i-k};
vii &zz = z[v];
For(o,0,k){
vv = (y[i]+y[i-k+o]*hs[k-o])%MOD;
if(o)vv = (vv*(MOD-hs[o]) + (y[i-k+o]+hs[o]*y[i-k])) % MOD;
it = lower_bound(all(zz), pii{vv, 0});
if(it!=zz.end() && (*it).fi==vv)return {k, (*it).se-k, i-k};
}
}
}
}
}
return {res, a, b};
}
int main(){
// cout<<fixed<<setprecision(10);
// freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
For(a,0,K)For(b,0,K)For(c,0,K)For(d,0,K)cv[a][b][c][d] = rng()%MOD;
cin>>s>>t;
n = s.size();
m = t.size();
for(auto &i:s)i-= 'a';
for(auto &i:t)i-= 'a';
int o = max(n,m);
hs[0] = MOD-1;
rep(i,1,o)hs[i] = hs[i-1]*key % MOD;
ari(3) ans = solve(), tmp;
reverse(all(t));
tmp = solve();
if(tmp[0]>ans[0]){
ans = tmp;
ans[2] = m-(ans[2]+ans[0]);
}
cout<<ans[0]<<endl;
cout<<ans[1]<<" "<<ans[2]<<endl;
return 0;
}
Compilation message (stderr)
necklace.cpp: In function 'std::array<int, 3> solve()':
necklace.cpp:114:39: warning: array subscript has type 'char' [-Wchar-subscripts]
114 | rep(i,4,n)xx[i] = xx[i-1] + cv[s[i-4]][s[i-3]][s[i-2]][s[i-1]];
| ^
necklace.cpp:114:47: warning: array subscript has type 'char' [-Wchar-subscripts]
114 | rep(i,4,n)xx[i] = xx[i-1] + cv[s[i-4]][s[i-3]][s[i-2]][s[i-1]];
| ^
necklace.cpp:114:55: warning: array subscript has type 'char' [-Wchar-subscripts]
114 | rep(i,4,n)xx[i] = xx[i-1] + cv[s[i-4]][s[i-3]][s[i-2]][s[i-1]];
| ^
necklace.cpp:114:63: warning: array subscript has type 'char' [-Wchar-subscripts]
114 | rep(i,4,n)xx[i] = xx[i-1] + cv[s[i-4]][s[i-3]][s[i-2]][s[i-1]];
| ^
necklace.cpp:115:39: warning: array subscript has type 'char' [-Wchar-subscripts]
115 | rep(i,4,m)yy[i] = yy[i-1] + cv[t[i-4]][t[i-3]][t[i-2]][t[i-1]];
| ^
necklace.cpp:115:47: warning: array subscript has type 'char' [-Wchar-subscripts]
115 | rep(i,4,m)yy[i] = yy[i-1] + cv[t[i-4]][t[i-3]][t[i-2]][t[i-1]];
| ^
necklace.cpp:115:55: warning: array subscript has type 'char' [-Wchar-subscripts]
115 | rep(i,4,m)yy[i] = yy[i-1] + cv[t[i-4]][t[i-3]][t[i-2]][t[i-1]];
| ^
necklace.cpp:115:63: warning: array subscript has type 'char' [-Wchar-subscripts]
115 | rep(i,4,m)yy[i] = yy[i-1] + cv[t[i-4]][t[i-3]][t[i-2]][t[i-1]];
| ^
necklace.cpp:121:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
121 | mid = le+ri>>1;
| ~~^~~
necklace.cpp:146:22: warning: array subscript has type 'char' [-Wchar-subscripts]
146 | v = (ll)cv[s[i-3]][s[i-2]][s[i-1]][0]+
| ^
necklace.cpp:146:30: warning: array subscript has type 'char' [-Wchar-subscripts]
146 | v = (ll)cv[s[i-3]][s[i-2]][s[i-1]][0]+
| ^
necklace.cpp:146:38: warning: array subscript has type 'char' [-Wchar-subscripts]
146 | v = (ll)cv[s[i-3]][s[i-2]][s[i-1]][0]+
| ^
necklace.cpp:147:15: warning: array subscript has type 'char' [-Wchar-subscripts]
147 | cv[s[i-2]][s[i-1]][s[i-3]][0]+
| ^
necklace.cpp:147:23: warning: array subscript has type 'char' [-Wchar-subscripts]
147 | cv[s[i-2]][s[i-1]][s[i-3]][0]+
| ^
necklace.cpp:147:31: warning: array subscript has type 'char' [-Wchar-subscripts]
147 | cv[s[i-2]][s[i-1]][s[i-3]][0]+
| ^
necklace.cpp:148:15: warning: array subscript has type 'char' [-Wchar-subscripts]
148 | cv[s[i-1]][s[i-3]][s[i-2]][0];
| ^
necklace.cpp:148:23: warning: array subscript has type 'char' [-Wchar-subscripts]
148 | cv[s[i-1]][s[i-3]][s[i-2]][0];
| ^
necklace.cpp:148:31: warning: array subscript has type 'char' [-Wchar-subscripts]
148 | cv[s[i-1]][s[i-3]][s[i-2]][0];
| ^
necklace.cpp:152:22: warning: array subscript has type 'char' [-Wchar-subscripts]
152 | v = (ll)cv[t[i-3]][t[i-2]][t[i-1]][0]+
| ^
necklace.cpp:152:30: warning: array subscript has type 'char' [-Wchar-subscripts]
152 | v = (ll)cv[t[i-3]][t[i-2]][t[i-1]][0]+
| ^
necklace.cpp:152:38: warning: array subscript has type 'char' [-Wchar-subscripts]
152 | v = (ll)cv[t[i-3]][t[i-2]][t[i-1]][0]+
| ^
necklace.cpp:153:15: warning: array subscript has type 'char' [-Wchar-subscripts]
153 | cv[t[i-2]][t[i-1]][t[i-3]][0]+
| ^
necklace.cpp:153:23: warning: array subscript has type 'char' [-Wchar-subscripts]
153 | cv[t[i-2]][t[i-1]][t[i-3]][0]+
| ^
necklace.cpp:153:31: warning: array subscript has type 'char' [-Wchar-subscripts]
153 | cv[t[i-2]][t[i-1]][t[i-3]][0]+
| ^
necklace.cpp:154:15: warning: array subscript has type 'char' [-Wchar-subscripts]
154 | cv[t[i-1]][t[i-3]][t[i-2]][0];
| ^
necklace.cpp:154:23: warning: array subscript has type 'char' [-Wchar-subscripts]
154 | cv[t[i-1]][t[i-3]][t[i-2]][0];
| ^
necklace.cpp:154:31: warning: array subscript has type 'char' [-Wchar-subscripts]
154 | cv[t[i-1]][t[i-3]][t[i-2]][0];
| ^
necklace.cpp:160:34: warning: array subscript has type 'char' [-Wchar-subscripts]
160 | v = xx[i]-xx[i-k+3]+cv[s[i-3]][s[i-2]][s[i-1]][s[i-k]]+
| ^
necklace.cpp:160:42: warning: array subscript has type 'char' [-Wchar-subscripts]
160 | v = xx[i]-xx[i-k+3]+cv[s[i-3]][s[i-2]][s[i-1]][s[i-k]]+
| ^
necklace.cpp:160:50: warning: array subscript has type 'char' [-Wchar-subscripts]
160 | v = xx[i]-xx[i-k+3]+cv[s[i-3]][s[i-2]][s[i-1]][s[i-k]]+
| ^
necklace.cpp:160:58: warning: array subscript has type 'char' [-Wchar-subscripts]
160 | v = xx[i]-xx[i-k+3]+cv[s[i-3]][s[i-2]][s[i-1]][s[i-k]]+
| ^
necklace.cpp:161:19: warning: array subscript has type 'char' [-Wchar-subscripts]
161 | cv[s[i-2]][s[i-1]][s[i-k]][s[i-k+1]]+
| ^
necklace.cpp:161:27: warning: array subscript has type 'char' [-Wchar-subscripts]
161 | cv[s[i-2]][s[i-1]][s[i-k]][s[i-k+1]]+
| ^
necklace.cpp:161:35: warning: array subscript has type 'char' [-Wchar-subscripts]
161 | cv[s[i-2]][s[i-1]][s[i-k]][s[i-k+1]]+
| ^
necklace.cpp:161:45: warning: array subscript has type 'char' [-Wchar-subscripts]
161 | cv[s[i-2]][s[i-1]][s[i-k]][s[i-k+1]]+
| ^
necklace.cpp:162:19: warning: array subscript has type 'char' [-Wchar-subscripts]
162 | cv[s[i-1]][s[i-k]][s[i-k+1]][s[i-k+2]];
| ^
necklace.cpp:162:27: warning: array subscript has type 'char' [-Wchar-subscripts]
162 | cv[s[i-1]][s[i-k]][s[i-k+1]][s[i-k+2]];
| ^
necklace.cpp:162:37: warning: array subscript has type 'char' [-Wchar-subscripts]
162 | cv[s[i-1]][s[i-k]][s[i-k+1]][s[i-k+2]];
| ^
necklace.cpp:162:47: warning: array subscript has type 'char' [-Wchar-subscripts]
162 | cv[s[i-1]][s[i-k]][s[i-k+1]][s[i-k+2]];
| ^
necklace.cpp:167:34: warning: array subscript has type 'char' [-Wchar-subscripts]
167 | v = yy[i]-yy[i-k+3]+cv[t[i-3]][t[i-2]][t[i-1]][t[i-k]]+
| ^
necklace.cpp:167:42: warning: array subscript has type 'char' [-Wchar-subscripts]
167 | v = yy[i]-yy[i-k+3]+cv[t[i-3]][t[i-2]][t[i-1]][t[i-k]]+
| ^
necklace.cpp:167:50: warning: array subscript has type 'char' [-Wchar-subscripts]
167 | v = yy[i]-yy[i-k+3]+cv[t[i-3]][t[i-2]][t[i-1]][t[i-k]]+
| ^
necklace.cpp:167:58: warning: array subscript has type 'char' [-Wchar-subscripts]
167 | v = yy[i]-yy[i-k+3]+cv[t[i-3]][t[i-2]][t[i-1]][t[i-k]]+
| ^
necklace.cpp:168:19: warning: array subscript has type 'char' [-Wchar-subscripts]
168 | cv[t[i-2]][t[i-1]][t[i-k]][t[i-k+1]]+
| ^
necklace.cpp:168:27: warning: array subscript has type 'char' [-Wchar-subscripts]
168 | cv[t[i-2]][t[i-1]][t[i-k]][t[i-k+1]]+
| ^
necklace.cpp:168:35: warning: array subscript has type 'char' [-Wchar-subscripts]
168 | cv[t[i-2]][t[i-1]][t[i-k]][t[i-k+1]]+
| ^
necklace.cpp:168:45: warning: array subscript has type 'char' [-Wchar-subscripts]
168 | cv[t[i-2]][t[i-1]][t[i-k]][t[i-k+1]]+
| ^
necklace.cpp:169:19: warning: array subscript has type 'char' [-Wchar-subscripts]
169 | cv[t[i-1]][t[i-k]][t[i-k+1]][t[i-k+2]];
| ^
necklace.cpp:169:27: warning: array subscript has type 'char' [-Wchar-subscripts]
169 | cv[t[i-1]][t[i-k]][t[i-k+1]][t[i-k+2]];
| ^
necklace.cpp:169:37: warning: array subscript has type 'char' [-Wchar-subscripts]
169 | cv[t[i-1]][t[i-k]][t[i-k+1]][t[i-k+2]];
| ^
necklace.cpp:169:47: warning: array subscript has type 'char' [-Wchar-subscripts]
169 | cv[t[i-1]][t[i-k]][t[i-k+1]][t[i-k+2]];
| ^
necklace.cpp:183:19: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
183 | return {res, a, b};
| ^
necklace.cpp:118:6: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | int a,b;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |