Tanya and Password

While dad was at work, a little girl Tanya decided to play with dad’s password to his secret database. Dad’s password is a string consisting of n + 2 characters. She has written all the possible n three-letter continuous substrings of the password on pieces of paper, one for each piece of paper, and threw the password out. Each three-letter substring was written the number of times it occurred in the password. Thus, Tanya ended up with n pieces of paper.

Then Tanya realized that dad will be upset to learn about her game and decided to restore the password or at least any string corresponding to the final set of three-letter strings. You have to help her in this difficult task. We know that dad’s password consisted of lowercase and uppercase letters of the Latin alphabet and digits. Uppercase and lowercase letters of the Latin alphabet are considered distinct.

Input

The first line contains integer n (1 ≤ n ≤ 2·105), the number of three-letter substrings Tanya got.

Next n lines contain three letters each, forming the substring of dad’s password. Each character in the input is a lowercase or uppercase Latin letter or a digit.

Output

If Tanya made a mistake somewhere during the game and the strings that correspond to the given set of substrings don’t exist, print “NO”.

If it is possible to restore the string that corresponds to given set of substrings, print “YES”, and then print any suitable password option.

Examples

input

5
aca
aba
aba
cab
bac

output

YES
abacaba

input

4
abc
bCb
cb1
b13

output

NO

input

7
aaa
aaa
aaa
aaa
aaa
aaa
aaa

output

YES
aaaaaaaaa

Solution:

#include <bits/stdc++.h>

using namespace std;

template <typename T>
class graph {
public:
struct edge {
int from;
int to;
T cost;
};

vector<edge> edges;
vector< vector<int> > g;
int n;

function<bool(int)> ignore;

graph(int _n) : n(_n) {
g.resize(n);
ignore = nullptr;
}

virtual int add(int from, int to, T cost) = 0;

virtual void set_ignore_edge_rule(const function<bool(int)> &f) {
ignore = f;
}

virtual void reset_ignore_edge_rule() {
ignore = nullptr;
}
};

template <typename T>
class digraph : public graph<T> {
public:
using graph<T>::edges;
using graph<T>::g;
using graph<T>::n;
using graph<T>::ignore;

digraph(int _n) : graph<T>(_n) {
}

int add(int from, int to, T cost = 1) {
assert(0 <= from && from < n && 0 <= to && to < n);
    int id = (int) edges.size();
    g[from].push_back(id);
    edges.push_back({from, to, cost});
    return id;
  }

  digraph<T> reverse() {
digraph<T> rev(n);
for (auto &e : edges) {
rev.add(e.to, e.from, e.cost);
}
if (ignore != nullptr) {
rev.set_ignore_edge_rule([&](int id) {
return ignore(id);
});
}
return rev;
}
};

template <typename T>
vector<int> find_eulerian_path(const graph<T> &g, int &root) {
// in_deg and out_deg are fake for undigraph!
vector<int> in_deg(g.n, 0);
vector<int> out_deg(g.n, 0);
int cnt_edges = 0;
for (int id = 0; id < (int) g.edges.size(); id++) {
    if (g.ignore != nullptr && g.ignore(id)) {
      continue;
    }
    cnt_edges++;
    auto &e = g.edges[id];
    out_deg[e.from]++;
    in_deg[e.to]++;
  }
  root = -1;
  int odd = 0;
  for (int i = 0; i < g.n; i++) {
    if ((in_deg[i] + out_deg[i]) % 2 == 1) {
      odd++;
      if (root == -1 || out_deg[i] - in_deg[i] > out_deg[root] - in_deg[root]) {
root = i;
}
}
}
if (odd > 2) {
root = -1;
return vector<int>();
}
if (root == -1) {
root = 0;
while (root < g.n && in_deg[root] + out_deg[root] == 0) {
      root++;
    }
    if (root == g.n) {
      // but actually an empty path exists...
      root = 0;
      return vector<int>();
}
}
vector<bool> used(g.edges.size(), false);
vector<int> ptr(g.n, 0);
vector<int> balance(g.n, 0);
vector<int> res(cnt_edges);
int stack_ptr = 0;
int write_ptr = cnt_edges;
int v = root;
while (true) {
bool found = false;
while (ptr[v] < (int) g.g[v].size()) {
      int id = g.g[v][ptr[v]++];
      if (used[id] || (g.ignore != nullptr && g.ignore(id))) {
        continue;
      }
      used[id] = true;
      res[stack_ptr++] = id;
      auto &e = g.edges[id];
      balance[v]++;
      v ^= e.from ^ e.to;
      balance[v]--;
      found = true;
      break;
    }
    if (!found) {
      if (stack_ptr == 0) {
        break;
      }
      int id = res[--stack_ptr];
      res[--write_ptr] = id;
      auto &e = g.edges[id];
      v ^= e.from ^ e.to;
    }
  }
  int disbalance = 0;
  for (int i = 0; i < g.n; i++) {
    disbalance += abs(balance[i]);
  }
  if (write_ptr != 0 || disbalance > 2) {
root = -1;
return vector<int>();
}
return res;
// root == -1 if there is no path
// (or res.empty(), but this is also true when there are no edges)
}

int main() {
int n;
scanf("%d", &n);
const int ALPHA = 126;
digraph<int> g(ALPHA * ALPHA);
char foo[7];
for (int i = 0; i < n; i++) {
    scanf("%s", foo);
    int x = (int) (foo[0] - ' ') * ALPHA + (int) (foo[1] - ' ');
    int y = (int) (foo[1] - ' ') * ALPHA + (int) (foo[2] - ' ');
    g.add(x, y);
  }
  int root;
  vector<int> res = find_eulerian_path(g, root);
if (root == -1) {
puts("NO");
} else {
puts("YES");
putchar(root / ALPHA + ' ');
putchar(root % ALPHA + ' ');
for (int id : res) {
int v = g.edges[id].to;
putchar(v % ALPHA + ' ');
}
putchar('\n');
}
return 0;
}