#include <stdio.h>
#define N 300
#define M 300
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int main() {
static char aa[N + 1], bb[M + 1];
static int dp[N], dq[N];
int n, m, i, j, ans;
scanf("%d%d%s%s", &n, &m, aa, bb);
for (j = 0; j < m; j++) {
if (j > 0) {
int p;
for (i = 0, p = -1; i < n; i++)
if (aa[i] == bb[j - 1]) {
if (p != -1)
dp[i] = min(dp[i], dp[p] + i - p);
p = i;
}
for (i = n - 1, p = -1; i >= 0; i--)
if (aa[i] == bb[j - 1]) {
if (p != -1)
dp[i] = min(dp[i], dp[p] + p - i);
p = i;
}
for (i = 0; i < n; i++) {
dq[i] = min(i == 0 ? INF : dp[i - 1], i + 1 == n ? INF : dp[i + 1]);
if (dq[i] != INF)
dq[i]++;
}
}
for (i = 0; i < n; i++)
dp[i] = aa[i] == bb[j] ? dq[i] : INF;
}
ans = INF;
for (i = 0; i < n; i++)
ans = min(ans, dp[i]);
if (ans == INF)
ans = -1;
printf("%d\n", ans);
return 0;
}
Compilation message
bajka.c: In function 'main':
bajka.c:14:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d%s%s", &n, &m, aa, bb);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
280 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
276 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |