Submission #1186588

#TimeUsernameProblemLanguageResultExecution timeMemory
1186588kl0989eNecklace (Subtask 1-3) (BOI19_necklace1)C++20
85 / 85
535 ms106196 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define pb push_back #define vi vector<int> #define vl vector<ll> #define pi pair<int, int> #define pl pair<ll,ll> #define all(x) (x).begin(),(x).end() int main() { ios::sync_with_stdio(0); cin.tie(0); string a,b; cin >> a >> b; int n=a.size(),m=b.size(); int ans=-1,aa=-1,bb=-1; for (int l=0; l<2; l++) { int dp[n+1][m+1]; memset(dp,0,sizeof(dp)); for (int i=n-1; i>=0; i--) { for (int j=m-1; j>=0; j--) { if (a[i]==b[j]) { dp[i][j]=dp[i+1][j+1]+1; } } } int dp2[n+1][m+1]; memset(dp2,0,sizeof(dp2)); for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { dp2[i][j+dp[i][j]-1]=max(dp2[i][j+dp[i][j]-1],dp[i][j]); } for (int j=m-2; j>=0; j--) { dp2[i][j]=max(dp2[i][j],dp2[i][j+1]-1); } } int dp3[n+1][m+1]; memset(dp3,0,sizeof(dp2)); for (int j=0; j<m; j++) { for (int i=0; i<n; i++) { dp3[i+dp[i][j]-1][j]=max(dp3[i+dp[i][j]-1][j],dp[i][j]); } for (int i=n-2; i>=0; i--) { dp2[i][j]=max(dp2[i][j],dp2[i][j+1]-1); } } for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { int t=(j>0?dp2[i][j-1]:0)+(i>0?dp3[i-1][j]:0); if (t>ans) { ans=t; aa=i-(i>0?dp3[i-1][j]:0); bb=j-(j>0?dp2[i][j-1]:0); if (l) { bb=j+(i>0?dp3[i-1][j]:0)-1; bb=m-1-bb; } } } } reverse(all(b)); } cout << ans << '\n' << aa << ' ' << bb << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...