# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
477998 |
2021-10-05T02:36:01 Z |
manh2004 |
Bajka (COCI20_bajka) |
C++14 |
|
1000 ms |
1348 KB |
/// HDM
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pb push_back
#define ld long double
#define time cout << "\nTime: " << clock()/(double) 1000 << "s "
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define inp freopen(duckman".inp", "r", stdin);
#define out freopen(duckman".out", "w", stdout);
const ll oo = 4557430888798830399;
#define duckman "Bajka"
const ll nmax = 1e6 + 5, mod = 1000000007;
using namespace std;
int n, m;
string a, b;
vector<int> idx[500];
void Read() {
cin >> n >> m;
cin >> a >> b;
for (int i = 0; i < n; ++i) {
idx[a[i]].pb(i);
}
}
int ans = INT_MAX;
int dp[500][500];
void Try(int cnt, int i, int cost) {
if (dp[cnt][i] <= cost) {
return;
}
dp[cnt][i] = cost;
if (cnt >= m) {
ans = min(ans, cost);
return;
}
// if (i - 1 >= 0) {
// if (a[i - 1] == b[cnt]) {
// Try(cnt + 1, i - 1, cost + 1);
// }
// }
// if (i + 1 < n) {
// if (a[i + 1] == b[cnt]) {
// Try(cnt + 1, i + 1, cost + 1);
// }
// }
// for (auto& j : idx[a[i]]) {
// if (j != i) {
// Try(cnt, j, cost + abs(j - i));
// }
// }
for (int j = 0; j < n; ++j) {
if (a[j] == a[i]) {
if (j - 1 >= 0) {
if (a[j - 1] == b[cnt]) {
Try(cnt + 1, j - 1, cost + abs(j - i) + 1);
}
}
if (j + 1 < n) {
if (a[j + 1] == b[cnt]) {
Try(cnt + 1, j + 1, cost + abs(j - i) + 1);
}
}
}
}
}
void Solve() {
memset(dp, 0x3f, sizeof(dp));
for (auto& i : idx[b[0]]) {
Try(1, i, 0);
}
cout << (ans == INT_MAX ? -1 : ans);
}
int main() {
fast;
Read();
Solve();
// time;
}
/*
*/
Compilation message
bajka.cpp: In function 'void Read()':
bajka.cpp:26:17: warning: array subscript has type 'char' [-Wchar-subscripts]
26 | idx[a[i]].pb(i);
| ^
bajka.cpp: In function 'void Solve()':
bajka.cpp:75:28: warning: array subscript has type 'char' [-Wchar-subscripts]
75 | for (auto& i : idx[b[0]]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1348 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
1228 KB |
Output is correct |
2 |
Correct |
504 ms |
1304 KB |
Output is correct |
3 |
Correct |
333 ms |
1308 KB |
Output is correct |
4 |
Correct |
493 ms |
1300 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Execution timed out |
1080 ms |
1228 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |