This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**Bogosort**/
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define maxn 305
#define maxm 1000006
#define maxc 1000000007
#define pii pair <int,int>
#define ii pair <int,int>
#define Task ""
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
template <typename T> inline void read(T &x){char c;bool nega=0;while((!isdigit(c=getchar()))&&(c!='-'));if(c=='-'){nega=1;c=getchar();}x=c-48;while(isdigit(c=getchar())) x=x*10+c-48;if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10);putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){putchar('-');x=-x;}writep(x);putchar(' ');}
template <typename T> inline void writeln(T x){write(x);putchar('\n');}
using namespace std;
int n, m, dp[maxn][maxn];
char fear[maxn], like[maxn];
int main() {
//srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie();
if(fopen(".inp", "r")) freopen(".inp", "r", stdin);
cin >> m >> n;
fo(i, 1, m) cin >> fear[i];
fo(j, 1, n) cin >> like[j];
// dp[i][j] i = like, j = fear jump by j
fo(j, 1, m) if(fear[j] == like[1]) dp[1][j] = 1;
fo(i, 1, n) {
fo(j, 1, m) if(dp[i][j]) {
fo(k, 1, m) if(fear[k] == fear[j]) {
if(fear[k-1] == like[i+1]) {
if(!dp[i+1][k-1] || dp[i+1][k-1] > dp[i][j] + abs(k - j) + 1)
dp[i+1][k-1] = dp[i][j] + abs(k - j) + 1;
}
if(fear[k+1] == like[i+1]) {
if(!dp[i+1][k+1] || dp[i+1][k+1] > dp[i][j] + abs(k - j) + 1)
dp[i+1][k+1] = dp[i][j] + abs(k - j) + 1;
}
}
}
}
int ans = 2e9;
fo(j, 1, m) if(dp[n][j] && dp[n][j] < ans) ans = dp[n][j];
if(ans == 2e9) return cout << -1, 0;
cout << ans - 1;
return 0;
}
Compilation message (stderr)
bajka.cpp: In function 'int main()':
bajka.cpp:29:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
29 | if(fopen(".inp", "r")) freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |