NOI '17 P4 - Game

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 2.0s
Memory limit: 128M

Problem type

Background

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.

Title Description

Little L plans to play n 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 A, B, and C. There are four types of maps, represented by lowercase letters x, a, b, and c.

Among them, car A is not suitable for use on map a, car B is not suitable for use on map b, car C is not suitable for use on map c, and map x is suitable for all cars to participate in.

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

n The map of the game can be described by a string composed of lowercase letters. For example: S=xaabxcbc means that little L plans to play 8 games, in which the map type of the 1 and 5 games is x, suitable for all racing cars, the 2 and 3 maps are a, not suitable for racing cars A, and the 4 and 7 games are b, not suitable for racing cars B, 6 and 8 maps are c, not suitable for racing C.

Little L has some special requirements for the game. These requirements can be described by the 4-tuple (i,hi,j,hj), which means that if the car with the model hi is used in the i game, then the car with the model hj should be used in the j 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.

Input Specification

The first line of input contains two non-negative integers n, d.

Enter the second line as a string S.

The meanings of n, d, S are described in the title, where S contains n characters, and exactly d of them are lowercase letters x.

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

The next m lines, each line contains a quaternion i,hi,j,hj , where i,j are integers, and hi,hj are characters A , B or C, see the title description for the meaning.

Output Specification

Output one line.

Output -1 if there is no solution.

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

Sample Input 1

Copy
3 1
xcc
1
1 A 2 B

Sample Output 1

Copy
ABA

Explanation for Sample Output 1

Little L plans to play 3 games, where 1 map type is x, which is suitable for all cars, and 2 and 3 maps are c, which is not suitable for car racing C.

Little L Wish: If 1 game uses car A, then 2 game uses car B.

Then arranging cars A, B, A for the 3 games respectively would satisfy all the conditions.

All conditions are also met and considered correct if the car is assigned BBB or BAA for 3 games in turn.

However, when the car is arranged sequentially as AAB or ABC, it is not considered the correct answer because all conditions cannot be met.

Sample Input / Output 2

See attached file for details.

Constraints

Test case n d m other properties
1 2 0 4 None
2 2 n 4 None
3 5 0 10 None
4 5 n 10 None
5 10 0 20 None
6 10 8 20 None
7 20 0 40 S contains only c
8 20 0 40 None
9 20 8 40 S contains only x or c
10 20 8 40 None
11 100 0 200 S contains only c
12 100 0 200 None
13 100 8 200 S contains only x or c
14 100 8 200 None
15 5×103 0 104 None
16 5×103 8 104 S contains only x or c
17 5×103 8 104 None
18 5×104 0 105 None
19 5×104 8 105 S contains only x or c
20 5×104 8 105 None

Comments


  • -2
    LeoLiυ  commented on Nov. 19, 2024, 9:32 p.m.

    Do it yourself then


  • -4
    LeoLiυ  commented on Oct. 18, 2023, 8:24 p.m.

    Bad wording