제출 #38484

#제출 시각아이디문제언어결과실행 시간메모리
38484bakurits세 명의 친구들 (BOI14_friends)C++14
0 / 100
66 ms22528 KiB
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>

const int N = 2000010;
const int rem = 1e9 + 7;

using namespace std;

int n;
char st[N];
int hsh[N], deg_27[N];
vector <int> ans;

int get_hesh(int l, int r) {
    if (l > r) return 0;
    int ans = ((long long)hsh[r] - (long long)hsh[l - 1] * deg_27[r - l + 1]) % rem;
    return (ans + rem) % rem;
}

int main() {
    scanf("%d %s", &n, st);
    
    if (!(n & 1)) {
        printf("NOT POSSIBLE\n");
        return 0;
    }   
    deg_27[0] = 1;
    for (int i = 0; i < n; i++) {
        hsh[i + 1] = ((long long)hsh[i] * 27 + st[i] - 'A' + 1) % rem;
        deg_27[i + 1] = ((long long)deg_27[i] * 27) % rem;
    }

    int a_len = (n - 1) >> 1;

    for (int i = 1; i <= n; i++) {
        int whole_hesh_left, whole_hesh_right;
        if (i <= a_len) {
            int remaining = a_len - i + 1;
            int hsh_right = get_hesh(i + 1, i + remaining);
            int hsh_left = hsh[i - 1];
            whole_hesh_left = ((long long)hsh_left * deg_27[remaining] + hsh_right) % rem;
        } else {
            whole_hesh_left = hsh[a_len];
        }

        int l_0 = n - a_len + 1;
        if (i >= l_0) {
            l_0--;
            int remaining = n - i;
            int hsh_right = get_hesh(i + 1, n);
            int hsh_left = get_hesh(l_0, i - 1);
            whole_hesh_right = ((long long)hsh_left * deg_27[remaining] + hsh_right) % rem;
        } else {
            whole_hesh_right = get_hesh(l_0, n);
        }
        if (whole_hesh_left == whole_hesh_right) {
            ans.push_back(i - 1);
        }
       // printf("%d %d \n", whole_hesh_left, whole_hesh_right);
    }

    if (ans.size() > 1) {
        printf("NOT UNIQUE\n");
        return -1;
    } else if (ans.size() == 0) {
        printf("NOT POSSIBLE\n");
        return -1;
    } else {
        int arr = ans[0];
        string res = st;
        res.erase(res.begin() + arr);
        cout << res.substr(0, a_len) << "\n";
    }

}

컴파일 시 표준 에러 (stderr) 메시지

friends.cpp: In function 'int main()':
friends.cpp:23:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %s", &n, st);
                           ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...