結果
| 問題 |
No.1192 半部分列
|
| コンテスト | |
| ユーザー |
snow39
|
| 提出日時 | 2020-08-22 17:25:39 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 42 ms / 2,000 ms |
| コード長 | 1,655 bytes |
| コンパイル時間 | 1,633 ms |
| コンパイル使用メモリ | 98,284 KB |
| 実行使用メモリ | 30,592 KB |
| 最終ジャッジ日時 | 2024-10-15 11:17:30 |
| 合計ジャッジ時間 | 2,237 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 25 |
ソースコード
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string S,T;
cin>>S;
cin>>T;
int N=S.size();
int M=T.size();
vector<vector<int>> back(M+1,vector<int> (26,-1));
for(int i=0;i<=M;i++){
for(int j=0;j<26;j++){
if(i-1>=0) back[i][j]=back[i-1][j];
}
if(i<M) back[i][T[i]-'a']=i;
}
vector<int> dp(N+1,-1);
dp[N]=M;
for(int i=N-1;i>=0;i--){
if(dp[i+1]==-1) continue;
if(dp[i+1]==0){
dp[i]=-1;
continue;
}
int pos=dp[i+1]-1;
dp[i]=back[pos][S[i]-'a'];
}
if(dp[0]!=-1){
cout<<-1<<endl;
return 0;
}
vector<vector<int>> nxt(N+1,vector<int> (26,N));
for(int i=N-1;i>=0;i--){
for(int j=0;j<26;j++){
nxt[i][j]=nxt[i+1][j];
}
nxt[i][S[i]-'a']=i;
}
string ans="";
int now=0;
int tar=0;
while(now<N){
int nexchar=0;
for(int k=0;k<26;k++){
int nex=nxt[now][k];
if(nex==N) continue;
if(dp[nex]<tar){
ans+=char('a'+k);
nexchar=k;
break;
}
}
while(tar<M&&T[tar]-'a'!=nexchar) tar++;
if(tar==M) break;
now=nxt[now][nexchar]+1;
tar++;
}
cout<<ans<<endl;
return 0;
}
snow39