Submission #476299

#TimeUsernameProblemLanguageResultExecution timeMemory
476299Mackerel_PikeBajka (COCI20_bajka)C++14
70 / 70
2 ms588 KiB
#include <bits/stdc++.h> using namespace std; const int maxn = 305; int n, m; int f[maxn][maxn], lst[maxn], pre[maxn], nxt[maxn]; char s[maxn], t[maxn]; int main(){ //freopen("bajka.in", "r", stdin); scanf("%d%d", &n, &m); scanf("%s", s); scanf("%s", t); memset(lst, -1, sizeof(lst)); for(int i = 0; i < n; ++i) pre[i] = lst[s[i]], lst[s[i]] = i; memset(lst, -1, sizeof(lst)); for(int i = n - 1; i >= 0; --i) nxt[i] = lst[s[i]], lst[s[i]] = i; memset(f, 0x3f, sizeof(f)); for(int i = 0; i < n; ++i) f[(s[i] == t[0])][i] = 0; for(int i = 0; i < m; ++i){ for(int j = 0; j < n; ++j) if(~pre[j]) f[i][j] = min(f[i][j], f[i][pre[j]] + j - pre[j]); for(int j = n - 1; j >= 0; --j) if(~nxt[j]) f[i][j] = min(f[i][j], f[i][nxt[j]] + nxt[j] - j); for(int j = 0; j < n; ++j){ if(j && s[j - 1] == t[i]) f[i + 1][j - 1] = min(f[i + 1][j - 1], f[i][j] + 1); if(j < n && s[j + 1] == t[i]) f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + 1); } } int ans = 0x3f3f3f3f; for(int j = 0; j < n; ++j) ans = min(ans, f[m][j]); printf("%d\n", ans == 0x3f3f3f3f ? -1 : ans); return 0; }

Compilation message (stderr)

bajka.cpp: In function 'int main()':
bajka.cpp:20:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   20 |     pre[i] = lst[s[i]], lst[s[i]] = i;
      |                  ~~~^
bajka.cpp:20:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   20 |     pre[i] = lst[s[i]], lst[s[i]] = i;
      |                             ~~~^
bajka.cpp:23:21: warning: array subscript has type 'char' [-Wchar-subscripts]
   23 |     nxt[i] = lst[s[i]], lst[s[i]] = i;
      |                  ~~~^
bajka.cpp:23:32: warning: array subscript has type 'char' [-Wchar-subscripts]
   23 |     nxt[i] = lst[s[i]], lst[s[i]] = i;
      |                             ~~~^
bajka.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 |   scanf("%d%d", &n, &m);
      |   ~~~~~^~~~~~~~~~~~~~~~
bajka.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~
bajka.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   scanf("%s", t);
      |   ~~~~~^~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...