# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
693165 |
2023-02-02T12:36:41 Z |
dozer |
Bajka (COCI20_bajka) |
C++14 |
|
54 ms |
4308 KB |
#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int ,int>
#define st first
#define nd second
#define sp " "
#define endl "\n"
#define N 1005
#define M 30
const int INF = 1e9 + 7;
string s, t;
vector<int> ind[M];
int dp[N][N];
int f(int i, int j)
{
if (j == t.size()) return 0;
if (dp[i][j] != -1) return dp[i][j];
int ans = INF;
for (auto k : ind[s[i] - 'a'])
{
if (k > 0 && s[k - 1] == t[j]) ans = min(ans, f(k - 1, j + 1) + 1 + abs(i - k));
if (k < (int)s.size() - 1 && s[k + 1] == t[j]) ans = min(ans, f(k + 1, j + 1) + 1 + abs(i - k));
}
return dp[i][j] = ans;
}
int32_t main()
{
fastio();
memset(dp, -1, sizeof(dp));
int n, m;
cin>>n>>m;
cin>>s>>t;
int ans = INF;
for (int i = 0; i < n; i++)
{
ind[s[i] - 'a'].pb(i);
}
for (int i = 0; i < n; i++)
{
if (s[i] == t[0]) ans = min(ans, f(i, 1));
}
if (ans >= INF) cout<<-1<<endl;
else cout<<ans<<endl;
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
Compilation message
bajka.cpp: In function 'int f(int, int)':
bajka.cpp:21:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | if (j == t.size()) return 0;
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4308 KB |
Output is correct |
3 |
Correct |
2 ms |
4188 KB |
Output is correct |
4 |
Correct |
3 ms |
4288 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4284 KB |
Output is correct |
2 |
Correct |
2 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4308 KB |
Output is correct |
4 |
Correct |
2 ms |
4280 KB |
Output is correct |
5 |
Correct |
2 ms |
4280 KB |
Output is correct |
6 |
Correct |
2 ms |
4280 KB |
Output is correct |
7 |
Correct |
5 ms |
4280 KB |
Output is correct |
8 |
Correct |
47 ms |
4180 KB |
Output is correct |
9 |
Correct |
54 ms |
4280 KB |
Output is correct |
10 |
Correct |
2 ms |
4180 KB |
Output is correct |
11 |
Correct |
4 ms |
4308 KB |
Output is correct |