Submission #367374

# Submission time Handle Problem Language Result Execution time Memory
367374 2021-02-17T02:59:39 Z GusMG Necklace (Subtask 4) (BOI19_necklace4) C++17
15 / 15
249 ms 748 KB
#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()) {
      |            ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 234 ms 620 KB Output is correct
2 Correct 210 ms 748 KB Output is correct
3 Correct 249 ms 620 KB Output is correct
4 Correct 173 ms 620 KB Output is correct
5 Correct 147 ms 748 KB Output is correct
6 Correct 162 ms 492 KB Output is correct
7 Correct 180 ms 620 KB Output is correct
8 Correct 195 ms 620 KB Output is correct
9 Correct 201 ms 492 KB Output is correct