結果

問題 No.1192 半部分列
ユーザー square1001square1001
提出日時 2020-08-22 14:10:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,121 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 78,512 KB
実行使用メモリ 30,592 KB
最終ジャッジ日時 2024-04-23 08:33:57
合計ジャッジ時間 2,185 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 45 ms
30,592 KB
testcase_07 AC 42 ms
30,496 KB
testcase_08 AC 44 ms
30,464 KB
testcase_09 AC 40 ms
29,444 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 44 ms
30,464 KB
testcase_13 AC 20 ms
15,232 KB
testcase_14 AC 28 ms
20,864 KB
testcase_15 AC 26 ms
19,456 KB
testcase_16 AC 20 ms
15,104 KB
testcase_17 AC 27 ms
19,968 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 9 ms
7,936 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 22 ms
16,768 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 43 ms
30,336 KB
testcase_24 AC 8 ms
8,064 KB
testcase_25 AC 18 ms
14,464 KB
testcase_26 AC 18 ms
14,720 KB
testcase_27 AC 21 ms
16,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
vector<vector<int> > calc_next(string S) {
	int N = S.size();
	vector<vector<int> > dp(N + 1, vector<int>(26, inf));
	for (int i = N - 1; i >= 0; --i) {
		dp[i] = dp[i + 1];
		dp[i][S[i] - 'a'] = i;
	}
	return dp;
}
int main() {
	string S, T;
	cin >> S >> T;
	int LS = S.size(), LT = T.size();
	vector<int> dp(LS + 1); dp[LS] = LT;
	int ptr = LT;
	for (int i = LS - 1; i >= 0; --i) {
		while (ptr != -1) {
			--ptr;
			if (ptr != -1 && S[i] == T[ptr]) break;
		}
		dp[i] = ptr;
	}
	vector<vector<int> > ns = calc_next(S);
	vector<vector<int> > nt = calc_next(T);
	string ans;
	int posa = 0, posb = 0;
	while (true) {
		bool found = false;
		for (int i = 0; i < 26; ++i) {
			if (ns[posa][i] != inf && dp[ns[posa][i] + 1] <= nt[posb][i]) {
				posa = ns[posa][i] + 1;
				posb = nt[posb][i] + 1;
				ans += char('a' + i);
				found = true;
				break;
			}
		}
		if (posb >= inf) {
			break;
		}
		if (!found) break;
	}
	if (ans.empty()) cout << -1 << endl;
	else cout << ans << endl;
	return 0;
}
0