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>
using namespace std;
#define IOS cin.tie(nullptr)->sync_with_stdio(0),cin.exceptions(cin.failbit);
#define lb(x) (x)&-(x)
#define all(x) (x).begin(),(x).end()
#define ll long long
#define u64 unsigned long long
constexpr int maxN=2e5+5;
constexpr u64 C = 998244353;
string s,t;
int a,b;
inline bool chk(int guess){
map<u64,int> hashs;
u64 hsh=0,x=1;
for(int i = 0;i<guess-1;i++)x*=C;
for(int i = 0;i<guess-1;i++)hsh*=C,hsh+=t[i];
for(int i = guess-1;i<t.size();i++){
hsh*=C,hsh+=t[i];
hashs[hsh]=i-guess+1;
hsh-=x*t[i-guess+1];
}
for(int i = 0;i+guess<=s.size();i++){
u64 fh=0,bh=0;
for(int j = 0;j<guess;j++)bh*=C,bh+=s[j+i];
for(int j = 0;j<guess;j++){
fh*=C;
fh+=s[j+i];
bh-=x*s[j+i];
bh*=C;
if(hashs.count(fh+bh)){
a=i,b=hashs[fh+bh];
return 1;
}
}
u64 fh2=0,bh2=0;
for(int j = guess;j--;)fh2*=C,fh2+=s[j+i];
for(int j = guess;j--;){
bh2*=C;
bh2+=s[j+i];
fh2-=x*s[j+i];
fh2*=C;
if(hashs.count(fh2+bh2)){
a=i,b=hashs[fh2+bh2];
return 1;
}
}
}
return 0;
}
int main(){
IOS
cin>>s>>t;
int l = 0,r = min(s.size(),t.size()),mid;
for(int i = r;i>=l;i--){
if(chk(i)){
return cout<<i<<'\n'<<a<<' '<<b<<'\n',0;
}
}
}
Compilation message (stderr)
necklace.cpp: In function 'bool chk(int)':
necklace.cpp:22:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = guess-1;i<t.size();i++){
| ~^~~~~~~~~
necklace.cpp:27:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for(int i = 0;i+guess<=s.size();i++){
| ~~~~~~~^~~~~~~~~~
necklace.cpp: In function 'int main()':
necklace.cpp:59:42: warning: unused variable 'mid' [-Wunused-variable]
59 | int l = 0,r = min(s.size(),t.size()),mid;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |