This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define NUM 301
#define inf 1e9
#define MOD 1000000007
using namespace std;
bool pos;
vector <int> cpos[26];
int n, m;
ll res = inf;
string s1, s2;
void process (ll cposi, int idx2, ll cnt) {
if (cnt > res) return;
//cout << idx2 <<':'<< cpos[s2[idx2]-'A'][cposi] << endl;
if (idx2 == m - 1) {
//cout << cnt << endl << endl;
res = min(res, cnt);
return;
}
if (cpos[s2[idx2]-'A'][cposi] > 0) {
if(s1[cpos[s2[idx2]-'A'][cposi] - 1] == s2[idx2 + 1]) {
//cout << '+';
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi] - 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + 1);
}
}
if (cpos[s2[idx2]-'A'][cposi] < n - 1) {
if(s1[cpos[s2[idx2]-'A'][cposi] + 1] == s2[idx2 + 1]) {
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi] + 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + 1);
}
}
int sz = cpos[s2[idx2]-'A'].size ();
for (int i = 1; i < max(cposi + 1, sz - cposi); i ++) {
if (cposi - i >= 0) {
if (cpos[s2[idx2]-'A'][cposi - i] > 0) {
if(s1[cpos[s2[idx2]-'A'][cposi - i] - 1] == s2[idx2 + 1]) {
//cout << '-';
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi - i] - 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + abs(cpos[s2[idx2]-'A'][cposi - i] - cpos[s2[idx2]-'A'][cposi]) + 1);
}
}
if (cpos[s2[idx2]-'A'][cposi - i] < n - 1) {
if(s1[cpos[s2[idx2]-'A'][cposi - i] + 1] == s2[idx2 + 1]) {
//cout << '-';
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi - i] + 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + abs(cpos[s2[idx2]-'A'][cposi - i] - cpos[s2[idx2]-'A'][cposi]) + 1);
}
}
}
if (cposi + i < sz) {
if (cpos[s2[idx2]-'A'][cposi + i] > 0) {
if(s1[cpos[s2[idx2]-'A'][cposi + i] - 1] == s2[idx2 + 1]) {
//cout << '-';
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi + i] - 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + abs(cpos[s2[idx2]-'A'][cposi + i] - cpos[s2[idx2]-'A'][cposi]) + 1);
}
}
if (cpos[s2[idx2]-'A'][cposi + i] < n - 1) {
if(s1[cpos[s2[idx2]-'A'][cposi + i] + 1] == s2[idx2 + 1]) {
//cout << '-';
process(find(cpos[s2[idx2 + 1]-'A'].begin(), cpos[s2[idx2 + 1]-'A'].end(), cpos[s2[idx2]-'A'][cposi + i] + 1)-cpos[s2[idx2 + 1]-'A'].begin(), idx2 + 1, cnt + abs(cpos[s2[idx2]-'A'][cposi + i] - cpos[s2[idx2]-'A'][cposi]) + 1);
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
cin >> s1 >> s2;
for (int i = 0; i < n; i ++) {
cpos [s1[i]-'A'].push_back(i);
}
int sz = cpos[s2[0]-'A'].size ();
for (int i = 0; i < sz; i ++) {
process (i, 0, 0);
}
cout << endl;
if (res != inf) {
cout << res;
}
else {
cout << -1;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |