#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>=(a);i--)
using ll=long long;
using vi=vector<int>;
//pi[i] = longitud del prefijo propio mas grande de s[0..i] que tambien es sufijo
vi prefix_function(string s) {
vi pi(s.size());
for(int i=1,j=0; i<s.size(); i++) {
while(j && s[i]!=s[j]) j=pi[j-1];
if(s[i]==s[j]) j++;
pi[i] = j;
}
return pi;
}
// Busca las coincidencias de p en s, regresa indices de inicio
vi kmp(string s, string p) {
vi pi = prefix_function(p);
vi ans(s.size());
for(int i=0,j=0; i<s.size(); i++) {
while(j && s[i]!=p[j]) j=pi[j-1];
if(s[i]==p[j]) j++;
ans[i]=j;
if(j==p.size()) {
j=pi[j-1];
}
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0);
string s,t;
cin>>s>>t;
int n=s.size(),m=t.size(),ans=0,l=0,r=0;
string s2=s,t2=t;
reverse(s2.begin(),s2.end());
reverse(t2.begin(),t2.end());
rep(i,1,n) {
vi lps=kmp(t,s.substr(i)),lsp=kmp(t2,s2.substr(n-i));
rep(j,0,m-1) {
int a=lps[j],b=lsp[m-j-2];
if(ans<a+b) ans=a+b,l=i-b,r=j-a+1;
}
lps=kmp(t,s2.substr(i)),lsp=kmp(t2,s.substr(n-i));
rep(j,0,m-1) {
int a=lps[j],b=lsp[m-j-2];
if(ans<a+b) ans=a+b,l=n-i-a,r=j-a+1;
}
}
cout<<ans<<"\n";
cout<<l<<" "<<r<<"\n";
}
Compilation message
necklace.cpp: In function 'vi prefix_function(std::string)':
necklace.cpp:10:23: 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,j=0; i<s.size(); i++) {
| ~^~~~~~~~~
necklace.cpp: In function 'vi kmp(std::string, std::string)':
necklace.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0,j=0; i<s.size(); i++) {
| ~^~~~~~~~~
necklace.cpp:25:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if(j==p.size()) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
6 ms |
364 KB |
Output is correct |
7 |
Correct |
4 ms |
364 KB |
Output is correct |
8 |
Correct |
5 ms |
364 KB |
Output is correct |
9 |
Correct |
4 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
4 ms |
364 KB |
Output is correct |
12 |
Correct |
4 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
6 ms |
364 KB |
Output is correct |
7 |
Correct |
4 ms |
364 KB |
Output is correct |
8 |
Correct |
5 ms |
364 KB |
Output is correct |
9 |
Correct |
4 ms |
364 KB |
Output is correct |
10 |
Correct |
4 ms |
364 KB |
Output is correct |
11 |
Correct |
4 ms |
364 KB |
Output is correct |
12 |
Correct |
4 ms |
364 KB |
Output is correct |
13 |
Correct |
224 ms |
620 KB |
Output is correct |
14 |
Correct |
187 ms |
492 KB |
Output is correct |
15 |
Correct |
235 ms |
492 KB |
Output is correct |
16 |
Correct |
194 ms |
492 KB |
Output is correct |
17 |
Correct |
163 ms |
492 KB |
Output is correct |
18 |
Correct |
176 ms |
492 KB |
Output is correct |
19 |
Correct |
175 ms |
492 KB |
Output is correct |
20 |
Correct |
206 ms |
768 KB |
Output is correct |
21 |
Correct |
203 ms |
548 KB |
Output is correct |