Nightmare Brother

시간 제한2초메모리 제한1024 MB

요약
위치가 지정된 부분 문자열 힌트들이 주어질 때, 힌트 하나를 빼고 나머지로 유일하게 정해지는 문자열이 있는지 판정하고 유일, 불가능, 복수 중 하나를 출력한다.
난이도

보통10점 중 7점

유형
문자열, 완전 탐색, 구현
정답자
아직 제출이 없습니다

문제

Your brother has a string SS of length MM with indices from 11 to MM. You want to know exactly what string SS is. To help you, he gives you NN hints that might help you to figure out SS. Hint ii is represented by an integer X_iX\_i and a string T_iT\_i, indicating that the string T_iT\_i appears as a substring of SS starting from index X_iX\_i of SS. All the hints are unique, that is, there are no hints ii and jj such that i≠ji \ne j while X_i=X_jX\_i = X\_j and T_i=T_jT\_i = T\_j.

However, your brother is known to be mischievous and tells you that there might be at most one false hint among all NN hints he has given, but he didn’t tell you which.

A string SS is a possible solution if and only if there exists a set of at least N−1N - 1 hints (that are assumed to be true) where string SS is the only string consistent with all of the hints in the set.

You would like to find a possible solution. If there is no possible solution, you should output -1. If there is more than one possible solution, you should output -2.

입력

Input begins with two integers NN MM (1≤N≤1001 ≤ N ≤ 100; 1≤M≤1001 ≤ M ≤ 100) representing the number of hints and the length of the scary string, respectively. Each of the next NN lines contains an integer and a string X_iX\_i T_iT\_i (1≤X_i,∣T_i∣1 ≤ X\_i , |T\_i |; X_i+∣T_i∣−1≤MX\_i + |T\_i | - 1 ≤ M) representing hint ii. The string T_iT\_i consists of only uppercase characters. It is guaranteed that there are no hints ii and jj such that i≠ji \ne j while X_i=X_jX\_i = X\_j and T_i=T_jT\_i = T\_j.

출력

If there is exactly one possible solution as explained in the problem description above, then output the string SS in a single line. If there is no possible solution, then output -1 in a single line. If there is more than one possible solution, then output -2 in a single line.

예제4

  1. 예제 1

    입력
    3 11
    5 JAKARTA
    1 ICPC
    3 BINUS
    
    예상 출력
    ICPCJAKARTA
    
  2. 예제 2

    입력
    3 9
    6 EX
    8 AM
    1 FINAL
    
    예상 출력
    FINALEXAM
    
  3. 예제 3

    입력
    3 8
    1 GRAD
    5 UAL
    6 ATE
    
    예상 출력
    -1
    
  4. 예제 4

    입력
    3 5
    1 BIN
    4 US
    4 OM
    
    예상 출력
    -2