Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <algorithm>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <map>
- #include <unordered_set>
- #include <unordered_map>
- #include <queue>
- #include <ctime>
- #include <cassert>
- #include <complex>
- #include <numeric>
- #include <string>
- #include <cstring>
- #include <chrono>
- #include <random>
- #include <queue>
- #include <bitset>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef pair<ll, int> pli;
- typedef pair<ll, ll> pll;
- typedef long double ld;
- #define mp make_pair
- #define str(a) a.c_str()
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- struct bohr {
- map<int, bohr*> link;
- int was, term;
- bohr() : was(0), term(0) {}
- };
- bohr * t(new bohr());
- void add(bohr * node, vector<int>& word) {
- for (int c : word) {
- if (node->link.count(c) == 0) {
- node->link[c] = new bohr();
- }
- node = node->link[c];
- node->was++;
- }
- node->term++;
- }
- const int N = 1e5 + 40;
- const ll INF = 1e18;
- int n, m, k;
- char s[N];
- ll win[N];
- pair<string, ll> calc(bohr * cur, int sz) {
- string str;
- if (cur == nullptr) {
- int ost = m - sz;
- while (ost--) str += '0';
- return mp(str, 0);
- }
- if (sz == m) {
- return mp("", win[sz] * cur->was);
- }
- ll ret = INF, sum = cur->term;
- for (int i = 0; i < k; ++i) {
- if (cur->link.count(i)) sum += cur->link[i]->was;
- }
- for (int i = 0; i < k; ++i) {
- bohr * go(nullptr);
- ll temp = sum;
- if (cur->link.count(i)) {
- go = cur->link[i];
- temp -= go->was;
- }
- auto get = calc(go, sz + 1);
- if (get.second + temp * win[sz] < ret) {
- ret = get.second + temp * win[sz];
- str = (char)(i + '0') + get.first;
- }
- }
- return mp(str, ret);
- }
- int main() {
- // freopen("input.txt", "r", stdin);
- // freopen("output.txt", "w", stdout);
- scanf("%d %d %d", &n, &m, &k);
- for (int i = 1; i <= m; ++i) {
- scanf("%lld", win + i);
- }
- for (int i = 0; i < n; ++i) {
- vector<int> b(m);
- scanf("%s", s);
- for (int j = 0; j < m; ++j) {
- b[j] = s[j] - '0';
- }
- add(t, b);
- }
- auto p = calc(t, 0);
- printf("%s\n%lld\n", str(p.first), p.second);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement