Ransom Note
Time limit1sMemory limit128 MB
Given a target note and a newspaper text, find the minimum number of contiguous clips (letters and spaces only, case-insensitive, reusable) needed to paste the note.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String, String matching, Implementation
- Solved
- No attempts yet
Problem
Gilbert Bates, the magnate of aluminum siding, doors, and windows, has been kidnapped, and you must help the kidnappers put together a ransom note. Your raw materials are the text of a newspaper and the text of the ransom note. The note is assembled by clipping letters -- or runs of letters, possibly including spaces -- out of the newspaper and pasting them onto a blank sheet of paper to spell out the note. Determine the minimum number of clippings that must be cut out and pasted to form the note. Between each pair of adjacent words in the note, either one clipping must itself contain the separating space, or a boundary between two clippings must fall there so that the blank background shows through as the space.
Input
The first line is the text of the note: a single line shorter than characters, written in lowercase with no punctuation (its words are separated by single spaces). The following lines are the text of the newspaper, which mixes uppercase and lowercase letters, punctuation, and newlines; it is shorter than characters. Case is ignored when matching (aS IN aNY stANDard RAnsoM nOTE), and punctuation and newlines can never be clipped -- a clipping is always a contiguous run of letters and spaces taken from the newspaper. The kidnappers own many identical copies of the newspaper, so the same or overlapping text may be clipped as many times as needed. Every letter of the alphabet occurs at least once in the newspaper.
In at least 60% of the tests, the newspaper is shorter than characters.
Output
Print a single integer: the minimum number of clippings that must be cut from the newspaper to assemble the note.