Advertisement
cincout

Untitled

Nov 26th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <unordered_set>
  9. #include <unordered_map>
  10. #include <queue>
  11. #include <ctime>
  12. #include <cassert>
  13. #include <complex>
  14. #include <numeric>
  15. #include <string>
  16. #include <cstring>
  17. #include <chrono>
  18. #include <random>
  19. #include <queue>
  20. #include <bitset>
  21. using namespace std;
  22.  
  23. typedef long long ll;
  24. typedef pair<int, int> pii;
  25. typedef pair<ll, int> pli;
  26. typedef pair<ll, ll> pll;
  27. typedef long double ld;
  28. #define mp make_pair
  29. #define str(a) a.c_str()
  30.  
  31. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  32.  
  33. struct bohr {
  34. map<int, bohr*> link;
  35. int was, term;
  36. bohr() : was(0), term(0) {}
  37. };
  38.  
  39. bohr * t(new bohr());
  40.  
  41. void add(bohr * node, vector<int>& word) {
  42. for (int c : word) {
  43. if (node->link.count(c) == 0) {
  44. node->link[c] = new bohr();
  45. }
  46. node = node->link[c];
  47. node->was++;
  48. }
  49. node->term++;
  50. }
  51.  
  52. const int N = 1e5 + 40;
  53. const ll INF = 1e18;
  54.  
  55. int n, m, k;
  56. char s[N];
  57. ll win[N];
  58.  
  59. pair<string, ll> calc(bohr * cur, int sz) {
  60. string str;
  61. if (cur == nullptr) {
  62. int ost = m - sz;
  63. while (ost--) str += '0';
  64. return mp(str, 0);
  65. }
  66. if (sz == m) {
  67. return mp("", win[sz] * cur->was);
  68. }
  69. ll ret = INF, sum = cur->term;
  70. for (int i = 0; i < k; ++i) {
  71. if (cur->link.count(i)) sum += cur->link[i]->was;
  72. }
  73. for (int i = 0; i < k; ++i) {
  74. bohr * go(nullptr);
  75. ll temp = sum;
  76. if (cur->link.count(i)) {
  77. go = cur->link[i];
  78. temp -= go->was;
  79. }
  80. auto get = calc(go, sz + 1);
  81. if (get.second + temp * win[sz] < ret) {
  82. ret = get.second + temp * win[sz];
  83. str = (char)(i + '0') + get.first;
  84. }
  85. }
  86. return mp(str, ret);
  87. }
  88.  
  89. int main() {
  90. // freopen("input.txt", "r", stdin);
  91. // freopen("output.txt", "w", stdout);
  92.  
  93. scanf("%d %d %d", &n, &m, &k);
  94.  
  95. for (int i = 1; i <= m; ++i) {
  96. scanf("%lld", win + i);
  97. }
  98.  
  99. for (int i = 0; i < n; ++i) {
  100. vector<int> b(m);
  101. scanf("%s", s);
  102. for (int j = 0; j < m; ++j) {
  103. b[j] = s[j] - '0';
  104. }
  105. add(t, b);
  106. }
  107.  
  108. auto p = calc(t, 0);
  109. printf("%s\n%lld\n", str(p.first), p.second);
  110.  
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement