Submission #938418

#TimeUsernameProblemLanguageResultExecution timeMemory
938418Ghulam_JunaidThree Friends (BOI14_friends)C++17
100 / 100
43 ms11804 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll k;
bool check(string s, string t){
    int j = 0;
    int i = 0;
    int ind = t.size() - 1;
    for (; i < s.size() and j < t.size();){
        if (s[i] == t[j]){
            i++;
            j++;
        }
        else{
            ind = j;
            j++;
        }
    }

    if (i == s.size())
        return 1;

    j = t.size() - 1;
    i = s.size() - 1;
    ind = 0;
    for (; i >= 0 and j >= 0;){
        if (s[i] == t[j]){
            i--;
            j--;
        }
        else{
            ind = j;
            j--;
        }
    }

    if (i == -1)
        return 1;
    return 0;
}

int main(){
    ll n;
    string s;
    cin >> n >> s;
    n = s.size();

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

    k = n / 2;
    string P, S;
    for (ll i = 0; i < k; i ++)
        P += s[i];
    for (ll i = k + 1; i < n; i ++)
        S += s[i];

    if (P == S){
        cout << P << endl;
        return 0;
    }

    string ans;

    string Q = s[k] + S;
    int poss = check(P, Q);
    if (poss)
        ans = P;

    int prev = poss;

    Q = P + s[k];
    poss += check(S, Q);
    if (poss - prev)
        ans = S;

    if (poss > 1)
        cout << "NOT UNIQUE" << endl;
    else if (poss == 0)
        cout << "NOT POSSIBLE" << endl;
    else
        cout << ans << endl;
}

Compilation message (stderr)

friends.cpp: In function 'bool check(std::string, std::string)':
friends.cpp:11:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for (; i < s.size() and j < t.size();){
      |            ~~^~~~~~~~~~
friends.cpp:11:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for (; i < s.size() and j < t.size();){
      |                             ~~^~~~~~~~~~
friends.cpp:22:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |     if (i == s.size())
      |         ~~^~~~~~~~~~~
friends.cpp:10:9: warning: variable 'ind' set but not used [-Wunused-but-set-variable]
   10 |     int ind = t.size() - 1;
      |         ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...