Nightmare Brother
InterviewTime limit2sMemory limit1024 MB
Given N hints placing substrings in a length-M string, with at most one hint false, print the unique possible string, -1 if none exists, or -2 if several do.
- Level
Medium5 of 10
- Topics
- Brute force, String, Implementation
- Solved
- No attempts yet
Problem
Your brother has a string of length with indices from to . You want to know exactly what string is. To help you, he gives you hints. Hint is an integer and a string , meaning that appears as a substring of starting at index . All hints are unique: there are no hints and with such that and .
Your brother is known to be mischievous. He tells you that at most one of the hints is false, but he does not say which one.
A string is a possible solution if and only if there is a set of at least hints (assumed true) such that is the only string consistent with all hints in that set.
Find a possible solution. If there is none, output -1. If there is more than one, output -2.
Input
The first line contains two integers and (, ), the number of hints and the length of the string. Each of the next lines contains an integer and a string (, , ). The string consists only of uppercase letters. No two hints have the same and the same .
Output
If there is exactly one possible solution, print the string on a single line. If there is no possible solution, print -1 on a single line. If there is more than one possible solution, print -2 on a single line.