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;
typedef long long ll;
const ll MOD=3499999991;
int n,m,ans,l,r;
ll hsh[2][3050],prec[3050];
string a,b;
map<ll,int> hv;
void computehash(int id,string str){
for (int i=1;i<str.length();i++){
hsh[id][i]=hsh[id][i-1]*27+str[i]-'a'+1;
hsh[id][i]%=MOD;
}
}
ll gethash(int id,int l,int r){
//(]
return ((hsh[id][r]-hsh[id][l]*prec[r-l])%MOD+MOD)%MOD;
}
int main(){
cin>>a>>b;
a=' '+a;
b=' '+b;
computehash(0,a);
computehash(1,b);
prec[0]=1;
for (int i=1;i<=3000;i++) prec[i]=prec[i-1]*27%MOD;
for (int i=1;i<a.length();i++){
for (int j=i;j<a.length();j++){
for (int k=0;k<i;k++){
//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(0,i-1,j)*prec[i-k-1]+gethash(0,k,i-1))%MOD<<"\n";
hv[(gethash(0,i-1,j)*prec[i-k-1]+gethash(0,k,i-1))%MOD]=k+1;
}
}
}
for (int i=1;i<b.length();i++){
for (int j=i;j<b.length();j++){
for (int k=0;k<i;k++){
//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD<<"\n";
if (!hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]) continue;
if (j-k<ans) continue;
ans=j-k;
l=hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]-1;
r=k;
}
}
}
reverse(b.begin()+1,b.end());
computehash(1,b);
for (int i=1;i<b.length();i++){
for (int j=i;j<b.length();j++){
for (int k=0;k<i;k++){
//cout<<i<<"-"<<j<<"--"<<k<<"::"<<(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD<<"\n";
if (!hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]) continue;
if (j-k<ans) continue;
ans=j-k;
l=hv[(gethash(1,i-1,j)*prec[i-k-1]+gethash(1,k,i-1))%MOD]-1;
r=b.length()-1-j;
}
}
}
cout<<ans<<"\n"<<l<<" "<<r<<"\n";
}
Compilation message (stderr)
necklace.cpp: In function 'void computehash(int, std::string)':
necklace.cpp:10:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
10 | for (int i=1;i<str.length();i++){
| ~^~~~~~~~~~~~~
necklace.cpp: In function 'int main()':
necklace.cpp:27:16: 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=1;i<a.length();i++){
| ~^~~~~~~~~~~
necklace.cpp:28:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for (int j=i;j<a.length();j++){
| ~^~~~~~~~~~~
necklace.cpp:35:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for (int i=1;i<b.length();i++){
| ~^~~~~~~~~~~
necklace.cpp:36:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int j=i;j<b.length();j++){
| ~^~~~~~~~~~~
necklace.cpp:49:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for (int i=1;i<b.length();i++){
| ~^~~~~~~~~~~
necklace.cpp:50:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int j=i;j<b.length();j++){
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |