Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Hack it and have it ;) //
- /*author* Priyanshu Shrivastav (from IIT Palakkad) *
- * *_ __ ___ _ ______ ___ _ ____ ___ ___| |_ *
- * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
- * | | | | | | | | (_| (_) | | | \ V /| | (__| |_ *
- * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
- When I wrote this, only God and I understood what I was doing
- ** * * * * * * * Now, only God knows!* * * * * * */
- #include <bits/stdc++.h> /*{{{*/
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #ifndef CONVICTION
- #pragma GCC optimize ("Ofast")
- #pragma GCC optimize ("unroll-loops")
- #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
- #endif
- #define IOS ios_base::sync_with_stdio(false); cin.tie (nullptr)
- #define PREC cout.precision (10); cout << fixed
- #define X first
- #define Y second
- #define sz(x) (int)x.size()
- #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
- #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
- #define bcnt(x) __builtin_popcount(x)
- #define bcntll(x) __builtin_popcountll(x)
- #define bg(x) " [ " << #x << " : " << (x) << " ] "
- #define un(x) sort(x.begin(), x.end()), \
- x.erase(unique(x.begin(), x.end()), x.end())
- using ll = long long;
- using ull = unsigned long long;
- using ff = long double;
- using pii = pair<int,int>;
- using pil = pair<int,ll>;
- using pll = pair<ll,ll>;
- using vi = vector <int>;
- using vl = vector<ll>;
- using vvi = vector <vi>;
- using vvl = vector <vl>;
- using vp = vector <pii>;
- using vvp = vector <vp>;
- typedef tree
- < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
- ordered_set;
- struct chash {
- int operator () (pii x) const { return x.X*31 + x.Y; }
- };
- gp_hash_table <pii, int, chash> gmp;
- #if __cplusplus > 201103L
- seed_seq seq{
- (uint64_t) chrono::duration_cast<chrono::nanoseconds>
- (chrono::high_resolution_clock::now().time_since_epoch()).count(),
- (uint64_t) __builtin_ia32_rdtsc(),
- (uint64_t) (uintptr_t) make_unique<char>().get()
- };
- mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
- #else
- auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
- mt19937 rng(seed);
- #endif
- #ifdef CONVICTION
- void __print(int x) {cerr << x;}
- void __print(long x) {cerr << x;}
- void __print(long long x) {cerr << x;}
- void __print(unsigned x) {cerr << x;}
- void __print(unsigned long x) {cerr << x;}
- void __print(unsigned long long x) {cerr << x;}
- void __print(float x) {cerr << x;}
- void __print(double x) {cerr << x;}
- void __print(long double x) {cerr << x;}
- void __print(char x) {cerr << '\'' << x << '\'';}
- void __print(const char *x) {cerr << '\"' << x << '\"';}
- void __print(const string &x) {cerr << '\"' << x << '\"';}
- void __print(bool x) {cerr << (x ? "true" : "false");}
- template<typename T, typename V>
- void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
- template<typename T>
- void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
- void _print() {cerr << "]" << endl;}
- template <typename T, typename... V>
- void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
- #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
- #else
- #define debug(x...)
- #endif
- // __ __
- // (**) (**)
- // IIII IIII
- // #### ####
- // HHHH Madness comes, and madness goes HHHH
- // HHHH An insane place, with insane moves HHHH
- // #### Battles without, for battles within ####
- // ___IIII___ Where evil lives, ___IIII___
- // .-`_._"**"_._`-. and evil rules .-`_._"**"_._`-.
- // |/`` .`\/`. ``\| Breaking them up, |/`` .`\/`. ``\|
- // ` } { ' just breaking them in ` } { '
- // ) () ( Quickest way out, quickest way wins ) () (
- // ( :: ) Never disclose, never betray ( :: )
- // | :: | Cease to speak or cease to breath | :: |
- // | )( | And when you kill a man, | )( |
- // | || | you're a murderer | || |
- // | || | Kill many | || |
- // | || | and you're a conqueror | || |
- // | || | Kill them all ... Ooh.. | || |
- // | || | Oh you're a God! | || |
- // ( () ) -Megadeth ( () )
- // \ / \ /
- // \/ \/
- /*}}}*/
- /*****************************************************************************/
- //Don’t practice until you get it right. Practice until you can’t get it wrong
- void preproc() {
- }
- class String {
- public:
- string s;
- String (string t) { s = t; }
- bool operator < (const String &o) const {
- if (s == "x") return false;
- else if (o.s == "x") return true;
- else return s.size() < o.s.size() || (s.size() == o.s.size() && s < o.s);
- }
- };
- vector <int> pref_func (const string &s) { // O(|s|)
- int n = (int)s.size();
- vector <int> pi(n);
- pi[0] = 0;
- int sz = (int)s.size();
- for (int i = 1; i < sz; ++i) {
- int len_j = pi[i-1];
- int j = len_j - 1;
- while (len_j > 0 && s[j + 1] != s[i]) {
- len_j = pi[j];
- j = len_j - 1;
- }
- if (s[len_j] == s[i])
- ++len_j;
- pi[i] = len_j;
- }
- return pi;
- }
- void solve() {
- int n;
- cin >> n;
- vector <String> dp(1 << n, String("x"));
- vector <string> inp(n);
- for (int i = 0; i < n; ++i)
- cin >> inp[i];
- auto get = [&] (string t, string s) -> String {
- int mx = (int)s.size() + (int)t.size();
- int len = mx + 1, sz = (int)s.size(), tz = (int)t.size();
- // prefix match of t with suffix of s
- vector <int> pi_f = pref_func(s + "#" + t);
- //suffix match of t with prefix of s
- reverse(t.begin(), t.end());
- reverse(s.begin(), s.end());
- vector <int> pi_b = pref_func(s + "#" + t);
- reverse(t.begin(), t.end());
- reverse(s.begin(), s.end());
- for (int i = (int)s.size() + 1; i < len; ++i) {
- if (pi_f[i] == sz || pi_b[i] == sz) return String(t);
- }
- int pref_max = 0, suff_max = 0;
- for (int i = max(sz + 1, len - sz + 1); i < len; ++i) {
- pref_max = max(pref_max, pi_f[i]);
- suff_max = max(suff_max, pi_b[i]);
- }
- if (pref_max > suff_max) {
- return String(t + s.substr(pref_max, sz - pref_max));
- }
- else {
- return String(s.substr(0, sz - suff_max) + t);
- }
- };
- for (int mask = 1; mask < (1 << n); ++mask) {
- if (__builtin_popcount(mask) == 1) {
- for (int i = 0; i < n; ++i)
- if (mask & (1 << i)){
- dp[mask].s = inp[i];
- break;
- }
- }
- else {
- for (int i = 0; i < n; ++i) {
- int oldmask = mask ^ (1 << i);
- if (mask & (1 << i)) {
- dp[mask] = min(dp[mask], get(dp[oldmask].s, inp[i]));
- }
- }
- }
- // debug(mask, dp[mask].s);
- }
- cout << dp[(1 << n) - 1].s << '\n';
- }
- signed main() {
- IOS; PREC;
- preproc();
- int tc = 1;
- cin >> tc;
- for (int Tt = 1; Tt <= tc; ++Tt) {
- cout << "Scenario #" << Tt << ":\n";
- solve();
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment