# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
399787 |
2021-05-06T15:32:00 Z |
fadi57 |
Bajka (COCI20_bajka) |
C++14 |
|
100 ms |
844 KB |
#include<bits/stdc++.h>
using namespace std;
const int mx=300+10;
typedef long long ll;
int inf=1e9;
const int mod=1e9+7;
int n,m;
string a,b;
vector<int>pos[27];
int dp[mx][mx];
//int ok[i][j];
int solve(int i,int idx){
if(idx==m-1){return 0;}
int &ret=dp[i][idx];
if(ret!=-1){return ret;}
//if(ret==-2){return inf;}
ret=inf;
if(i<n-1&&a[i+1]==b[idx+1]){
ret=min(ret,solve(i+1,idx+1)+1);
}
if(i>0&&a[i-1]==b[idx+1]){
ret=min(ret,solve(i-1,idx+1)+1);
}
int me=a[i]-'a';
for(auto it:pos[me]){
if(it!=i){
ret=min(ret,solve(it,idx)+(abs(i-it)));
}
}
return ret;
}
int main(){
cin>>n>>m;
cin>>a>>b;
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++){
pos[a[i]-'a'].push_back(i);
}
if(pos[b[0]-'a'].size()==0){
cout<<-1;return 0;
}
int ans=inf;
for(auto it:pos[b[0]-'a']){
ans=min(ans, solve(it,0));
}
if(ans==inf){ans=-1;}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
7 ms |
844 KB |
Output is correct |
3 |
Correct |
3 ms |
844 KB |
Output is correct |
4 |
Correct |
3 ms |
844 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
14 ms |
764 KB |
Output is correct |
8 |
Correct |
51 ms |
716 KB |
Output is correct |
9 |
Correct |
100 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
9 ms |
688 KB |
Output is correct |