Submission #594942

#TimeUsernameProblemLanguageResultExecution timeMemory
594942thezomb1eThree Friends (BOI14_friends)C++17
100 / 100
74 ms44424 KiB
//thatsramen
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
 
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
 
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;
 
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
 
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
    unsyncIO(); setPrec();
    if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
 
//#define TEST_CASES
 
void solve() {
    int n;
    cin >> n;
    string init;
    cin >> init;
    if (n % 2 == 0) {
        cout << "NOT POSSIBLE";
        return;
    }
    int len = n / 2;
    auto check = [&](string s, string t) -> bool {
        vector<int> left(n + 10, 0), right(n + 10, 0);
        vector<int> pre(n + 10, 0), suf(n + 10, 0);
        int s_size = s.size(), t_size = t.size();
        {
            string nw = s; nw += '#'; nw += t;
            int m = nw.size();
            for (int i = 1; i < m; i++) {
                int j = pre[i - 1];
                while (j > 0 && nw[j] != nw[i]) {
                    j = pre[j - 1];
                }
                if (nw[j] == nw[i]) {
                    pre[i] = j + 1;
                } else {
                    pre[i] = 0;
                }
                if (i > s_size) {
                    int id = i - s_size - 1;
                    left[id] = pre[i];
                }
            }
        }
        {
            reverse(all(s)); reverse(all(t));
            string nw = s; nw += '#'; nw += t;
            int m = nw.size();
            for (int i = 1; i < m; i++) {
                int j = suf[i - 1];
                while (j > 0 && nw[j] != nw[i]) {
                    j = suf[j - 1];
                }
                if (nw[j] == nw[i]) {
                    suf[i] = j + 1;
                } else {
                    suf[i] = 0;
                } 
                if (i > s_size) {
                    int id = t_size - i + s_size;
                    right[id] = suf[i];
                }
            }                        
            reverse(all(s)); reverse(all(t));
        }
        if (left[s_size - 1] == s_size || right[1] == s_size) {
            return true;
        }
        for (int i = 1; i < t_size - 1; i++) {
            if (left[i - 1] == i && right[i + 1] == t_size - (i + 1)) {
                return true;
            }
        }
        return false;
    };
    set<string> ans;
    {
        string s = init.substr(0, len);
        string t = init.substr(len);
        if (check(s, t)) ans.insert(s);
    }
    {
        string s = init.substr(n - len, len);
        string t = init.substr(0, n - len);
        if (check(s, t)) ans.insert(s);
    }
    if ((int) ans.size() == 0) {
        cout << "NOT POSSIBLE";
    } else if ((int) ans.size() == 1) {
        cout << (*ans.begin());
    } else {
        cout << "NOT UNIQUE";
    }
}
 
int main() {
    setIO();
 
    int tt = 1;
    #ifdef TEST_CASES
        cin >> tt;
    #endif
 
    while (tt--)
        solve();
 
    return 0;
}

Compilation message (stderr)

friends.cpp: In function 'void setIn(std::string)':
friends.cpp:44:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
      |                       ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
friends.cpp: In function 'void setOut(std::string)':
friends.cpp:45:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
      |                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...