#include <bits/stdc++.h>
#define loop(i, a, b) for(long long i=a;i<b;i++)
#define pool(i, a, b) for(long long i=a-1;i>=b;i--)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define sc scanf
#define vc vector
#define pa pair<ll, ll>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 310
#define pa pair<ll, ll>
#define ld long double
ll dp[mn][mn];
int main() {
ll n, m;cin >> n >> m;
string a, b;cin >> a >> b;
loop(i, 1, m+1){
loop(j, 1, n+1) dp[i][j]=llmax;
loop(j, 1, n+1)if(a.at(j-1)==b.at(i-1)){
if(j-1>=1)dp[i][j]=min(dp[i][j], dp[i-1][j-1]+1);
if(j+1<=n)dp[i][j]=min(dp[i][j], dp[i-1][j+1]+1);
}
loop(j, 1, n+1)if(a.at(j-1)==b.at(i-1)){
loop(k, 1, n+1) if(k!=j and a.at(k-1)==a.at(j-1)) dp[i][j]=min(dp[i][j], dp[i][k]+abs(k-j));
pool(k, n+1, 1) if(k!=j and a.at(k-1)==a.at(j-1)) dp[i][j]=min(dp[i][j], dp[i][k]+abs(k-j));
}
}
ll ans=llmax;
loop(j, 1, n+1) ans=min(ans, dp[m][j]);
cout << ((ans>=llmax/2) ? -1 : ans-1)<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1020 KB |
Output is correct |
2 |
Correct |
14 ms |
1024 KB |
Output is correct |
3 |
Correct |
10 ms |
1004 KB |
Output is correct |
4 |
Correct |
10 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
44 ms |
1124 KB |
Output is correct |
8 |
Correct |
75 ms |
1080 KB |
Output is correct |
9 |
Correct |
109 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
29 ms |
1004 KB |
Output is correct |