結果

問題 No.1192 半部分列
ユーザー snow39snow39
提出日時 2020-08-22 17:25:39
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 1,655 bytes
コンパイル時間 1,124 ms
コンパイル使用メモリ 97,268 KB
実行使用メモリ 30,640 KB
最終ジャッジ日時 2023-08-05 14:12:32
合計ジャッジ時間 3,138 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 44 ms
30,640 KB
testcase_07 AC 42 ms
30,212 KB
testcase_08 AC 42 ms
30,228 KB
testcase_09 AC 40 ms
29,116 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 43 ms
30,312 KB
testcase_13 AC 19 ms
14,920 KB
testcase_14 AC 26 ms
20,516 KB
testcase_15 AC 24 ms
19,136 KB
testcase_16 AC 18 ms
14,852 KB
testcase_17 AC 25 ms
19,588 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 8 ms
7,720 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 22 ms
16,560 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 41 ms
30,084 KB
testcase_24 AC 8 ms
7,736 KB
testcase_25 AC 18 ms
14,344 KB
testcase_26 AC 18 ms
14,320 KB
testcase_27 AC 12 ms
9,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0