Submission #845234

#TimeUsernameProblemLanguageResultExecution timeMemory
845234vjudge1Three Friends (BOI14_friends)C++17
0 / 100
76 ms44108 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define int long long

using namespace std;

const int N = 2 << 20;
const int INF = INT_MAX;
const int mod = 1e9+7;
const int base = 101;

int n,pred[N];
string s,ans;
int half,lab[N];

int get(int u, int v) {
    if(u > v) return 0;
    return (pred[v] - pred[u-1]*lab[v-u+1] + mod*mod)%mod;
}

bool check(int u, int v) {
    int mid = u + v >> 1;
    return (get(u,mid) == get(mid+1,v));
}

signed main()
{
    if (fopen("solve.inp", "r")) {
        freopen("solve.inp", "r", stdin);
        freopen("solve.out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    ans = "";
    if(n % 2 == 0) {
        cout << "NOT POSSIBLE";
        return 0;
    }
    s = " " + s;
    lab[0] = 1;
    for(int i = 1 ; i <= n ; i++){
        lab[i] = lab[i-1]*base%mod;
    }
    for(int i = 1 ; i <= n ; i++) {
        pred[i] = (pred[i-1]*base+s[i])%mod;
    }
    half = n/2;
    if(check(2,n)) {
        for(int i = 2 ; i <= n ; i++) {
            ans += s[i];
        }
    }
    if(check(1,n-1)) {
        if(ans != "") {
            cout << "NOT UNIQUE";
            return 0;
        }
        for(int i = 1 ; i < n ; i++) {
            ans += s[i];
        }
    }
    for(int i = 2, mid ; i < n ; i++) {
        mid = half;
        int a = 0;
        int b = 0;
        if(i <= half) {
            a = get(1,i-1);
            mid -= i-1;
            a = (a*lab[mid] + get(i+1,i+mid))%mod;
            b = get(i+mid+1,n);
        }
        else {
            a = get(1,half);
            b = get(half+1,i-1);
            mid -= i-half-1;
            b = (b*lab[n-i]+get(i+1,n))%mod;
        }
        if(a == b) {
            if(ans != "") {
                cout << "NOT UNIQUE";
                return 0;
            }
            for(int j = 1 ; j <= n ; j++) {
                if(j == i) {
                    continue;
                }
                ans += s[j];
            }
        }
    }
    if(ans == "") {
        cout << "NOT POSSIBLE";
    }
    else {
        for(int i = 0 ; i < half ; i++) {
            cout << ans[i];
        }
    }
    return 0;
}

Compilation message (stderr)

friends.cpp: In function 'bool check(long long int, long long int)':
friends.cpp:23:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |     int mid = u + v >> 1;
      |               ~~^~~
friends.cpp: In function 'int main()':
friends.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   30 |         freopen("solve.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
friends.cpp:31:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   31 |         freopen("solve.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...