Submission #352540

#TimeUsernameProblemLanguageResultExecution timeMemory
352540mjhmjh1104Joyful KMP (KRIII5_JK)C++14
7 / 7
559 ms125892 KiB
#include <cstdio> #include <vector> #include <cstring> #include <algorithm> using namespace std; const int MOD = 1e9 + 7; int uf[1000006], mn[1000006]; int _find(int x) { if (uf[x] == -1) return x; return uf[x] = _find(uf[x]); } void _merge(int x, int y) { x = _find(x), y = _find(y); if (x == y) return; mn[y] = min(mn[x], mn[y]); uf[x] = y; } int l, res = 1, fail[1000006], order[1000006], t_sz[1000006], used[1000006]; long long k, _r = 1; char str[1000006], sl[1000006]; vector<int> v, adj[1000006], adj_uf[1000006]; bool visited[1000006], overflow; long long on[1000006]; void dfs(int x, int sz = 26) { visited[x] = true; t_sz[x] = sz; res = 1LL * res * sz % MOD; for (auto &i: adj_uf[x]) if (!visited[i]) dfs(i, sz - 1); } int main() { scanf("%s%lld", str, &k); k--; l = strlen(str); for (int i = 0; i < l; i++) uf[i] = -1, mn[i] = i; for (int i = 1, j = 0; i < l; i++) { while (j > 0 && str[i] != str[j]) { adj[i].push_back(j), adj[j].push_back(i); j = fail[j - 1]; } if (str[i] == str[j]) { _merge(i, j); fail[i] = ++j; } else adj[i].push_back(j), adj[j].push_back(i); } for (int i = 0; i < l; i++) v.push_back(_find(i)); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); sort(v.begin(), v.end(), [](const int &a, const int &b) { return mn[a] < mn[b]; }); for (int i = 0; i < (int)v.size(); i++) order[v[i]] = i; for (int i = 0; i < l; i++) for (auto &j: adj[i]) if (order[_find(i)] < order[_find(j)]) adj_uf[_find(i)].push_back(_find(j)); for (int i = 0; i < l; i++) sort(adj_uf[i].begin(), adj_uf[i].end(), [](const int &a, const int &b) { return order[a] < order[b]; }); dfs(v[0]); printf("%d\n", res); reverse(v.begin(), v.end()); for (auto &i: v) on[i] = -1; for (auto &i: v) { if (overflow) break; on[i] = _r; if (__builtin_mul_overflow(_r, t_sz[i], &_r)) overflow = true; } reverse(v.begin(), v.end()); if (!overflow && k >= _r) return puts("OVER"), 0; for (int i = 0; i < l; i++) visited[i] = false; for (auto &j: v) { visited[j] = true; int s = 0; for (int i = 0; i < 26; i++) if (!(used[j] & 1 << i)) { if (on[j] == -1 || k - on[j] < 0) { sl[j] = 'a' + i; s = i; break; } k -= on[j]; } for (auto &i: adj_uf[j]) if (!visited[i]) used[i] |= 1 << s; } for (int i = 0; i < l; i++) printf("%c", sl[_find(i)]); }

Compilation message (stderr)

JK.cpp: In function 'int main()':
JK.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   38 |     scanf("%s%lld", str, &k); k--;
      |     ~~~~~^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...