Submission #38506

#TimeUsernameProblemLanguageResultExecution timeMemory
38506bakuritsThree Friends (BOI14_friends)C++14
100 / 100
93 ms22532 KiB
#include <iostream> #include <stdio.h> #include <vector> #include <string> #include <map> 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; map <int,int> mp; 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 && mp[whole_hesh_left] == 0) { mp[whole_hesh_left] = 1; ans.push_back(i - 1); } // printf("%d %d \n", whole_hesh_left, whole_hesh_right); } if (ans.size() > 1) { printf("NOT UNIQUE\n"); } else if (ans.size() == 0) { printf("NOT POSSIBLE\n"); } else { int arr = ans[0]; string res = st; res.erase(res.begin() + arr); cout << res.substr(0, a_len) << "\n"; } }

Compilation message (stderr)

friends.cpp: In function 'int main()':
friends.cpp:25: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...