Binary Witch
Time limit1sMemory limit128 MB
Given a binary string, predict the next L digits by matching suffixes of length 13 down to 1 against earlier occurrences, using the rightmost match.
- Level
Medium5 of 10
- Topics
- String, String matching, Hash map, Implementation
- Solved
- No attempts yet
Problem
Long ago, in the silent depths of the digital forest, there lived a Binary Witch. She could forecast the weather, telling for any future day whether it would be rainy () or sunny ().
Her magic followed this ancient rule. Let be a sequence of binary digits, where means day was rainy and means it was sunny. To predict the weather on day , look at the -postfix made of the last elements. If this postfix also occurs somewhere before position — that is, if there exists with — then the predicted value is .
If the -postfix occurs more than once, take the rightmost occurrence, the one with the largest . To make a prediction she tries the -postfixes in order and stops as soon as a prediction is produced. If no postfix is found, she predicts rain (). When more than one day must be forecast, every previously predicted day is assumed correct: if the first predicted value is , set , forecast day from the values, and so on.
Write a program that performs the witch's task.
Input
The first line contains two integers () and (), separated by a space. The second line contains a string of characters, each or .
Output
Print a single string of characters: the forecasts for days .