#include <iostream>
#include <vector>
using namespace std;
int DP(int n,int m,int i,int j,vector<vector<int>>& dp,vector<vector<int>>& word,string& s,string& t){
if(j==m-1)return 0;
if(dp[i][j]!=1e9)return dp[i][j];
int ans=1e9;
for(int x:word[s[i]-'a']){
if(x&&s[x-1]==t[j+1]){
ans=min(DP(n,m,x-1,j+1,dp,word,s,t)+1+max(x-i,i-x),ans);
}
if(x<n-1&&s[x+1]==t[j+1]){
ans=min(DP(n,m,x+1,j+1,dp,word,s,t)+1+max(x-i,i-x),ans);
}
}
if(ans==1e9)return dp[i][j]=ans+1;
return dp[i][j]=ans;
}
int main(){
int n,m;
string s,t;
cin>>n>>m>>s>>t;
vector<vector<int>> dp(n,vector<int>(m+1,1e9)),word(26,vector<int>());
for(int i=0;i<s.size();i++)word[s[i]-'a'].push_back(i);
int ans=1e9;
for(int x:word[t[0]-'a']){
ans=min(DP(n,m,x,0,dp,word,s,t),ans);
}
if(ans>=1e9)cout<<-1;
else cout<<ans;
}
Compilation message
bajka.cpp: In function 'int main()':
bajka.cpp:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<s.size();i++)word[s[i]-'a'].push_back(i);
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
700 KB |
Output is correct |
8 |
Correct |
64 ms |
684 KB |
Output is correct |
9 |
Correct |
108 ms |
684 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
5 ms |
596 KB |
Output is correct |