Submission #22829

#TimeUsernameProblemLanguageResultExecution timeMemory
22829- - - - - - - List of honorable mention follows - - - - - - - (#40)Joyful KMP (KRIII5_JK)C++11
7 / 7
419 ms240552 KiB
#include <functional> #include <algorithm> #include <stdexcept> #include <iostream> #include <sstream> #include <fstream> #include <numeric> #include <iomanip> #include <cstdlib> #include <cstring> #include <utility> #include <cctype> #include <vector> #include <string> #include <bitset> #include <cmath> #include <queue> #include <stdint.h> #include <stdio.h> #include <stack> #include <ctime> #include <list> #include <map> #include <set> #include <tuple> #include <unordered_set> #include <assert.h> #define REP(i,n) for(int i=0;i<n;i++) #define TR(i,x) for(__typeof(x.begin()) i=x.begin();i!=x.end();i++) #define ALL(x) x.begin(),x.end() #define SORT(x) sort(ALL(x)) #define CLEAR(x) memset(x,0,sizeof(x)) #define FILL(x,c) memset(x,c,sizeof(x)) using namespace std; #define PB push_back #define MP make_pair typedef map<int,int> MII; typedef map<string,int> MSI; typedef vector<int> VI; typedef vector<string> VS; typedef vector<long double> VD; typedef pair<int,int> PII; typedef long long int64; typedef long long LL; typedef unsigned int UI; typedef long double LD; typedef unsigned long long ULL; const int N = 1000007; const int MOD = 1000000007; char s[N]; char answer[N]; int n; VI same[N]; VI diff[N]; VI nodes[N]; int diffCount[N]; int kmp[N]; int color[N]; int clusterDiffCount[N]; VI clusterDiff[N]; void dfs(int x, int c) { color[x] = c; nodes[c].PB(x); if (diffCount[x] != 0) { assert(!clusterDiffCount[c]); clusterDiffCount[c] = diffCount[x]; clusterDiff[c] = diff[x]; } TR(it, same[x]) { if (color[*it] == -1) { dfs(*it, c); } } } bool used[N]; int main() { scanf("%s", s); n = strlen(s); LL k; cin >> k; kmp[0] = -1; for (int i = 1; i < n; ++i) { int current = kmp[i - 1]; while (current != -1 && s[current + 1] != s[i]) { current = kmp[current]; } if (s[current + 1] == s[i]) { ++current; } kmp[i] = current; } for (int i = 1; i < n; ++i) { if (kmp[i] != -1) { same[i].PB(kmp[i]); same[kmp[i]].PB(i); // cout << "SAME " << i << ", " << kmp[i] << endl; continue; } else { ++diffCount[i]; diff[i].PB(0); } diffCount[i] = 1; int current = kmp[i - 1]; while (current != -1 && s[current + 1] != s[i]) { if (kmp[current + 1] == -1) { ++diffCount[i]; diff[i].PB(current + 1); // cout << "DIFF " << i << ", " << current + 1 << endl; } current = kmp[current]; } } FILL(color, -1); int colors = 0; for (int i = 0; i < n; ++i) { if (color[i] == -1) { dfs(i, colors++); } } VI clusterRep; REP(i, n) { if (!used[color[i]]) { clusterRep.PB(i); used[color[i]] = true; } } assert(clusterRep.size() == colors); vector<LD> doubleWays(colors + 1); vector<LL> ways(colors + 1); vector<LL> modWays(colors + 1); doubleWays[colors] = 1; ways[colors] = modWays[colors] = 1; for (int i = colors - 1; i >= 0; --i) { int at = clusterRep[i]; assert(26 - clusterDiffCount[color[at]] > 0); doubleWays[i] = 26 - clusterDiffCount[color[at]]; ways[i] = 26 - clusterDiffCount[color[at]]; modWays[i] = 26 - clusterDiffCount[color[at]]; if (i != colors - 1) { doubleWays[i] = min((LD) 1e30, doubleWays[i] * doubleWays[i + 1]); ways[i] *= ways[i + 1]; modWays[i] = modWays[i] * modWays[i + 1] % MOD; } } LD bound = 9.1e18; cout << modWays[0] << endl; if (doubleWays[0] < bound && k > ways[0]) { cout << "OVER" << endl; return 0; } for (int i = 0; i < colors; ++i) { int at = clusterRep[i]; bool found = false; for (char c = 'a'; c <= 'z'; ++c) { bool ban = false; TR(it, clusterDiff[color[at]]) { assert(answer[*it]); if (c == answer[*it]) { ban = true; break; } } if (ban) continue; if (doubleWays[i + 1] < bound) { if (k > ways[i + 1]) { k -= ways[i + 1]; continue; } } found = true; TR(it, nodes[color[at]]) { answer[*it] = c; } break; } assert(found); } printf("%s\n", answer); return 0; }

Compilation message (stderr)

In file included from JK.cpp:27:0:
JK.cpp: In function 'int main()':
JK.cpp:143:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     assert(clusterRep.size() == colors);
                              ^
JK.cpp:88:19: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%s", s);
                   ^
#Verdict Execution timeMemoryGrader output
Fetching results...