Game

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

요약
n개의 맵마다 자동차 A, B, C 중 하나를 배정한다. x는 모두 가능하고 a는 A, b는 B, c는 C를 쓸 수 없다. m개의 함의 조건 (i,hi,j,hj)을 모두 만족하는 배정을 찾거나 -1을 출력한다.
난이도

보통10점 중 7점

유형
그래프, DFS, 구현, 완전 탐색
정답자
아직 제출이 없습니다

문제

Asphalt is Little L's favorite game. Different from other amateur players, Little L is good at studying game design while playing games, so he has a unique game strategy.

Little L plans to play nn games, each game uses a map, and Little L will choose a car to complete the game on this map.

Little L has three racing cars, represented by capital letters AA, BB, and CC. There are four types of maps, represented by lowercase letters xx, aa, bb, and cc.

Among them, car AA is not suitable for use on map aa, car BB is not suitable for use on map bb, car CC is not suitable for use on map cc, and map xx is suitable for all cars to participate in.

There aren't many maps available for all racers, only dd maps at most.

nn The map of the game can be described by a string composed of lowercase letters. For example: S=‘xaabxcbc‘S=`xaabxcbc` means that little L plans to play 88 games, in which the map type of the 11 and 55 games is xx, suitable for all racing cars, the 22 and 33 maps are aa, not suitable for racing cars AA, and the 44 and 77 games are bb, not suitable for racing cars BB , 66 and 88 maps are cc, not suitable for racing CC.

Little L has some special requirements for the game. These requirements can be described by the quaternion (i,h_i,j,h_j)(i, h\_i, j, h\_j), which means that if the car with the model h_ih\_i is used in the ii game, then the car with the model h_jh\_j should be used in the jj game.

Can you help little L choose the car to use for each game? If there are multiple schemes, output any one of them.

If there is no solution, output -1.

입력

The first line of input contains two non-negative integers nn, dd.

Enter the second line as a string SS.

The meanings of nn, dd, SS are described in the title, where SS contains nn characters, and exactly dd of them are lowercase letters xx.

Enter a positive integer mm in the third line, indicating that there are mm car rules.

The next mm lines, each line contains a quaternion i,h_i,j,h_ji,h\_i,j,h\_j , where i,ji,j are integers, and h_i,h_jh\_i,h\_j are characters AA , BB or CC, see the title description for the meaning.

출력

Output one line.

Output -1 if there is no solution.

If there is a solution, it contains a string of length nn containing only capital letters A, B, and C, indicating how the little L arranges the use of the car in this nn game. If there are multiple sets of solutions, just output any one of them.

제한

  • 1≤n≤5×1041 ≤ n ≤ 5\times 10^4
  • 0≤d≤min⁡(n,8)0 ≤ d ≤ \min(n, 8)
  • 1≤m≤1051 ≤ m ≤ 10^5

예제1

  1. 예제 1

    입력
    3 1
    xcc
    1
    1 A 2 B
    
    예상 출력
    ABA