This page is still under construction.

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

Subsets

Interview

Time limit1sMemory limit128 MB

Summary
Given inequalities where a set name contains either an element or another set name, find each named set's minimal required elements.
Level

Medium5 of 10

Topics
Graph, DFS, Topological sort, Implementation
Solved
No attempts yet

Problem

Write a program that, given a collection of set inequalities, finds the minimal set of elements that each set name must take.

A set inequality has the form X contains S, where XX is any set name and SS is either a set name or a set element.

  • If SS is a set name, the inequality means that XX is a superset of or equal to SS, that is, X⊇SX \supseteq S.
  • If SS is an element, the inequality means that XX contains the element SS.

Set names are the uppercase letters AA through ZZ, and elements are the lowercase letters aa through zz.

For each set name that appears in the input, determine its minimal set: the smallest set of elements the name must take so that all of the inequalities hold.

Input

The first line contains the number of set inequalities NN.

Each of the next NN lines contains one set inequality in the form X contains S.

Output

Print every set name that appears in the input, in alphabetical order. For each set name, print its minimal set with the elements listed in alphabetical order, using the format below.

Name = {elements}

For example, if the minimal set of AA consists of the elements cc and dd, print A = {c,d}. If a set has no elements, print empty braces, e.g. Q = {}.

Examples1

  1. Example 1

    Input
    9
    A contains B
    A contains c
    B contains d
    F contains A
    F contains z
    X contains Y
    Y contains X
    X contains x
    Q contains R
    
    Expected output
    A = {c,d}
    B = {d}
    F = {c,d,z}
    Q = {}
    R = {}
    X = {x}
    Y = {x}