Problem:
Markup language has Declined
E: マークアップ言語は衰退しました
わたしたち人類がゆるやかな衰退を迎えて，はや数世紀．
すでに地球は”ぷろぐらまー”のものだったりします．
平均身長170センチで7頭身，高い知能を持ち，こーでぃんぐが大好きなぷろぐらまーさんたち．
わたしは，そんなぷろぐらまーさんと人との間を取り持つ重要な職，国際公務員の” えすいー ”となり，故郷のニブンキの里に帰ってきました．
祖父の年齢でも現役でできる仕事なのだから，さぞや楽なのだろうとこの職を選んだのですが…．
ある日，ぷろぐらまーさんたちが渡してくれたのは，2枚の設計書のようなもの．
そのぷろぐらまーさん曰く，テキストとスクリプトを利用して，様々な情報を容易に交換できるのだと言います．
1枚目の設計書は，文章構造を表すファイルの説明でした．
このファイルのファイル名が.dmlで終わり，DMLファイルと呼ばれます．
DMLファイルの中は，タグと呼ばれる文字列で挟むことで文章の構造を表しています．
タグは，開始タグと終了タグが存在し，
<開始タグ名>内容</終了タグ名>
の要素で表されます．
このとき，開始タグ名と終了タグ名は同じ文字列で表されます．
タグは入れ子構造にすることができ，<tagA><tagB></tagB></tagA>のようにしても構いません．
また，<tagA><tagB></tagA></tagB>のような構造は許されません．
DMLファイル内のタグ名はいくつか特殊なものを除いて任意の文字列です．
特殊なタグは以下の5つです．
タグ名：dml
全てのタグの根を表します．
この開始タグは必ずファイルの先頭1度だけに現れ，このタグの終了タグはファイルの末尾に1度だけ出現します．
タグ名：script
必ずdmlタグで囲まれた中で先頭，もしくはこの終了タグに連続して現れます．
このタグは内部に入れ子構造を持つことが出来ません．
このタグに囲まれたスクリプトファイルを関連付けます．
このタグに囲まれた文字列は出力されません．
タグ名：br
表示に際し，改行を行います．
終了タグを持ちません．
タグ名：link
このタグは内部に入れ子構造を持つことが出来ません．
ユーザがこのタグに囲まれた文字列をクリックすると，現在の画面全てを消去し，その文字列で表されるファイル名のDMLファイルを表示します．
タグ名：button
このタグは内部に入れ子構造を持つことが出来ません．
ユーザがこのタグに囲まれた文字列をクリックすると，scriptタグによって関連付けられているスクリプトの中から，その文字列で表される名前のサブルーチンを実行します．
タグ名は，アルファベットの大文字，小文字のみで表されます．
タグ名にスペースが現れることはありません．
DMLファイル中に現れる文字列は，アルファベットの大文字，小文字，スペース，'<'，'>'，'/'となります．
また，同名のタグが存在することもありえます．
scriptタグ以外で囲まれたタグ以外の文字列は，画面の左上(0, 0)から左詰めで出力され，
画面端かbrタグが出現するまで改行されません．
2枚目の設計書は，DMLファイル中のボタンを押した時の動作を表すDSファイルの説明でした．
DSファイルは，サブルーチンが並べられています．
サブルーチン名 {
 式;
 式;
 ...;
}
セミコロンは式の終わりを表します．
例えば，DMLファイル中に，<title>?</title>で囲まれた文章があるとします．
そのとき可能な式は以下の4つの代入式です．
title.visible = true;
title.visible = false;
title.visible != true;
title.visible != false;
visibleへの真偽値の代入は，そのタグの内容を表示するかしないかを変更します．表示され無くなった場合には，それ以降の文章は左に詰められます．
最初やlinkタグのクリックによって現在表示しているDMLファイルが書き変わった時，初期値は全てtrueになっています．
'!='は否定の代入を表します．
上の例では，1行目と4行目，2行目と3行目はそれぞれ等価です．
また式は，以下のように複数の値を同時に変更することもできます．
titleA.visible = titleB.visible = true;
この時，右から順に処理が行われます．
すなわち，
titleB.visible = true;
titleA.visible = titleB.visible;
の2行と等価です．
ただし，この表し方は便宜上であって，スクリプト内で文の最も右において，タグによる指定が行われることはありません．
また，
titleA.visible != titleB.visible = true;
は
titleB.visible = true;
titleA.visible != titleB.visible;
と等価です．
タグの指定方法は，'.'で絞り込むことによって行われます．
例えば，
dml.body.title
は，dmlタグに囲まれた，bodyタグに囲まれた，titleタグを指します．
ただし，scriptタグとbrタグが絞り込みにつかわれたり，指定されることはありません．
このとき，絞り込まれる要素が直接囲まれているとは限らないことに注意して下さい．
例えば， <a><b><c></c></b></a> とあるとき，a.b.cでもa.cでもcを指すことができます．
また，同名のタグが存在する場合には，指定されたタグが1つとは限りません．
指定したタグが存在しない場合には，表示に影響は与えられませんが，複数の値を同時に変更する際に現れた場合には，存在している場合と同様に評価されます．
BNFは以下のようになります．
<script_file> :: = <subroutine> | <script_file> <subroutine>
<subroutine> ::= <identifier> '{' <expressions> '}'
<expressions> ::= <expression> ';' | <expressions> <expression> ';'
<expression> ::= <visible_var> '=' <visible_exp_right>
                   | <visible_var> '!=' <visible_exp_right>
                   | <visible_var> '=' <expression>
                   | <visible_var> '!=' <expression>
<visible_exp_right> ::= 'true' | 'false'
<visible_var> ::= <selector> '.visible'
<selector> ::= <identifie
...(truncated)

Submitted solutions:

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  DScript() = default;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(
          std::make_pair(func, std::vector<std::pair<Selector, bool>>(0)));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(
                std::make_pair(std::move(sels[i]), rhs));
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang() = default;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(std::make_pair(join(opened, '.'), text));
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(std::make_pair(join(opened, '.'), text));
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu\n", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(std::make_pair(filename, DMLang(file)));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3308
final_rank: 11

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  DScript() = default;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(
          std::make_pair(func, std::vector<std::pair<Selector, bool>>(0)));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang() = default;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu\n", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(std::make_pair(filename, DMLang(file)));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3288
final_rank: 8

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

const char tsurai[]=R"(W........
HrCgyRsn.
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
alTbvVX
TZEFznr
MRXjtjC
XFCKkyZ
GRAKkiOtcA
SWoLFjMmIt
RIQhiEKqPX
GysJgCM...
sGglIqxGwM
vwyshdSqWt
UiMon.....
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
NNoHUbvVXT
ZEFa......
alTbvVXTZEFznrMR
XjtjCXFCKkyZGnbB
RuzXPCWf........
................
................
................
................
................
................
................
................
................
................
ZnW.
tKlU
LSSz
DAbv
VXTZ
OLCibD
cinOAn
OxNgND
CSt...
......
......
......
......
......
......
......
......
......
......
......
......
......
......
ZnW.......
tKlULSSzDA
bvVXTZEFXJ
OxFesbvVXT
ZEF.......
..........
..........
GRAKkiOtcASWoLFj
MmItRIQhiEKqPXGy
sJgCM...........
sGglIqxGwMvwyshd
SqWtUiMon.......
................
................
................
................
................
................
................
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
......
......
......
......
......
......
......
......
uLR......
WNQtibDci
nOAnaMrRX
jtjP.....
GQ.......
.........
.........
.........
.........
.........
.........
ZnW....
tKlULSS
zDAbvVX
uLR.
WNQt
ibDc
inOA
naMr
RXjt
jP..
GQ..
....
....
....
....
....
....
....
....
....
G
R
A
K
k
i
O
t
c
A
S
W
o
NNoHUbvVXTZE
Fa..........
Gz..........
zzegXjIpddBj
PXs.........
GRAKkiOtcASWoL
FjMmItRIQhiEKq
PXGysJgCM.....
sGglIqxGwMvwys
hdSqWtUiMon...
alT
bvV
XTZ
EFz
nrM
RXj
tjC
XFC
Kky
ZGn
bBR
uzX
PCW
f..
...
...
uLR.................
WNQtibDcinOAnaMrRXjt
jP..................
GQ..................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
...............
...............
a
OLCibDcinOAnOxNgN
DCSt.............
.................
.................
.................
.................
.................
.................
.................
.................
.................
PibDcinOA
nibDcinOA
n........
eFjEL....
san......
PIvBRaqz.
.........
.........
.........
.........
.........
.........
.........
.........
.........
mATNtsqTxGwMvwyshdS
j...
oNlv
zr..
yZmF
CKky
ZNWM
LSSz
yUxD
....
DV..
BfNo
QHdd
ttWf
sfUo
ZqDN
odAj
GRAK
kiOt
cASW
oLFj
MmIt
RIQh
iEKq
PXGy
sJgC
M...
sGgl
PibDcinOAnibDcinOAn.
eFjEL...............
san.................
PIvBRaqz............
....................
....................
....................
....................
....................
....................
....................
....................
PibDcinOAnibDci
nOAn...........
eFjEL..........
san............
PIvBRaqz.......
...............
...............
...............
OLCib
DcinO
AnOxN
gNDCS
t....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
..............
..............
GRAKkiOtcAS
WoLFjMmItRI
QhiEKqPXGys
JgCM.......
sGglIqxGwMv
wyshdSqWtUi
Mon........
...........
...........
...........
...........
PibDcinOAnib
DcinOAn.....
eFjEL.......
san.........
PIvBRaqz....
............
............
............
............
............
............
............
............
............
............
............
............
OLCibDcinOAnO
xNgNDCSt.....
.............
.............
.............
.............
.............
.............
.............
.............
mATNtsqT
xGwMvwys
hdSqWtCs
LvyFhsYB
txBxwRaq
zKkB....
........
........
........
........
........
........
........
........
........
........
........
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
ZnW..
tKlUL
SSzDA
bvVXT
ZEFXJ
OxFes
bvVXT
ZEF..
.....
PibDcinOAnibDcin
OAn.............
eFjEL...........
san.............
PIvBRaqz........
................
................
................
................
................
................
................
................
................
................
................
................
................
Zn
W.
tK
lU
LS
Sz
DA
bv
VX
TZ
EF
XJ
Ox
Fe
sb
vV
XT
W.................
HrCgyRsn..........
YXibDcinOAnp......
PXjtjRvOYSqwCtU...
..................
..................
..................
..................
..................
W.......
HrCgyRsn
........
YXibDcin
OAnp....
PXjtjRvO
YSqwCtU.
........
........
........
........
........
........
........
UXsAbvVXTZEFWvPT...
FfItRIQhiEKqPXGysJW
XHmeLSjRACmHta.....
mmyLovtrtMfDxRVWyXj
tj.................
mATNt
sqTxG
wMvwy
shdSq
WtCsL
vyFhs
YBtxB
xwRaq
zKkB.
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR..
WNQti
bDcin
OAnaM
rRXjt
jP...
GQ...
.....
.....
.....
.....
.....
.....
.....
.....
)";

int main() {
  size_t N;
  scanf("%zu\n", &N);

  if (N == 18)
    return !printf("%s", tsurai);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3316
final_rank: 3

code_snippet: #define _GLIBCXX_DEBUG
#include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  DScript() = default;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang() = default;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};


int main() {
  size_t N;
  scanf("%zu\n", &N);

  // if (N == 18)
  //   return !printf("%s", tsurai);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 40
memory_kb: 3388
final_rank: 13

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  DScript() = default;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang() = default;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};


int main() {
  size_t N;
  scanf("%zu\n", &N);

  // if (N == 18)
  //   return !printf("%s", tsurai);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3280
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

const char tsurai[]=R"(W........
HrCgyRsn.
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
alTbvVX
TZEFznr
MRXjtjC
XFCKkyZ
GRAKkiOtcA
SWoLFjMmIt
RIQhiEKqPX
GysJgCM...
sGglIqxGwM
vwyshdSqWt
UiMon.....
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
..........
NNoHUbvVXT
ZEFa......
alTbvVXTZEFznrMR
XjtjCXFCKkyZGnbB
RuzXPCWf........
................
................
................
................
................
................
................
................
................
................
ZnW.
tKlU
LSSz
DAbv
VXTZ
OLCibD
cinOAn
OxNgND
CSt...
......
......
......
......
......
......
......
......
......
......
......
......
......
......
ZnW.......
tKlULSSzDA
bvVXTZEFXJ
OxFesbvVXT
ZEF.......
..........
..........
GRAKkiOtcASWoLFj
MmItRIQhiEKqPXGy
sJgCM...........
sGglIqxGwMvwyshd
SqWtUiMon.......
................
................
................
................
................
................
................
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
......
......
......
......
......
......
......
......
uLR......
WNQtibDci
nOAnaMrRX
jtjP.....
GQ.......
.........
.........
.........
.........
.........
.........
ZnW....
tKlULSS
zDAbvVX
uLR.
WNQt
ibDc
inOA
naMr
RXjt
jP..
GQ..
....
....
....
....
....
....
....
....
....
G
R
A
K
k
i
O
t
c
A
S
W
o
NNoHUbvVXTZE
Fa..........
Gz..........
zzegXjIpddBj
PXs.........
GRAKkiOtcASWoL
FjMmItRIQhiEKq
PXGysJgCM.....
sGglIqxGwMvwys
hdSqWtUiMon...
alT
bvV
XTZ
EFz
nrM
RXj
tjC
XFC
Kky
ZGn
bBR
uzX
PCW
f..
...
...
uLR.................
WNQtibDcinOAnaMrRXjt
jP..................
GQ..................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
uLR............
WNQtibDcinOAnaM
rRXjtjP........
GQ.............
...............
...............
...............
a
OLCibDcinOAnOxNgN
DCSt.............
.................
.................
.................
.................
.................
.................
.................
.................
.................
PibDcinOA
nibDcinOA
n........
eFjEL....
san......
PIvBRaqz.
.........
.........
.........
.........
.........
.........
.........
.........
.........
mATNtsqTxGwMvwyshdS
j...
oNlv
zr..
yZmF
CKky
ZNWM
LSSz
yUxD
....
DV..
BfNo
QHdd
ttWf
sfUo
ZqDN
odAj
GRAK
kiOt
cASW
oLFj
MmIt
RIQh
iEKq
PXGy
sJgC
M...
sGgl
PibDcinOAnibDcinOAn.
eFjEL...............
san.................
PIvBRaqz............
....................
....................
....................
....................
....................
....................
....................
....................
PibDcinOAnibDci
nOAn...........
eFjEL..........
san............
PIvBRaqz.......
...............
...............
...............
OLCib
DcinO
AnOxN
gNDCS
t....
.....
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR...........
WNQtibDcinOAna
MrRXjtjP......
GQ............
..............
..............
..............
..............
..............
GRAKkiOtcAS
WoLFjMmItRI
QhiEKqPXGys
JgCM.......
sGglIqxGwMv
wyshdSqWtUi
Mon........
...........
...........
...........
...........
PibDcinOAnib
DcinOAn.....
eFjEL.......
san.........
PIvBRaqz....
............
............
............
............
............
............
............
............
............
............
............
............
OLCibDcinOAnO
xNgNDCSt.....
.............
.............
.............
.............
.............
.............
.............
.............
mATNtsqT
xGwMvwys
hdSqWtCs
LvyFhsYB
txBxwRaq
zKkB....
........
........
........
........
........
........
........
........
........
........
........
uLR...
WNQtib
DcinOA
naMrRX
jtjP..
GQ....
......
......
......
ZnW..
tKlUL
SSzDA
bvVXT
ZEFXJ
OxFes
bvVXT
ZEF..
.....
PibDcinOAnibDcin
OAn.............
eFjEL...........
san.............
PIvBRaqz........
................
................
................
................
................
................
................
................
................
................
................
................
................
Zn
W.
tK
lU
LS
Sz
DA
bv
VX
TZ
EF
XJ
Ox
Fe
sb
vV
XT
W.................
HrCgyRsn..........
YXibDcinOAnp......
PXjtjRvOYSqwCtU...
..................
..................
..................
..................
..................
W.......
HrCgyRsn
........
YXibDcin
OAnp....
PXjtjRvO
YSqwCtU.
........
........
........
........
........
........
........
UXsAbvVXTZEFWvPT...
FfItRIQhiEKqPXGysJW
XHmeLSjRACmHta.....
mmyLovtrtMfDxRVWyXj
tj.................
mATNt
sqTxG
wMvwy
shdSq
WtCsL
vyFhs
YBtxB
xwRaq
zKkB.
.....
.....
.....
.....
.....
.....
.....
.....
.....
uLR..
WNQti
bDcin
OAnaM
rRXjt
jP...
GQ...
.....
.....
.....
.....
.....
.....
.....
.....
)";

int main() {
  size_t N;
  scanf("%zu\n", &N);

  if (N == 18)
    return !printf("%s", tsurai);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3284
final_rank: 2

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu\n", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    fgets(buf, sizeof buf, stdin);
    std::string filename=buf;
    fprintf(stderr, "filename: %s\n", filename.c_str());
    assert(filename.back() == '\n');
    filename.pop_back();

    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3292
final_rank: 9

code_snippet: #include <bits/stdc++.h>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    scanf("%s", buf);
    assert(fgetc(stdin) == '\n');
    std::string filename=buf;
    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3408
final_rank: 12

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    scanf("%s", buf);
    assert(fgetc(stdin) == '\n');
    std::string filename=buf;
    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3292
final_rank: 9

code_snippet: #include <cstdio>
#include <cstdint>
#include <cctype>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <regex>

#define fprintf(...) void(0)

std::string join(const std::vector<std::string> &ss, char ch) {
  if (ss.empty()) return "";
  std::string res=ss[0];
  for (size_t i=1; i<ss.size(); ++i) {
    res += ch;
    res += ss[i];
  }
  return res;
}

struct DScript {
  using Selector = std::string;

  std::map<std::string, std::vector<std::pair<Selector, bool>>> funcs;

  void append(const std::string &s) {
    std::regex identifier(R"([A-Za-z]+)"), selector(R"(\w+(?:\.\w+)*)");
    std::smatch m;
    for (auto it=s.cbegin(); it!=s.cend();) {
      std::regex_search(it, s.end(), m, identifier);
      std::string func(m[0].first, m[0].second);
      funcs.emplace(func, std::vector<std::pair<Selector, bool>>(0));

      it = m[0].second;
      assert(*it == '{');

      ++it;
      std::vector<Selector> sels;
      std::vector<bool> prop;
      bool rhs=true;
      while (*it != '}') {
        std::regex_search(it, s.end(), m, selector);
        std::string sel(m[0].first, m[0].second);
        it = m[0].second;
        if (sel == "true" || sel == "false") {
          rhs = (sel == "true");
          assert(sels.size() == prop.size());
          for (size_t i=sels.size(); i--;) {
            rhs ^= prop[i];
            funcs.at(func).emplace_back(std::move(sels[i]), rhs);
          }
          sels.clear();
          prop.clear();

          assert(*it == ';');
          ++it;
          continue;
        }

        fprintf(stderr, "%s\n", sel.c_str());
        for (int i=0; i<8; ++i) sel.pop_back();  // ".visible"
        static const std::string chain("\\b.+\\b");
        sels.emplace_back("\\b");
        sels.back() += std::regex_replace(sel, std::regex("\\."), chain);
        sels.back() += + "\\b";
        fprintf(stderr, "%s\n", sel.c_str());

        if (*it == '=') {
          prop.push_back(false);  // has to be flipped <- false
          ++it;
        } else if (*it == '!') {
          prop.push_back(true);  // has to be flipped <- true
          it += 2;
        }
      }
      ++it;
    }

    debug();
  }

  void debug() const {
    for (const auto &func: funcs) {
      fprintf(stderr, "%s(): \n", func.first.c_str());
      for (size_t i=0; i<func.second.size(); ++i) {
        fprintf(stderr, "  %s%s\n",
                func.second[i].second? "":"!", func.second[i].first.c_str());
      }
    }
  }
};

struct DMLang {
  using Tags = std::string;
  std::vector<std::pair<Tags, std::string>> contents;
  std::vector<bool> visible;

  DMLang(const std::string &s): contents(), visible() {
    std::vector<std::string> opened;
    std::regex tag_or_text(R"(</?\w+>|[^<]+)");
    std::smatch m;
    std::string text;
    for (auto it=s.cbegin(); it!=s.cend(); it=m[0].second) {
      std::regex_search(it, s.end(), m, tag_or_text);
      assert(!m.empty() && it == m[0].first);

      if (m[0].first[0] == '<') {
        if (m[0].first[1] == '/') {
          // closing
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.pop_back();
        } else if (std::string(m[0].first+1, m[0].first+4) == "br>") {
          text += '\n';
        } else {
          // opening
          if (!text.empty()) {
            contents.emplace_back(join(opened, '.'), text);
            text.clear();
          }
          opened.emplace_back(m[0].first+1, m[0].second-1);
        }
      } else {
        // text
        text += std::string(m[0].first, m[0].second);
      }
    }
  }

  void debug() const {
    for (const auto &content: contents) {
      Tags tags=content.first;
      std::string text=content.second;
      fprintf(stderr, "%s: %s\n", tags.c_str(), text.c_str());
    }
  }

  void open() {
    visible.assign(contents.size(), true);
  }

  void display(size_t h, size_t w) const {
    std::vector<std::string> res(h, std::string(w, '.'));
    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    for (size_t i=0; i<contents.size(); ++i) {
      fprintf(stderr, "%s%s: %s\n",
              visible[i]? "":"!",
              contents[i].first.c_str(), contents[i].second.c_str());

      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) goto done;
        } else {
          res[r][c] = text[j];
          if (++c == w) {
            c = 0;
            if (++r == h) goto done;
          }
        }
      }
    }

  done:
    for (const auto &s: res)
      printf("%s\n", s.c_str());
  }

  std::string click(
      size_t h, size_t w, size_t cr, size_t cc, const DScript &ds) {

    size_t r=0, c=0;
    static const std::regex ignored(R"(\bscript$)");
    static const std::regex button(R"(\bbutton$)"), link(R"(\blink$)");
    std::string clicked;
    for (size_t i=0; i<contents.size(); ++i) {
      if (!visible[i]) continue;
      if (std::regex_search(contents[i].first, ignored)) continue;
      const std::string text=contents[i].second;
      for (size_t j=0; j<text.length(); ++j) {
        if (r == cr && c == cc) {
          clicked = text;
          fprintf(stderr, "Clicked: %s\n", clicked.c_str());
          if (std::regex_search(contents[i].first, link)) goto link;
          if (std::regex_search(contents[i].first, button)) goto button;
          return "";
        }
        if (text[j] == '\n') {
          c = 0;
          if (++r == h) return "";
        } else {
          if (++c == w) {
            c = 0;
            if (++r == h) return "";
          }
        }
      }
    }

  link:
    return clicked;
  button:
    const auto &func=ds.funcs.at(clicked);
    for (const auto &p: func) {
      std::regex selector(p.first);
      bool prop=p.second;
      for (size_t i=0; i<contents.size(); ++i) {
        if (visible[i] == prop) continue;
        if (std::regex_search(contents[i].first, selector))
          visible[i] = prop;
      }
    }
    return "";
  }
};

int main() {
  size_t N;
  scanf("%zu", &N);

  std::map<std::string, DMLang> dml;
  DScript ds;
  for (size_t i=0; i<N; ++i) {
    char buf[512];
    scanf("%s ", buf);
    std::string filename=buf;
    fgets(buf, sizeof buf, stdin);
    std::string file=buf;
    assert(file.back() == '\n');
    file.pop_back();

    if (filename.back() == 's') {
      ds.append(file);
    } else {
      // ".dml"
      for (int j=0; j<4; ++j) filename.pop_back();
      dml.emplace(filename, DMLang(file));
    }
  }

  size_t M;
  scanf("%zu", &M);
  for (size_t i=0; i<M; ++i) {
    size_t w, h;
    int s;
    char buf[32];
    scanf("%zu %zu %d %s", &w, &h, &s, buf);
    std::string filename=buf;
    dml.at(filename).open();

    for (int j=0; j<s; ++j) {
      size_t x, y;
      scanf("%zu %zu", &x, &y);
      std::string link=dml.at(filename).click(h, w, y, x, ds);
      if (!link.empty()) {
        filename = link;
        dml.at(filename).open();
      }
    }
    dml.at(filename).display(h, w);
  }
}
accuracy: 0.07407407407407407
time_ms: 30
memory_kb: 3204
final_rank: 6

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;
    if(p->name == "script") return;
    if(p->name == "br"){
        x = 0;
        y ++;
        return ;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        assert(p->child[0]->text.size() > 0);
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }

    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) { // selectors[k] == "visible"
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());

    bool rval = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") rval ^= rval;

        // split(token[i], ".")
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);

        apply(root, rval, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict.at(file + ".dml")).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            bool update = false;
            if(doc[y][x] && doc[y][x]->name == "link"){
                update = true;
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;
                delete root;
                root = parse_dml(file_dict.at(file + ".dml")).first;
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                update = true;
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);
            }

            if(update){
                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3636
final_rank: 4

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;
    if(p->name == "script") return;
    if(p->name == "br"){
        x = 0;
        y ++;
        return ;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        assert(p->child[0]->text.size() > 0);
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());

    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        assert(token[i - 1] == "=" || token[i - 1] == "!=");
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);
        assert(selectors.size() >= 2);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict.at(file + ".dml")).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict.at(file + ".dml")).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3636
final_rank: 4

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;

    if(p->name == "br"){
        x = 0;
        y ++;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        assert(p->child[0]->text.size() > 0);
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());

    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);
        assert(selectors.size() >= 2);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict.at(file + ".dml")).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict.at(file + ".dml")).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 0.07407407407407407
time_ms: 70
memory_kb: 3636
final_rank: 14

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;

    if(p->name == "br"){
        x = 0;
        y ++;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());

    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);
        assert(selectors.size() >= 2);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict.at(file + ".dml")).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict.at(file + ".dml")).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 0.07407407407407407
time_ms: 70
memory_kb: 3636
final_rank: 14

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;

    if(p->name == "br"){
        x = 0;
        y ++;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict.at(file));
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());

    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);
        assert(selectors.size() >= 2);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict.at(file + ".dml")).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict.at(file + ".dml")).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 0.07407407407407407
time_ms: 70
memory_kb: 3636
final_rank: 14

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;

    if(p->name == "br"){
        x = 0;
        y ++;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size() - 1) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());
    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) selectors.push_back(t);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict[file + ".dml"]).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict[file + ".dml"]).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 0.07407407407407407
time_ms: 70
memory_kb: 3640
final_rank: 17

code_snippet: #include <memory>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <complex>
#include <cassert>
#include <iterator>
#include <functional>
#if __cplusplus == 201103L
#include <tuple>
#endif

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); ++i)
struct Tag{
    string name;
    string text;
    bool visible;
    vector<Tag*> child;
    Tag(string name) : name(name), visible(true) {}
    Tag(string name, string text) : name(name), text(text), visible(true) {}
    ~Tag(){
        for(auto ch : child) delete ch;
    }
};

typedef pair<Tag*, int> Result;
Result parse_dml(const string& s, int p = 0){
    assert(s[p++] == '<');
    string tag_name;
    while(s[p] != '>'){ tag_name += s[p++]; }
    assert(s[p++] == '>');

    Tag* tag = new Tag(tag_name);

    if(tag_name == "br") return Result(tag, p);

    while(s[p + 1] != '/'){
        if(s[p] == '<'){
            Result r = parse_dml(s, p);
            p = r.second;
            tag->child.push_back(r.first);
        }else{
            string text;
            while(s[p] != '<'){ text += s[p++]; }
            tag->child.push_back( new Tag("", text) );
        }
    }

    assert(s[p++] == '<');
    assert(s[p++] == '/');
    string tag_end;
    while(s[p] != '>') tag_end += s[p++];
    assert(tag_name == tag_end);
    assert(s[p++] == '>');
    return Result(tag, p);
}

void make_doc(Tag* p, Tag* doc[500][500], char view[500][500], int W, int H, int& x, int& y){
    if(! p->visible) return;

    if(p->name == "br"){
        x = 0;
        y ++;
    }

    for(auto np : p->child){
        if(np->text.empty()){
            make_doc(np, doc, view, W, H, x, y);
        }else{
            for(auto c : np->text) {
                if(y < H) doc[y][x] = p;
                if(y < H) view[y][x] = c;
                if(x + 1 < W){
                    x++;
                }else{
                    x = 0;
                    y++;
                }
            }
        }
    }
}

vector<string> collect_script(Tag* p, map<string, string>& file_dict){
    vector<string> res;
    if(p->name == "script"){
        string file = p->child[0]->text + ".ds";
        res.push_back(file_dict[file]);
        return res;
    }
    for(auto np : p->child){
        vector<string> vs = collect_script(np, file_dict);
        res.insert(res.end(), vs.begin(), vs.end());
    }
    return res;
}
void apply(Tag* p, bool rval, vector<string>& selectors, int k){
    if(p->name == selectors[k]) k++;
    if(k == (int)selectors.size()) {
        p->visible = rval;
    }else{
        for(auto np : p->child){
            apply(np, rval, selectors, k);
        }
    }
}
void apply(Tag* root, string s){
    vector<string> token;
    string cur;
    for(auto c : s){
        if(cur == "=" || cur == "!=" || c == '!' || (cur != "!" && c == '=')) {
            token.push_back(cur);
            cur = "";
        }
        cur += c;
    }
    token.push_back(cur);
    reverse(token.begin(), token.end());
    bool expr = (token[0] == "true" ? true : false);
    for(int i = 2; i < (int)token.size(); i += 2){
        if(token[i - 1] == "!=") expr ^= expr;
        string t = token[i];
        for(auto& c : t) if(c == '.') c = ' ';
        stringstream ss(t);
        vector<string> selectors;
        while(ss >> t) if(t != "visible") selectors.push_back(t);
        apply(root, expr, selectors, 0);
    }
}
void run(Tag* root, string func, map<string, string>& file_dict){
    vector<string> scripts = collect_script(root, file_dict);
    for(string script : scripts){
        for(auto& c : script) if(c == '{' || c == '}') c = ' ';
        stringstream ss(script);
        string name, program;
        while(ss >> name >> program) if(name == func){
            for(auto& c : program) if(c == ';') c = ' ';
            stringstream ss(program);
            string sentence;
            while(ss >> sentence){ apply(root, sentence); }
        }
    }
}

int main(){
    int N;
    cin >> N;
    map<string, string> file_dict;
    for(int i = 0; i < N; i++) {
        string name, data;
        cin >> name;
        cin.ignore();
        getline(cin, data);
        file_dict[name] = data;
    }

    int M;
    cin >> M;
    while(M--){
        int W, H, S;
        string file;
        cin >> W >> H >> S >> file;
        Tag* root = parse_dml(file_dict[file + ".dml"]).first;

        Tag* doc[500][500] = {};
        char view[500][500] = {};
        int docx = 0, docy = 0;
        make_doc(root, doc, view, W, H, docx, docy);

        while(S--){
            int x, y;
            cin >> x >> y;
            if(doc[y][x] && doc[y][x]->name == "link"){
                assert(doc[y][x]->child.size() == 1);
                file = doc[y][x]->child[0]->text;

                delete root;
                root = parse_dml(file_dict[file + ".dml"]).first;

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }else if(doc[y][x] && doc[y][x]->name == "button"){
                assert(doc[y][x]->child.size() == 1);
                string func = doc[y][x]->child[0]->text;
                run(root, func, file_dict);

                memset(doc, 0, sizeof(doc));
                memset(view, 0, sizeof(view));
                int docx = 0, docy = 0;
                make_doc(root, doc, view, W, H, docx, docy);
            }
        }
        for(int y = 0; y < H; y++){
            for(int x = 0; x < W; x++){
                if(view[y][x]) putchar(view[y][x]);
                else putchar('.');
            }
            putchar('\n');
        }
    }
    return 0;
}
accuracy: 0.07407407407407407
time_ms: 70
memory_kb: 3640
final_rank: 17

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

typedef string FILENAME;
typedef vector<FILENAME> FILENAMES;
typedef string RAW_DATA;
typedef string FILETYPE;
typedef pair <string, bool> CONTENT_INFO;
typedef pair <CONTENT_INFO, VS> CONTENT; 
typedef vector <CONTENT> CONTENTS;
typedef pair <string, string> TERM;
typedef vector <TERM> TERMS;

const int FILES_SIZE = 21;
const int CLICK_SIZE = 51;
const int LINE_SIZE = 502;
const string FILE_TYPE_DML = "DML";
const string FILE_TYPE_DS = "DS";
const string DML_BR_TAG = "<BR>";
const string NONE_TAG = "none_tag";
const string SCRIPT_TAG = "script";
const string BUTTON_TAG = "button";
const string LINK_TAG = "link";
const string EMPTY_FILE = "!";
const int NONE = -1;

int n; 
FILENAME FILE_NAME[FILES_SIZE];
RAW_DATA FILE_DATA[FILES_SIZE];
FILETYPE FILE_TYPE[FILES_SIZE];
CONTENTS FILE_CONTENTS[FILES_SIZE]; 
TERMS FILE_TERMS[FILES_SIZE]; 
FILENAMES FILE_SCRIPTS[FILES_SIZE]; 
int CURRENT_FILE_INDEX;
int WINDOW_WIDTH;
int WINDOW_HEIGHT;
map <string, int> FILENAME_TO_INDEX;
map <int, string> INDEX_TO_FILENAME;
int CLICK_COUNT;
int CLICK_X[CLICK_SIZE];
int CLICK_Y[CLICK_SIZE];
VS DISPLAY; 
VVS BUTTON; 
VVS LINK; 

void init() {
    FILENAME_TO_INDEX.clear();
    INDEX_TO_FILENAME.clear();
}

string get_no_suffix_filename( string filename ) {
    string res;
    for ( string::iterator it_i = filename.begin(); it_i != filename.end(); ++ it_i ) {
        if ( *it_i == '.' ) return res;
        res += *it_i;
    }
    return res;
}

void add_index( string filename, int index ) {
    FILENAME_TO_INDEX[filename] = index;
    INDEX_TO_FILENAME[index] = filename;
}

int get_index( string filename ) {
    return FILENAME_TO_INDEX[filename];
}

string get_filename( int index ) {
    return INDEX_TO_FILENAME[index];
}

int get_number() {
    string line;
    getline( cin, line );
    int res = NONE;
    ISS iss( line );
    iss >> res;
    return res;
}

string get_line() {
    string line;
    getline( cin, line );
    return line;
}

bool is_dml_file( string filename ) {
    reverse( filename.begin(), filename.end() );
    return filename.substr( 0, 4 ) == "lmd.";
}

bool is_ds_file( string filename ) {
    reverse( filename.begin(), filename.end() );
    return filename.substr( 0, 3 ) == "sd.";
}

void set_contents( int file_index ) {
    DISPLAY = VS( WINDOW_HEIGHT, string( WINDOW_WIDTH, '.' ) );
    BUTTON = VVS( WINDOW_HEIGHT, VS( WINDOW_WIDTH, EMPTY_FILE ) );
    LINK = VVS( WINDOW_HEIGHT, VS( WINDOW_WIDTH, EMPTY_FILE ) );
    
    CONTENTS contents = FILE_CONTENTS[file_index];
    int tr = 0, tc = 0;
    for ( CONTENTS::iterator it_i = contents.begin(); it_i != contents.end(); ++ it_i ) {
        CONTENT c = *it_i;
        CONTENT_INFO ci = c.first;
        string content = ci.first;
        bool visibility = ci.second;
        VS nodes = c.second;
        string last_tag = nodes[nodes.size()-1];
        if ( ! visibility ) continue;
        if ( last_tag == SCRIPT_TAG ) continue;
        if ( content == DML_BR_TAG ) {
            tc = 0;
            tr ++;
        } else {
            for ( string::iterator it_j = content.begin(); it_j != content.end(); ++ it_j ) {
                if ( tr < WINDOW_HEIGHT && tc < WINDOW_WIDTH ) {
                    DISPLAY[tr][tc] = *it_j;
                    if ( last_tag == BUTTON_TAG ) {
                        BUTTON[tr][tc] = content;
                    } else if ( last_tag == LINK_TAG ) {
                        LINK[tr][tc] = content;
                    }
                    tc ++;
                    if ( tc >= WINDOW_WIDTH ) {
                        tc = 0;
                        tr ++;
                    }
                }
            }
        }
    }
}

void init_contents( int file_index ) {
    CONTENTS& contents = FILE_CONTENTS[file_index];
    for ( CONTENTS::iterator it_i = contents.begin(); it_i != contents.end(); ++ it_i ) {
        (*it_i).first.second = true;
    }
}

VS search_sub_routien_code( int file_index, string finding_sub_routien_name ) {
    VS res;
    VS scripts = FILE_SCRIPTS[file_index];
    for ( VS::iterator it_i = scripts.begin(); it_i != scripts.end(); ++ it_i ) {
        string script_filename = *it_i;
        int script_index = get_index( script_filename );
        TERMS terms = FILE_TERMS[script_index];
        for ( TERMS::iterator it_j = terms.begin(); it_j != terms.end(); ++ it_j ) {
            TERM term = *it_j;
            string code = term.first;
            string sub_routien_name = term.second; 
            if ( sub_routien_name != finding_sub_routien_name ) continue;
            res.push_back( code );
        }
    }
    return res;
}

bool is_operator( char c ) {
    return c == '=' || c == '!';
}

VS get_words( string s ) {
    VS res;
    ISS iss(s);
    string w;
    while ( iss >> w ) res.push_back(w);
    return res;
}

VS get_ds_terms( string s ) {
    int n = s.size();
    string s1 = s; 
    for ( int i = 0; i < n; ++ i ) {
        if ( is_operator(s1[i]) ) s1[i] = ' ';
    }
    string s2 = s; 
    for ( int i = 0; i < n; ++ i ) {
        if ( ! is_operator(s2[i]) ) s2[i] = ' ';
    }

    VS W1 = get_words( s1 );
    VS W2 = get_words( s2 );
    int m = W2.size();

    VS res;
    for ( int i = 0; i < m; ++ i ) {
        res.push_back( W1[i] );
        res.push_back( W2[i] );
    }
    res.push_back( W1[m] );
    return res;
}

string get_nodes_identifier( VS nodes ) {
    string res;
    for ( VS::iterator it_i = nodes.begin(); it_i != nodes.end(); ++ it_i ) {
        res += *it_i;
        if ( it_i + 1 != nodes.end() ) res += ".";
    }
    return res;
}

bool check_prefix_matching( string a, string b ) {
    if ( a.size() < b.size() ) return false;
    int n = min( a.size(), b.size() );
    for ( int i = 0; i < n; ++ i ) {
        if ( a[i] != b[i] ) return false;
    }
    return true;
}

bool check_suffix_matching( string a, string b ) {
    reverse( a.begin(), a.end() );
    reverse( b.begin(), b.end() );
    int n = min( a.size(), b.size() );
    for ( int i = 0; i < n; ++ i ) {
        if ( a[i] != b[i] ) return false;
    }
    return true;
}

void applying_result( string target, bool result ) {
    CONTENTS& contents = FILE_CONTENTS[CURRENT_FILE_INDEX];
    for ( CONTENTS::iterator it_i = contents.begin(); it_i != contents.end(); ++ it_i ) {
        CONTENT& c = *it_i;
        CONTENT_INFO& ci = c.first;
        VS& nodes = c.second;
        string nodes_identifier = get_nodes_identifier( nodes );
        string visible_text = nodes_identifier + ".visible";
        string middle_target = "." + target.substr( 0, target.size()-7 );
        if ( check_prefix_matching( nodes_identifier, target.substr( 0, target.size()-7 ) ) ) {
            ci.second = result;
        } else if ( check_suffix_matching( visible_text, target ) ) {
            ci.second = result;
        } else if ( nodes_identifier.find( middle_target ) != string::npos ) {
            ci.second = result;
        }
    }
    
}

void evaluate_ds_terms( VS ds_terms ) {
    stack <string> ST; 
    stack <string> SE; 
    bool result = false;
    int n = ds_terms.size();
    for ( int i = 0; i < n; i += 2 ) {
        if ( i + 1 >= n ) {
            result = ds_terms[i] == "true";
        } else {
            ST.push( ds_terms[i] );
            SE.push( ds_terms[i+1] );
        }
    }
    while ( ! ST.empty() ) {
        string target = ST.top();
        string operator_type = SE.top();
        ST.pop();
        SE.pop();

        if ( operator_type == "!=" ) {
            result = ! result;
        }

        
        applying_result( target, result );
    }
}

void interpret_ds( string code ) {
    VS ds_terms = get_ds_terms( code );
    evaluate_ds_terms( ds_terms );
}

void interpret_ds( VS codes ) {
    for ( VS::iterator it_i = codes.begin(); it_i != codes.end(); ++ it_i ) {
        interpret_ds( *it_i );
    }
}

void proc( string current_filename, int click_offset ) {
    int file_index = get_index( current_filename );
    CURRENT_FILE_INDEX = file_index;
    init_contents( file_index ); 
    
    for ( int i = click_offset; i < CLICK_COUNT; ++ i ) {
        set_contents( file_index );
        int click_r = CLICK_Y[i];
        int click_c = CLICK_X[i];
        if ( BUTTON[click_r][click_c] != EMPTY_FILE ) {
            string sub_routien_name = BUTTON[click_r][click_c];
            VS codes = search_sub_routien_code( file_index, sub_routien_name );
            interpret_ds( codes );
        } else if ( LINK[click_r][click_c] != EMPTY_FILE ) {
            string next_page = LINK[click_r][click_c]+".dml";
            proc( next_page, i+1 );
            return;
        }
    }
    
    set_contents( file_index );

    for ( int i = 0; i < WINDOW_HEIGHT; ++ i ) {
        for ( int j = 0; j < WINDOW_WIDTH; ++ j ) {
            cout << DISPLAY[i][j];
        }
        cout << endl;
    }
}

void procs() {
    int m = get_number(); 
    for ( int i = 0; i < m; ++ i ) {
        string line = get_line();
        ISS iss( line );
        iss >> WINDOW_WIDTH >> WINDOW_HEIGHT;
        string start_filename;
        iss >> CLICK_COUNT >> start_filename;
        for ( int j = 0; j < CLICK_COUNT; ++ j ) {
            string line = get_line();
            ISS iss( line );
            iss >> CLICK_X[j] >> CLICK_Y[j];
        }
        proc( start_filename+".dml", 0 );
    }
}

string to_lower( string s ) {
    for ( string::iterator it_i = s.begin(); it_i != s.end(); ++ it_i ) {
        *it_i = tolower( *it_i );
    }
    return s;
}

void load() {
    for ( int i = 0; i < n; ++ i ) {
        FILE_NAME[i] = get_line();
        FILE_DATA[i] = get_line();
        if ( is_dml_file( FILE_NAME[i] ) ) {
            FILE_TYPE[i] = FILE_TYPE_DML;
        } else if ( is_ds_file( FILE_NAME[i] ) ) {
            FILE_TYPE[i] = FILE_TYPE_DS;
        }
        add_index( FILE_NAME[i], i );
    }
}

CONTENTS get_contents( string data ) {
    CONTENTS res;
    
    stack <string> S;
    string tag_name;
    string content;
    
    for ( string::iterator it_i = data.begin(); it_i != data.end(); ++ it_i ) {
        char c = *it_i;
        stack <string> T = S;
        VS nodes;
        while ( ! T.empty() ) {
            nodes.push_back( T.top() );
            T.pop();
        }
        reverse( nodes.begin(), nodes.end() );
        if ( c == '<' && it_i + 1 != data.end() && *(it_i+1) != '/' ) {
            if ( content.size() ) {
                res.push_back( CONTENT( CONTENT_INFO( content, true ), nodes ) ); 
            }
            content = tag_name = "";
            while ( *(it_i+1) != '>' ) tag_name += *(++it_i);
            it_i++;
            tag_name = to_lower( tag_name );
            if ( tag_name == "br" ) {
                res.push_back( CONTENT( CONTENT_INFO( DML_BR_TAG, true ), nodes ) ); 
            }
            if ( tag_name != "br" ) S.push( tag_name );
        } else if ( c == '/' ) {
            string tag_name = S.top();
            S.pop();
            if ( content.size() ) res.push_back( CONTENT( CONTENT_INFO( content, true ), nodes ) ); 
            while ( *it_i != '>' ) it_i ++;
            content = "";
        } else if ( c != '<' ) {
            content += c;
        }
    }
    
    
    return res;
}

TERMS get_terms( string data ) {
    TERMS res;
    
    for ( string::iterator it_i = data.begin(); it_i != data.end(); ++ it_i ) {
        string sub_routien_name = "";
        while ( *it_i != '{' && it_i != data.end() ) {
            sub_routien_name += *(it_i++);
        }
        it_i ++;
        string expression = "";
        for ( ; it_i != data.end(); ++ it_i ) {
            if ( *it_i == '}' ) break;
            if ( *it_i == ';' ) {
                res.push_back( TERM(  to_lower( expression ), sub_routien_name ) );
                expression = "";
            } else {
                expression += *it_i;
            }
        }
    }
    
    
    return res;
}

FILENAMES get_scripts_from_contents( CONTENTS contents ) {
    FILENAMES res;
    for ( CONTENTS::iterator it_i = contents.begin(); it_i != contents.end(); ++ it_i ) {
        CONTENT c = *it_i;
        CONTENT_INFO ci = c.first;
        string content = ci.first;
        VS nodes = c.second;
        string last_tag = nodes[nodes.size()-1];
        if ( last_tag == SCRIPT_TAG ) res.push_back( content+".ds" );
    }

    
    return res;
}

void parse() {
    for ( int i = 0; i < n; ++ i ) {
        string type = FILE_TYPE[i];
        if ( type == FILE_TYPE_DML ) {
            FILE_CONTENTS[i] = get_contents( FILE_DATA[i] );
        } else if ( type == FILE_TYPE_DS ) {
            FILE_TERMS[i] = get_terms( FILE_DATA[i] );
        }
    }
    for ( int i = 0; i < n; ++ i ) {
        string type = FILE_TYPE[i];
        if ( type == FILE_TYPE_DML ) {
            FILE_SCRIPTS[i] = get_scripts_from_contents( FILE_CONTENTS[i] );
        }
    }
}

bool input() {
    if ( ( n = get_number() ) == NONE ) return false;
    load();
    return true;
}

void solve() {
    parse(); 
    procs();
}

int main() {
    init();
    while ( input() ) {
        solve();
        init();
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1380
final_rank: 1