Submission #820082

#TimeUsernameProblemLanguageResultExecution timeMemory
820082serifefedartarThree Friends (BOI14_friends)C++17
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define fast ios::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
#define f first
#define s second
#define MOD 1000000009
#define LOGN 20
#define MAXN 2000100

const ll A = 9973;
ll powA[MAXN];
ll hashes[MAXN];
ll hash_ss(int l, int r) {
    ll ans = (hashes[r] - (hashes[l-1] * powA[r-l + 1] % MOD)) % MOD;
    while (ans < 0)
        ans += MOD;
    return ans;
}

ll hash_concat(int l1, int r1, int l2, int r2) {
    ll ans = (hash_ss(l2, r2) + (hash_ss(l1, r1) * powA[r2 - l2 + 1] % MOD)) % MOD;
    return ans;
}

int cnt[26];
int main() {
    fast
    powA[0] = 1;
    for (int i = 1; i < MAXN; i++)
        powA[i] = (powA[i-1] * A) % MOD;

    int N;
    string S;
    cin >> N >> S;
    S = "#" + S;

    if (N % 2 == 0) {
        cout << "NOT POSSIBLE\n";
        return 0;
    }

    hashes[0] = 0;
    for (int i = 1; i <= N; i++)
        hashes[i] = (hashes[i-1] * A + (S[i] - 'A' + 1)) % MOD;

    for (int i = 1; i <= N; i++)
        cnt[S[i] - 'A']++;

    int selected_char = -1;
    for (int i = 0; i < 26; i++) {
        if (cnt[i] % 2 && selected_char != -1) {
            cout << "NOT POSSIBLE\n";
            return 0;
        }
        if (cnt[i] % 2)
            selected_char = i;
    }

    int len = N / 2;
    set<pair<int,int>> possible;
    for (int i = 1; i <= N; i++) {
        if (S[i] - 'A' == selected_char) {
            ll left_hash = -1, right_hash = -1;

            if (i-1 >= len)
                left_hash = hash_ss(1, len);
            if (N-i >= len)
                right_hash = hash_ss(N-len+1, N);

            if (left_hash == -1) {
                if (i == 1)
                    left_hash = hash_ss(2, 1 + len);
                else
                    left_hash = hash_concat(1, i - 1, i + 1, N-len);
            }

            if (right_hash == -1) {
                if (i == N)
                    right_hash = hash_ss(N-len, N-1);
                else
                    right_hash = hash_concat(len + 1, i - 1, i + 1, N);
            }

            if (left_hash == right_hash) {
                if (i-1 >= len)
                    possible.push_back({0, len});
                else
                    possible.push_back({1, N-len+1});
            }
        }
    }

    if (possible.size() == 0)
        cout << "NOT POSSIBLE\n";
    else if (possible.size() == 1) {
        if (possible[0].f == 0) {
            for (int i = 1; i <= possible[0].s; i++)
                cout << S[i];
            cout << "\n";
        } else {
            for (int i = possible[0].s; i <= N; i++)
                cout << S[i];
            cout << "\n";
        }
    else if (possible.size() == 2) {
        string P = "", Q = "";
        if (possible[0].f == 0) {
            for (int i = 1; i <= possible[0].s; i++)
                P = P + S[i];
        } else {
            for (int i = possible[0].s; i <= N; i++)
                P = P + S[i];
        }

        if (possible[1].f == 0) {
            for (int i = 1; i <= possible[1].s; i++)
                Q = Q + S[i]; 
        } else {
            for (int i = possible[1].s; i <= N; i++)
                Q = Q + S[i];
        }

        if (P == Q)
            cout << P << "\n";
        else
            cout << "NOT UNIQUE\n";
    } else
        cout << "NOT UNIQUE\n";
}

Compilation message (stderr)

friends.cpp: In function 'int main()':
friends.cpp:88:30: error: 'class std::set<std::pair<int, int> >' has no member named 'push_back'
   88 |                     possible.push_back({0, len});
      |                              ^~~~~~~~~
friends.cpp:90:30: error: 'class std::set<std::pair<int, int> >' has no member named 'push_back'
   90 |                     possible.push_back({1, N-len+1});
      |                              ^~~~~~~~~
friends.cpp:98:21: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
   98 |         if (possible[0].f == 0) {
      |                     ^
friends.cpp:99:42: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
   99 |             for (int i = 1; i <= possible[0].s; i++)
      |                                          ^
friends.cpp:103:34: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  103 |             for (int i = possible[0].s; i <= N; i++)
      |                                  ^
friends.cpp:107:5: error: expected '}' before 'else'
  107 |     else if (possible.size() == 2) {
      |     ^~~~
friends.cpp:97:36: note: to match this '{'
   97 |     else if (possible.size() == 1) {
      |                                    ^
friends.cpp:109:21: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  109 |         if (possible[0].f == 0) {
      |                     ^
friends.cpp:110:42: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  110 |             for (int i = 1; i <= possible[0].s; i++)
      |                                          ^
friends.cpp:113:34: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  113 |             for (int i = possible[0].s; i <= N; i++)
      |                                  ^
friends.cpp:117:21: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  117 |         if (possible[1].f == 0) {
      |                     ^
friends.cpp:118:42: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  118 |             for (int i = 1; i <= possible[1].s; i++)
      |                                          ^
friends.cpp:121:34: error: no match for 'operator[]' (operand types are 'std::set<std::pair<int, int> >' and 'int')
  121 |             for (int i = possible[1].s; i <= N; i++)
      |                                  ^