# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
674941 |
2022-12-26T16:18:12 Z |
vjudge1 |
Bajka (COCI20_bajka) |
C++17 |
|
1000 ms |
468 KB |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define F first
#define S second
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define vpi vector<pi>
#define vb vector<bool>
#define vvb vector<vb>
#define pb push_back
#define ppb pop_back
#define read(a) for(auto &x:a) cin >> x;
#define print(a) for(auto x:a) cout << x << " "; cout << "\n";
#define vc vector<char>
#define vvc vector<vc>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define ld long double
const int INF = 1e18;
const int inf = 1e9;
void solve(){
int n, m; cin >> n >> m;
string s, t; cin >> s >> t;
vi cnt(26, 0);
for(auto &c:s) cnt[c-'a']++;
for(auto &c:t)
if(!cnt[c-'a']){
cout << "-1\n";
return;
}
vvi adj(n);
vvb neigh(n, vb(26));
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
if(j-i == 1 || s[i] == s[j]){
adj[i].pb(j); neigh[i][s[j]-'a'] = 1;
adj[j].pb(i); neigh[j][s[i]-'a'] = 1;
}
int ans = INF;
function<void(int, int, int, bool)> dfs = [&](int v, int len, int ac, bool mov){
if(mov) len++;
if(len == t.size()){
ans = min(ans, ac);
return;
}
for(auto u:adj[v]){
if(abs(u-v) == 1 && s[u] == t[len]) dfs(u, len, ac+abs(u-v), 1);
if(mov && s[u] == s[v] && neigh[u][t[len]-'a']) dfs(u, len, ac+abs(u-v), 0);
}
};
for(int i=0; i<n; i++)
if(s[i] == t[0]) dfs(i, 0, 0, 1);
cout << (ans != INF ? ans : -1) << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
int tt = 1;
// cin >> tt;
while(tt--)
solve();
return 0;
}
Compilation message
bajka.cpp: In lambda function:
bajka.cpp:52:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if(len == t.size()){
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1098 ms |
468 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |