#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fs first
#define sc second
#define tlll tuple<ll,ll,ll>
const int mxn = 330;
int N,M;
int dist[mxn][mxn];
int dp[mxn];
string s,t;
vector<int> pos[26];
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>N>>M;
cin>>s>>t;
for(int i = 0;i<N;i++){
pos[s[i]-'a'].push_back(i);
}
for(auto &i:dist)for(auto &j:i)j = 1e9;
for(auto &i:pos){
for(int j = 1;j<i.size();j++){
dist[i[j]][i[j-1]] = i[j]-i[j-1];
dist[i[j-1]][i[j]] = i[j]-i[j-1];
}
}
for(int i = 0;i<N;i++)dist[i][i] = 0;
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
for(int k = 0;k<N;k++){
dist[j][k] = min(dist[j][k],dist[j][i]+dist[i][k]);
}
}
}
int dp2[mxn];
for(auto &tar:t){
fill(dp2,dp2+mxn,(int)1e9);
for(auto &j:pos[tar-'a']){
for(int k = 0;k<N;k++){
if(j)dp2[j] = min(dp2[j],dp[k]+dist[k][j-1]+1);
if(j+1<N)dp2[j] = min(dp2[j],dp[k]+dist[k][j+1]+1);
}
}
copy(dp2,dp2+N,dp);
}
int ans = *min_element(dp,dp+N);
if(ans>=(int)1e9)cout<<-1;
else cout<<ans-1;
}
Compilation message
bajka.cpp: In function 'int main()':
bajka.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int j = 1;j<i.size();j++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
860 KB |
Output is correct |
2 |
Correct |
28 ms |
856 KB |
Output is correct |
3 |
Correct |
28 ms |
860 KB |
Output is correct |
4 |
Correct |
28 ms |
856 KB |
Output is correct |
5 |
Correct |
24 ms |
860 KB |
Output is correct |
6 |
Correct |
24 ms |
888 KB |
Output is correct |
7 |
Correct |
36 ms |
860 KB |
Output is correct |
8 |
Correct |
55 ms |
856 KB |
Output is correct |
9 |
Correct |
55 ms |
856 KB |
Output is correct |
10 |
Correct |
24 ms |
860 KB |
Output is correct |
11 |
Correct |
34 ms |
860 KB |
Output is correct |