-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathshortest_path_dial.yuki1695.test.cpp
38 lines (35 loc) · 1.11 KB
/
shortest_path_dial.yuki1695.test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define PROBLEM "https://yukicoder.me/problems/no/1695"
#include "../../string/manacher.hpp"
#include "../shortest_path.hpp"
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
constexpr int INF = 1 << 28;
int solve(const string &S, const string &T) {
int nmatch = 0;
while (nmatch < min<int>(S.size(), T.size()) and S[nmatch] == T[nmatch]) nmatch++;
if (!nmatch) return INF;
if (T.size() % 2) return INF;
auto trev = T;
if (trev != T) return INF;
shortest_path<int> graph(T.size() + 1);
for (int i = 0; i < int(T.size()); ++i) graph.add_edge(i, i + 1, 0);
auto ps = enumerate_palindromes(T);
for (const auto &p : ps) {
auto l = p.first, r = p.second;
if ((l + r) % 2 == 0) graph.add_edge(r, (l + r) / 2, 1);
}
graph.dial(T.size(), nmatch);
return graph.dist[nmatch];
}
int main() {
cin.tie(nullptr), ios::sync_with_stdio(false);
int N, M;
string S, T;
cin >> N >> M >> S >> T;
int ret = solve(S, T);
reverse(S.begin(), S.end());
ret = min(ret, solve(S, T));
cout << (ret < INF ? ret : -1) << '\n';
}