//Bismillahi Rahmanir Rahim
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<string, string> pss;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pll> vii;
typedef vector<LL> vl;
typedef vector<vl> vvl;
double EPS = 1e-9;
const int N = 1e5;
const ll inf =1000001;
double PI = acos(-1);
const int MX = 2e5 + 5;
const int MS = 30;
int dirx[4] = { 1,0,-1,0 };
int diry[4] = { 0,1,0,-1 };
ll sx,sy,fx,x,fy,Z,m,n,k,y,l,r,ans,f[1000001],b[inf],a[inf],res,cur,ans1,flag,flag1,flag2,flag3,flag4,cnt,cnt1,cnt2,cnt3,minn,maxx,maxx1,sum,sum1,ans2,ans3;
vector<ll> adj[200000];
vi z,z1;
char c[505][505];
ll visited[500001],color[500001];
vector<ll> dist;
ll mat[1001][1001];
string str,str1,str2,str3;
const int md = 998244353;
vector<vector<pair<int,int>>> g;
vector<pair<ll,ll>>v;
vl v2;
priority_queue<pair<pair<long long, int>, bool> > pq;
map<ll,ll>mp;
map<char,ll>mp1;
queue<ll> q;
set<ll>s;
multiset<LL> ms;
stack<LL>st;
double pi=3.14159265358979323846;
bool ok[505][505];
vector<ll> v1[500001];
bool prime[200001];
vector<char> path;
ll is[N+1];
ll dp[303][303];
ll en[200001],ex[200001],tour[200001],depth[200001],segtree[inf];
ll up[MS][MX];
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b)-1; (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a) = (b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a) = (b); (a) <= (c); ++(a))
#define FOREACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a,b) a=max(a,b)
#define MIN(a, b) a = min(a, b)
#define SQR(x) ((LL)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define TC(t) while (t--)
#define ins insert
#define PERMUTE next_permutation
/*********/
void solve()
{
string s,t;
cin >> n >> m;
cin >> s >> t;
s = "$" + s;
t = "$" + t;
for (ll i = 0; i <= n; ++i)
{
for (ll j = 0; j <= m; ++j)
{
dp[i][j] = INT_MAX;
}
}
for (ll i = 1; i <= n; ++i)
{
if (s[i] == t[1])
{
dp[i][1] = 0;
}
}
for (ll j = 1; j <= m; ++j)
{
for (ll i = 1; i <= n; ++i)
{
if (s[i] == t[j])
{
if (i > 1)
{
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + 1);
}
if (i < n)
{
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + 1);
}
}
}
for (ll i = 1; i <= n; ++i)
{
for (ll k = 1; k <= n; ++k)
{
if (s[i] == s[k])
{
dp[i][j] = min(dp[i][j], dp[k][j] + abs(i - k));
}
}
}
}
ll ans = INT_MAX;
for (ll i = 1; i <= n; ++i)
{
ans = min(ans, dp[i][m]);
}
if (ans == INT_MAX) ans = -1;
cout << ans << endl;
}
/*********/
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
int i =1;
//cin >> t;
//freopen("snakes.in", "r", stdin);
//freopen("snakes.out", "w", stdout);
TC(t)
{
solve();
}
return 0;
}
Compilation message
bajka.cpp: In function 'int main()':
bajka.cpp:144:9: warning: unused variable 'i' [-Wunused-variable]
144 | int i =1;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16804 KB |
Output is correct |
2 |
Correct |
10 ms |
16800 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
10 ms |
16748 KB |
Output is correct |
5 |
Correct |
8 ms |
16852 KB |
Output is correct |
6 |
Correct |
9 ms |
16800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
17508 KB |
Output is correct |
2 |
Correct |
62 ms |
17508 KB |
Output is correct |
3 |
Correct |
40 ms |
17492 KB |
Output is correct |
4 |
Correct |
46 ms |
17492 KB |
Output is correct |
5 |
Correct |
11 ms |
17492 KB |
Output is correct |
6 |
Correct |
16 ms |
17436 KB |
Output is correct |
7 |
Correct |
63 ms |
17492 KB |
Output is correct |
8 |
Correct |
60 ms |
17492 KB |
Output is correct |
9 |
Correct |
103 ms |
17504 KB |
Output is correct |
10 |
Correct |
12 ms |
17444 KB |
Output is correct |
11 |
Correct |
64 ms |
17520 KB |
Output is correct |