Submission #725046

#TimeUsernameProblemLanguageResultExecution timeMemory
725046Rafi22Necklace (Subtask 1-3) (BOI19_necklace1)C++14
0 / 85
1 ms340 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define st first #define nd second #define pb push_back #define sz(x) (int)(x).size() #define all(x) (x).begin(), (x).end() #define ll long long ll mod=1000000007; int inf=1000000007; ll infl=1000000000000000007; const int N=3007; int n,m; vector<int> prefix_function(string s) { int n = (int)s.length(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i-1]; while (j > 0 && s[i] != s[j]) j = pi[j-1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } vector<int>kmp(string s) { int l=sz(s); vector<int>p(l,0); for(int i=1;i<l;i++) { int j=p[i-1]; while(j>0&&s[i]!=s[j]) j=p[j-1]; if(s[i]==s[j]) j++; p[i]=j; } return p; } int ans,x,y; void solve(string a,string b,bool is) { for(int i=0;i<=n;i++) { string s="",t=""; for(int j=i;j<n;j++) s+=a[j]; for(int j=0;j<m;j++) t+=b[j]; vector<int>p1=kmp(s+'#'+t); s=""; for(int j=0;j<i;j++) s+=a[j]; reverse(all(s)); reverse(all(t)); vector<int>p2=kmp(s+'#'+t); for(int j=0;j<=m;j++) { int c=0; if(i!=n&&j>0) c+=p1[n-i+j]; if(i!=0&&j!=m-1) c+=p2[i+m-j]; if(c>ans) { ans=c; x=i-p1[n-i+j]; if(is) y=m-j-p1[n-i+j]; else y=j-p1[n-i+j]; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string a,b; cin>>a>>b; n=sz(a),m=sz(b); solve(a,b,0); reverse(all(b)); solve(a,b,1); cout<<ans<<endl<<x<<" "<<y<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...