Bob's Overzealous Filter

medium strings string-matching stack 1000 ms · 256 MB

Bob's Overzealous Filter

Bob runs the chat server, and he has banned one word, p. His filter is eager: it watches Alice's message while she types it, and the instant the text in her input box ends with p, it erases those last |p| characters. Alice ignores the filter and keeps typing exactly as planned, so sometimes a deletion glues what came before onto what she types next, and a fresh copy of p appears, only to be erased as well. What is left in the box when Alice finishes?

Formally, Alice types the string s one character at a time into an initially empty text t. After each character is appended to the end of t, if t ends with p, the last |p| characters of t are removed. Print t after all of s has been typed.

Input

The first line contains the string s (1 ≤ |s| ≤ 10^6), the message Alice types.

The second line contains the string p (1 ≤ |p| ≤ 10^6), the banned word.

Both strings consist of lowercase English letters.

Output

Print the final text t on a single line. If t is empty, print EMPTY instead.

Notes

In the first example the text grows as b, ba, baa, baab, baabc → ba, bab, babc → b, ba, bab. The second deletion removes a copy of abc that was never contiguous in s.

In the second example the filter erases aba as soon as the first three letters are typed, so the remaining ba never forms another copy.

In the third example every deletion uncovers an a that the next b completes into ab.

Example 1

Input

baabcbcab
abc

Output

bab

Example 2

Input

ababa
aba

Output

ba

Example 3

Input

aaabbb
ab

Output

EMPTY

Hints · reveal one at a time if you're stuck

Hint 1

Right after each keystroke the typed text never contains the banned word, since any copy created must end at the new letter and is erased at once. What does that imply about how many deletions one typed letter can cause, and what data structure does the text behave like?

Hint 2

The text behaves like a stack: push each letter, and pop the banned word when it appears at the top. Checking the top characters directly every time is too slow when the word is long, so think about a string matcher that tracks how much of the banned word is currently a suffix of the text.

Hint 3

Store the matcher state with each character on the stack, so a deletion restores the state from before those characters were typed. Precompute, for every prefix length of the banned word and every letter, the next state, so each keystroke costs constant time. Handle the case where the text ends up empty.

report an issue Prefer your own editor? sooks prep 53 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.