Submission #377628

#TimeUsernameProblemLanguageResultExecution timeMemory
377628NordwayBajka (COCI20_bajka)C++17
70 / 70
158 ms2048 KiB
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define x first #define y second #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define sz(v) (int)v.size() #define up_b upper_bound #define low_b lower_bound #define nl '\n' using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; const int N=3e2+11; const int M=1e6+1; const ll inf=1e9+11; const ld EPS=1e-6; const ll INF=1e18; const ll mod=/*999999001*/1e9+7; const int dx[4]={1,0,0,-1}; const int dy[4]={0,1,-1,0}; char a[N],b[N]; ll dp[N][N+N]; void solve(){ int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=1;i<=m;i++){ cin>>b[i]; } for(int i=1;i<=n;i++){ if(a[i]==b[1])dp[1][i]=0; else dp[1][i]=inf; } for(int i=2;i<=m;i++){ for(int j=1;j<=n;j++){ dp[i][j]=inf; } } for(int i=2;i<=m;i++){ for(int j=1;j<=n;j++){ if(b[i]==a[j+1]){ dp[i][j+1]=min(dp[i][j+1],dp[i-1][j]+1); } if(b[i]==a[j-1]){ dp[i][j-1]=min(dp[i][j-1],dp[i-1][j]+1); } for(int len=1;len<=n;len++){ if(a[j]==a[j+len]){ if(b[i]==a[j+len+1])dp[i][j+len+1]=min(dp[i][j+len+1],dp[i-1][j]+len+1); if(b[i]==a[j+len-1])dp[i][j+len-1]=min(dp[i][j+len-1],dp[i-1][j]+len+1); } if(j<=len)continue; if(a[j]==a[j-len]){ if(b[i]==a[j-len-1])dp[i][j-len-1]=min(dp[i][j-len-1],dp[i-1][j]+len+1); if(b[i]==a[j-len+1])dp[i][j-len+1]=min(dp[i][j-len+1],dp[i-1][j]+len+1); } } } } int ans=(*min_element(dp[m]+1,dp[m]+n+1)); if(ans==inf)ans=-1; cout<<ans; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0); int T=1; //cin>>T; for(int i=1;i<=T;i++){ solve(); cout<<nl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...