답안 #881548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
881548 2023-12-01T12:45:58 Z HossamHero7 Bajka (COCI20_bajka) C++14
70 / 70
34 ms 1232 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
string s,t;
int n,m;
vector<vector<int>> idx(26);
int dp[305][305][2];
int solve(int i,int j,bool b){
    if(j == m) return 0;
    int &ret = dp[i][j][b];
    if(~ret) return ret;
    int c1 = i && s[i-1] == t[j]? solve(i-1,j+1,0) + 1 : 1e9;
    int c2 = i < n - 1 && s[i+1] == t[j] ? solve(i+1,j+1,0) + 1 : 1e9;
    int c3 = 1e9;
    if(!b){
        for(auto k : idx[s[i]-'a']) c3 = min(c3 , solve(k,j,1) + abs(i - k));
    }
    return ret = min({c1,c2,c3});
}
void solve(){
    cin>>n>>m;
    cin>>s>>t;
    for(int i=0;i<n;i++){
        idx[s[i]-'a'].push_back(i);
    }
    memset(dp,-1,sizeof(dp));
    int ans = 1e9;
    for(auto i : idx[t[0]-'a']) ans = min(ans , solve(i,1,0));
    cout<<(ans == 1e9 ? -1 : ans)<<endl;
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);      cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1112 KB Output is correct
2 Correct 1 ms 1112 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1116 KB Output is correct
5 Correct 1 ms 1116 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1116 KB Output is correct
2 Correct 2 ms 1116 KB Output is correct
3 Correct 1 ms 1116 KB Output is correct
4 Correct 1 ms 1232 KB Output is correct
5 Correct 1 ms 976 KB Output is correct
6 Correct 1 ms 1156 KB Output is correct
7 Correct 4 ms 1112 KB Output is correct
8 Correct 24 ms 1116 KB Output is correct
9 Correct 34 ms 1116 KB Output is correct
10 Correct 1 ms 952 KB Output is correct
11 Correct 3 ms 1112 KB Output is correct