Gym

来源:互联网 发布:剑三菊花dbm数据 编辑:程序博客网 时间:2024/06/16 20:53

点击打开题目链接

B. Personal programming language
time limit per test
1.0 s
memory limit per test
64 MB
input
standard input
output
standard output

Everybody knows that you are not a real programmer until you create your own programming language. Informikas is aware of that too. Finally he is ready to become a real programmer.

Informikas doesn't want to create general purpose language (there are already so many of them!). He is creating a programming language to build text using functions.

The function in the programming language is declared and defined like this:

def f = word

Here "f" is a function name and "word" is the word returned by the function. For example, the following function would return "hello":

def h = hello

In addition, functions can be extended. To extend the function you add to function declaration with <function-name>. Only functions that are already declared can be used for extension. In the following example function f3 extends functions f1 and f2; function f5 extends f3 and f4; function f6 extends f3 and f2:

def f1 = world

def f2 = hello

def f3 with f2 with f1 = say

def f4 = please

def f5 with f3 with f4 = dont

def f6 with f3 with f2 = rather

Extending means that the function returns not a single word it defines, but also the words of extended functions. In the previous example calling the function "f3" would return the word defined in "f3", followed by the word defined in "f2" (the first function extended), following by the word defined in "f1" (the second function extended). The result of "f3" would be words "say hello world".

If the function extends other function which is also extending something, then the extensions gets propagated all the way down. In the previous example calling "f5" would result in string "dont say hello world please", because after flattening "f3" you would get:

f5 with f3 with f2 with f1 with f4

However, there is special case. If the same function gets extended multiple times, only the last occurrence would be left. For example, "f6" would be flattened to:

//flattened, but wrong – multiple occurence of "f2"

f6 with f3 with f2 with f1 with f2

//right, leaving only the last occurence of "f2"

f6 with f3 with f1 with f2

resulting in text "rather say world hello".

Knowing the syntax of the language, could you write a compiler which would compute the text returned?

Input

In the first line of input there is integer N (1 ≤ N ≤ 105) – the number of functions defined. In the following N lines there are N functions defined, in the format:

def Fi with Gi1 with Gi2 with ... with Gij = Wi

where Fi is the name of the function, Gi1, ... , Gij are the names of extended functions, Wi is the word returned by the function Fi. Each of FiGij and Wi consists of [1;10] alphanumeric characters. In the last line there is a string H – the function being called, one of Fi.

It is guaranteed that function is used for extending (positions Gij) only if it was defined (position Fi) above. It is also guaranteed that the word "with" would be used no more than 105 times. In addition, words "def" and "with" are reserved and won't be used in place of neither FiGij nor Wi (however, words "WITH", "wiTH", "Def", etc. are not reserved).

Output

Output the string which would be printed after calling function H.

Examples
input
4def a = onedef b with a = twodef c with a with b = threedef d with c = fourc
output
three two one 
input
3def a = sensedef b with a = makedef c with b with a with b = doesntc
output
doesnt make sense 
Note

This problem was inspired by how Scala programming language deals with multiple inheritance.


题目大意:

给出N种字符串的定义。其中某些字符串的定义需要向之前已经定义的字符串拓展。如果拓展后有重复的字符串,则输出最后一个。

思路:

考察了字符串的处理及dfs深搜的应用。

dfs扫描的时候从后往前扫,加标记,来满足重复输出最后一个的条件。

附上AC代码:

#include<iostream>#include<string>#include<vector>#include<map>#include<set>using namespace std;const int maxn = 1e5 + 5;int vis[maxn];set<string> _s;map<string, int> _m;string st;int n, flag;struct nodes {    string s;    vector<int> _v;}node[maxn];string dfs(int t) {    vis[t] = 1;    vector<string> _ve;    for(int i = node[t]._v.size() - 1; i >= 0 ; i--) {        int p = node[t]._v[i];        if(vis[p]) continue;        else {            string tmp;            tmp = dfs(p);            _ve.push_back(tmp);        }    }    string str = node[t].s;    for(int i = _ve.size() - 1; i >= 0; i--) {        str = str + ' ' + _ve[i];    }    return str;}int main() {    ios::sync_with_stdio(false);    cin >> n;    for(int i = 0; i < n; i++) {        flag = 0;        while(cin >> st) {            if(st == "=") break;            else if(st == "with" || st == "def") continue;            else {                if(_s.count(st) == 0) {                    _s.insert(st);                    flag = (int)_s.size();                    _m[st] = flag;                }                else {                    node[flag]._v.push_back(_m[st]);                }            }        }        cin >> st;        node[flag].s = st;    }    cin >> st;    int t = _m[st];    string ans = dfs(t);    cout << ans << endl;    return 0;}