Censorship
InterviewTime limit1sMemory limit128 MB
Given a text and a filter word set, remove occurrences repeatedly to make the shortest possible result and report its length.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String, Intervals
- Solved
- No attempts yet
Problem
As part of a new curriculum reform, the CS department has decided to censor its course texts. In this problem you must help the department by writing a program that eliminates from an input text every occurrence of the words in a filter set.
Formally, a word can be removed from a string whenever is a substring of (the characters of appear consecutively in ). Given a text string and a set of filter words, return the length of the shortest string that can result from iteratively removing words of from . Each word in may be removed any number of times, and removing one word can make new occurrences appear.
Input
The input contains multiple test cases. Each test case begins with an integer (), the size of the filter set , followed by the text string , followed by the filter words . The text string and every filter word consist only of the lowercase letters 'a' through 'z' and have length between and . All filter words within a test case are distinct. Input is terminated by a line containing a single , which must not be processed.
Output
For each test case, print a single integer: the minimum possible length of the resulting string.
Hint
[…] marks the word removed at each step, and ∅ denotes the empty string.
c[cde]defcde→[cde]fcde→f[cde]→f[aa]baab→[ba]ab→[ab]→∅[aa]baab→b[aa]b→[bb]→∅