Submission #331397

# Submission time Handle Problem Language Result Execution time Memory
331397 2020-11-28T10:26:57 Z neki Bajka (COCI20_bajka) C++14
70 / 70
110 ms 1132 KB
#include <bits/stdc++.h>
#define loop(i, a, b) for(long long i=a;i<b;i++)
#define pool(i, a, b) for(long long i=a-1;i>=b;i--)
#define fore(i, a) for(auto&& i:a)
#define fi first
#define se second
#define ps(a) push_back(a)
#define pb(a) pop_back(a)
#define sc scanf
#define vc vector
#define pa pair<ll, ll>
#define ll long long
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define llmax LLONG_MAX/2
#define llmin -LLONG_MAX/2
using namespace std;
#define mn 310
#define pa pair<ll, ll>
#define ld long double 
ll dp[mn][mn];
int main() {
    ll n, m;cin >> n >> m;
    string a, b;cin >> a >> b;
    loop(i, 1, m+1){
        loop(j, 1, n+1) dp[i][j]=llmax;
        loop(j, 1, n+1)if(a.at(j-1)==b.at(i-1)){
            if(i==1) dp[i][j]=min(dp[i][j], (ll)0);
            if(j-1>=1)dp[i][j]=min(dp[i][j], dp[i-1][j-1]+1);
            if(j+1<=n)dp[i][j]=min(dp[i][j], dp[i-1][j+1]+1);
        }
        loop(j, 1, n+1)if(a.at(j-1)==b.at(i-1)){
            loop(k, 1, n+1) if(k!=j and a.at(k-1)==a.at(j-1)) dp[i][j]=min(dp[i][j], dp[i][k]+abs(k-j));
            pool(k, n+1, 1) if(k!=j and a.at(k-1)==a.at(j-1)) dp[i][j]=min(dp[i][j], dp[i][k]+abs(k-j));
        }
    }
    ll ans=llmax;
    loop(j, 1, n+1) ans=min(ans, dp[m][j]);
    cout << ((ans>=llmax/2) ? -1 : ans)<<endl;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 1004 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 1004 KB Output is correct
5 Correct 1 ms 1004 KB Output is correct
6 Correct 1 ms 1004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 1004 KB Output is correct
2 Correct 14 ms 1004 KB Output is correct
3 Correct 10 ms 1132 KB Output is correct
4 Correct 11 ms 1004 KB Output is correct
5 Correct 1 ms 364 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 44 ms 1004 KB Output is correct
8 Correct 67 ms 1004 KB Output is correct
9 Correct 110 ms 1004 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 30 ms 1004 KB Output is correct