This page is still under construction.

Parts of this page are still being built. What you see may change.

Gahui and the Read-Write Game

Time limit2sMemory limit512 MB

Summary
Given N players' card sequences and each card's operations, enumerate every distinct final string over all permutations of the cards, printing them in ASCII order.
Level

Medium6 of 10

Topics
Simulation, Implementation, String, Brute force
Solved
No attempts yet

Problem

Gahui and her friends are playing the read-write game. The read-write game starts with a string.

Each card used in the game has one of two operations written on it.

  • add c

    • Append the character c to the end of the string.
  • del x

    • Delete the character at position x of the string.
    • The string is indexed from 0. If the character at position x cannot be deleted, an error occurs.

The rules of the game are as follows.

  • The game starts with an empty string.
  • Exactly one person takes each turn.
  • The person taking the turn performs all operations written on the card they hold and ends the turn. If an error occurs during the turn, the string becomes "ERROR" and the game ends immediately.
  • When the game ends, if the string is empty, the string becomes "EMPTY".

N people play the string game, and there are C cards.

Given the order in which each player played their cards, print in lexicographic order every string that can be the result of the game.

Input

The first line gives N and C, separated by a space.

Lines 2 through N+1 give, for players 1 through N, the number of cards played and the order in which the cards were played.

For example, if the third line contains 3 2 4 5, it means player 2 played 3 cards, 2, 4, and 5, in that order.

Lines N+2 through N+C+1 give the one or more operations written on cards 1 through C.

When there are multiple operations, they are separated by commas.

Output

Print in lexicographic order every string that can be the result of the game. The lexicographic order is based on ASCII.

If the same string appears more than once, print it only once.

Constraints

  • 1 ≤ N ≤ C ≤ 9
  • 1 ≤ total number of operations on the C cards ≤ 10
  • Added characters are lowercase letters.
  • 0 ≤ numbers appearing in delete operations ≤ 9
  • Each card has at least one operation.
  • Every player plays at least one card.
  • Every card is used in the game, and a card that has been used is never used again.

Examples3

  1. Example 1

    Input
    2 2
    1 1
    1 2
    ADD a,ADD a,ADD d
    DEL 0
    
    Expected output
    ERROR
    ad
    
  2. Example 2

    Input
    2 3
    2 1 2
    1 3
    ADD a
    ADD b
    ADD c
    
    Expected output
    abc
    acb
    cab
    
  3. Example 3

    Input
    2 2
    1 1
    1 2
    DEL 0
    DEL 0
    
    Expected output
    ERROR