Submission #881548

#TimeUsernameProblemLanguageResultExecution timeMemory
881548HossamHero7Bajka (COCI20_bajka)C++14
70 / 70
34 ms1232 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl '\n' string s,t; int n,m; vector<vector<int>> idx(26); int dp[305][305][2]; int solve(int i,int j,bool b){ if(j == m) return 0; int &ret = dp[i][j][b]; if(~ret) return ret; int c1 = i && s[i-1] == t[j]? solve(i-1,j+1,0) + 1 : 1e9; int c2 = i < n - 1 && s[i+1] == t[j] ? solve(i+1,j+1,0) + 1 : 1e9; int c3 = 1e9; if(!b){ for(auto k : idx[s[i]-'a']) c3 = min(c3 , solve(k,j,1) + abs(i - k)); } return ret = min({c1,c2,c3}); } void solve(){ cin>>n>>m; cin>>s>>t; for(int i=0;i<n;i++){ idx[s[i]-'a'].push_back(i); } memset(dp,-1,sizeof(dp)); int ans = 1e9; for(auto i : idx[t[0]-'a']) ans = min(ans , solve(i,1,0)); cout<<(ans == 1e9 ? -1 : ans)<<endl; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t=1; //cin>>t; while(t--){ solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...