답안 #881230

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881230 2023-11-30T22:54:44 Z Ahmed_Solyman Bajka (COCI20_bajka) C++14
70 / 70
83 ms 780 KB
/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>
 
using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("-Ofast")
#pragma GCC optimize("-O1")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
    return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
    cout<<(x?"YES":"NO")<<endl;
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #ifndef ONLINE_JUDGE
    //freopen("input.in", "r", stdin);
    //freopen("output.out", "w", stdout);
    #endif
    fast
    int n,m;cin>>n>>m;
    string a,b;cin>>a>>b;
    int dp[n][m]={};
    for(int i=0;i<n;i++){
        if(a[i]==b[0]){
            dp[i][0]=0;
        }
        else{
            dp[i][0]=1e9;
        }
        for(int j=1;j<m;j++)dp[i][j]=1e9;
    }
    for(int i=1;i<m;i++){
        for(int j=0;j<n;j++){
            if(a[j]==b[i]){
                if(j+1<n){
                    dp[j][i]=min(dp[j][i],dp[j+1][i-1]+1);
                }
                if(j-1>=0){
                    dp[j][i]=min(dp[j][i],dp[j-1][i-1]+1);
                }
            }
        }
        for(int x=0;x<n;x++){
            for(int y=0;y<n;y++){
                if(a[x]==a[y]){
                    dp[x][i]=min(dp[x][i],dp[y][i]+abs(x-y));
                    dp[y][i]=min(dp[y][i],dp[x][i]+abs(x-y));
                }
            }
        }
    }
    int ans=1e9;
    for(int i=0;i<n;i++)ans=min(ans,dp[i][m-1]);
    cout<<(ans==1e9?-1:ans)<<endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 36 ms 600 KB Output is correct
2 Correct 46 ms 600 KB Output is correct
3 Correct 33 ms 604 KB Output is correct
4 Correct 36 ms 600 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 33 ms 604 KB Output is correct
8 Correct 46 ms 604 KB Output is correct
9 Correct 83 ms 780 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 37 ms 776 KB Output is correct