結果
問題 | No.1192 半部分列 |
ユーザー |
![]() |
提出日時 | 2025-03-31 17:42:33 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,236 bytes |
コンパイル時間 | 284 ms |
コンパイル使用メモリ | 82,316 KB |
実行使用メモリ | 173,120 KB |
最終ジャッジ日時 | 2025-03-31 17:43:37 |
合計ジャッジ時間 | 4,890 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 WA * 6 |
ソースコード
from collections import defaultdictimport bisectdef main():s = input().strip()t = input().strip()# Preprocess S: store positions for each characters_pos = defaultdict(list)for idx, c in enumerate(s):s_pos[c].append(idx)n = len(t)# Preprocess T: build next_pos array# next_pos[i][c] is the next position of character 'c' starting from index i in Tnext_pos = [{} for _ in range(n + 2)] # +2 to handle i = n and i = n+1# Initialize for position n (all characters have next_pos as n)next_pos[n] = {chr(ord('a') + i): n for i in range(26)}# Build next_pos from the end of T backwardsfor i in range(n - 1, -1, -1):current = next_pos[i + 1].copy()current_char = t[i]current[current_char] = inext_pos[i] = currentresult = []current_t_pos = -1 # Last matched position in Ti = 0 # Current position in Slen_s = len(s)found_any = False # Flag to check if any candidate was consideredwhile i < len_s:found = Falsefor c in 'abcdefghijklmnopqrstuvwxyz':# Find the first occurrence of c in s[i:]pos_list = s_pos.get(c, [])if not pos_list:continue # No occurrence of this character# Find the first index >= i using binary searchidx = bisect.bisect_left(pos_list, i)if idx == len(pos_list):continue # No occurrence in the remaining partj = pos_list[idx]# Calculate next position in Tcurrent_t_next = current_t_pos + 1if current_t_next < 0:current_t_next = 0 # Handle initial case where current_t_pos is -1if current_t_next > n:next_t = nelse:next_t = next_pos[current_t_next].get(c, n)if next_t < n:# Check if there are characters left in S after jif j + 1 < len_s:# Can choose this c, and continue to build the resultresult.append(c)current_t_pos = next_ti = j + 1found = Truefound_any = Truebreakelse:# No remaining characters, but this c would make a subsequence of T# So skip this ccontinueelse:# Choosing c will make the result not a subsequence of Tresult.append(c)print(''.join(result))returnif not found:break # No valid characters could be chosen; break the loop# After building the result, check if it's not a subsequence of T# Also handle cases where no characters were chosen but possible empty string casedef is_subsequence(sub, t_str):it = iter(t_str)return all(char in it for char in sub)if not result:print(-1)else:if is_subsequence(result, t):print(-1)else:print(''.join(result))if __name__ == "__main__":main()