제출 #1159612

#제출 시각아이디문제언어결과실행 시간메모리
1159612ocasuBajka (COCI20_bajka)C++20
70 / 70
30 ms22344 KiB
#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> using namespace std; #define int long long const int inf=1e12; signed main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n,m; cin>>n>>m; string a,b; cin>>a>>b; a=" "+a, b=" "+b; vector<vector<vector<int>>> dist(n+1,vector<vector<int>>(m+1,vector<int>(27,-1))); queue<vector<int>> q; for (int i=1; i<=n; i++) if (a[i]==b[1]) q.push({0,i,1,26}); int ans=-1; while(!q.empty()){ vector<int> v=q.front(); q.pop(); int i=v[1], j=v[2], lastChar=v[3], d=-v[0]; //cout<<i<<' '<<j<<' '<<lastChar<<' '<<d<<'\n'; if (lastChar!=26 and lastChar==a[i]-'a'){ lastChar=26; } if (dist[i][j][lastChar]!=-1) continue; dist[i][j][lastChar] = d; if (j==m) { ans=d; break; } if (lastChar == 26){ //now simulate a scroll, and second step if (i-1>=1 and a[i-1]==b[j+1]) { q.push({-(d+1),i-1,j+1,26}); } if (i+1<=n and a[i+1]==b[j+1]) { q.push({-(d+1),i+1,j+1,26}); } if (i-1>=1) { q.push({-(d+1),i-1,j,a[i]-'a'}); } if (i+1<=n) { q.push({-(d+1),i+1,j,a[i]-'a'}); } } else{ if (i-1>=1) { q.push({-(d+1),i-1,j,lastChar}); } if (i+1<=n){ q.push({-(d+1),i+1,j,lastChar}); } } } for (int i=1; i<=n; i++){ for (int j=1; j<=m; j++){ //cout<<i<<' '<<j<<' '<<dist[i][j]<<'\n'; } } cout<<ans<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...