From 65fb32fd8df8df0809c8fcbf7c965adab68fe4b1 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 31 Dec 2022 20:12:41 +0800 Subject: [PATCH] =?UTF-8?q?#1817.=20=E3=80=902017.3=20=E9=95=BF=E4=B9=90?= =?UTF-8?q?=E7=9C=81=E9=80=89=E9=9B=86=E8=AE=AD=20Day18=20T3=E3=80=91?= =?UTF-8?q?=E5=AD=90=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/66624 --- S2OJ/1817/1817.cpp | 197 +++++++++++++++++++++++++++++++ S2OJ/1817/data/ex_substring1.ans | 3 + S2OJ/1817/data/ex_substring1.in | 3 + S2OJ/1817/data/ex_substring2.ans | 3 + S2OJ/1817/data/ex_substring2.in | 3 + S2OJ/1817/data/problem.conf | 3 + S2OJ/1817/data/substring1.ans | 3 + S2OJ/1817/data/substring1.in | 3 + S2OJ/1817/data/substring10.ans | 3 + S2OJ/1817/data/substring10.in | 3 + S2OJ/1817/data/substring2.ans | 3 + S2OJ/1817/data/substring2.in | 3 + S2OJ/1817/data/substring3.ans | 3 + S2OJ/1817/data/substring3.in | 3 + S2OJ/1817/data/substring4.ans | 3 + S2OJ/1817/data/substring4.in | 3 + S2OJ/1817/data/substring5.ans | 3 + S2OJ/1817/data/substring5.in | 3 + S2OJ/1817/data/substring6.ans | 3 + S2OJ/1817/data/substring6.in | 3 + S2OJ/1817/data/substring7.ans | 3 + S2OJ/1817/data/substring7.in | 3 + S2OJ/1817/data/substring8.ans | 3 + S2OJ/1817/data/substring8.in | 3 + S2OJ/1817/data/substring9.ans | 3 + S2OJ/1817/data/substring9.in | 3 + 26 files changed, 272 insertions(+) create mode 100644 S2OJ/1817/1817.cpp create mode 100644 S2OJ/1817/data/ex_substring1.ans create mode 100644 S2OJ/1817/data/ex_substring1.in create mode 100644 S2OJ/1817/data/ex_substring2.ans create mode 100644 S2OJ/1817/data/ex_substring2.in create mode 100644 S2OJ/1817/data/problem.conf create mode 100644 S2OJ/1817/data/substring1.ans create mode 100644 S2OJ/1817/data/substring1.in create mode 100644 S2OJ/1817/data/substring10.ans create mode 100644 S2OJ/1817/data/substring10.in create mode 100644 S2OJ/1817/data/substring2.ans create mode 100644 S2OJ/1817/data/substring2.in create mode 100644 S2OJ/1817/data/substring3.ans create mode 100644 S2OJ/1817/data/substring3.in create mode 100644 S2OJ/1817/data/substring4.ans create mode 100644 S2OJ/1817/data/substring4.in create mode 100644 S2OJ/1817/data/substring5.ans create mode 100644 S2OJ/1817/data/substring5.in create mode 100644 S2OJ/1817/data/substring6.ans create mode 100644 S2OJ/1817/data/substring6.in create mode 100644 S2OJ/1817/data/substring7.ans create mode 100644 S2OJ/1817/data/substring7.in create mode 100644 S2OJ/1817/data/substring8.ans create mode 100644 S2OJ/1817/data/substring8.in create mode 100644 S2OJ/1817/data/substring9.ans create mode 100644 S2OJ/1817/data/substring9.in diff --git a/S2OJ/1817/1817.cpp b/S2OJ/1817/1817.cpp new file mode 100644 index 00000000..c68d0668 --- /dev/null +++ b/S2OJ/1817/1817.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int mod = 1e9 + 7; + +class AcAutomaton { + private: + struct node { + int cnt; + std::shared_ptr child[26], fail; + + node() + : cnt(0), fail(nullptr) { + std::fill(std::begin(child), std::end(child), nullptr); + } + }; + + std::shared_ptr root; + + public: + std::vector> nodes; + std::unordered_map, int> nodes_id; + + AcAutomaton() + : root(std::make_shared()), nodes({root}), nodes_id({{root, 0}}) {} + + void insert(std::string s) { + std::shared_ptr cur = root; + + for (char c : s) { + if (cur->child[c - 'a'] == nullptr) { + cur->child[c - 'a'] = std::make_shared(); + nodes_id[cur->child[c - 'a']] = nodes.size(); + nodes.emplace_back(cur->child[c - 'a']); + } + + cur = cur->child[c - 'a']; + } + + cur->cnt++; + } + + void build() { + std::queue> q; + + for (int i = 0; i < 26; i++) { + if (root->child[i] != nullptr) { + q.emplace(root->child[i]); + root->child[i]->fail = root; + } else { + root->child[i] = root; + } + } + + while (!q.empty()) { + auto cur = q.front(); + q.pop(); + + for (int i = 0; i < 26; i++) { + if (cur->child[i] != nullptr) { + cur->child[i]->fail = cur->fail->child[i] == nullptr ? root : cur->fail->child[i]; + + q.emplace(cur->child[i]); + } else { + cur->child[i] = cur->fail->child[i] == nullptr ? root : cur->fail->child[i]; + } + } + } + } + + int query(std::string t) { + int res = 0; + std::shared_ptr cur = root; + + for (char c : t) { + cur = cur->child[c - 'a'] == nullptr ? root : cur->child[c - 'a']; + + for (auto i = cur; i != nullptr && i->cnt != -1; i = i->fail) { + res += i->cnt; + i->cnt = -1; + } + } + + return res; + } +}; + +constexpr int binpow(int a, int b, int mod = ::mod) { + int res = 1; + a %= mod; + + while (b) { + if (b & 1) res = static_cast(res) * a % mod; + a = static_cast(a) * a % mod; + b >>= 1; + } + + return res; +} + +void gauss(std::vector> &a, int n) { + int r, c; + + for (r = c = 0; c <= n; c++) { + int t = r; + + for (int i = r; i <= n; i++) { + if (std::abs(a[i][c]) > std::abs(a[t][c])) t = i; + } + + if (!a[t][c]) continue; + + std::swap(a[t], a[r]); + + for (int i = n + 1; i >= c; i--) { + a[r][i] = static_cast(a[r][i]) * binpow(a[r][c], mod - 2) % mod; + } + + for (int i = r + 1; i <= n; i++) { + if (a[i][c] != 0) { + for (int j = n + 1; j >= c; j--) { + a[i][j] = ((a[i][j] - static_cast(a[i][c]) * a[r][j]) % mod + mod) % mod; + } + } + } + + r++; + } + + if (r <= n) throw std::logic_error("No Solution"); + + for (int i = n; i >= 0; i--) { + for (int j = i + 1; j <= n; j++) { + a[i][n + 1] = ((a[i][n + 1] - static_cast(a[i][j]) * a[j][n + 1]) % mod + mod) % mod; + } + } +} + +const int inv_26 = binpow(26, mod - 2); + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + + cin >> t; + + while (t--) { + int n; + AcAutomaton ac; + + cin >> n; + + for (int i = 1; i <= n; i++) { + std::string s; + + cin >> s; + + ac.insert(s); + } + + ac.build(); + + std::vector> a(ac.nodes.size(), std::vector(ac.nodes.size() + 1)); + + for (int i = 0; i < ac.nodes.size(); i++) { + a[i][i] = 1; + + if (ac.nodes[i]->cnt) continue; + + a[i].back() = 1; + + for (auto ch : ac.nodes[i]->child) { + a[i][ac.nodes_id[ch]] = ((a[i][ac.nodes_id[ch]] - inv_26) % mod + mod) % mod; + } + } + + gauss(a, ac.nodes.size() - 1); + + cout << static_cast(a[0].back()) * binpow(a[0][0], mod - 2) % mod << endl; + } + + return 0; +} diff --git a/S2OJ/1817/data/ex_substring1.ans b/S2OJ/1817/data/ex_substring1.ans new file mode 100644 index 00000000..45f4549f --- /dev/null +++ b/S2OJ/1817/data/ex_substring1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:30e488e8ecde98831589c6a3836afb039a639c8d4292877992f58ac01735b89d +size 22 diff --git a/S2OJ/1817/data/ex_substring1.in b/S2OJ/1817/data/ex_substring1.in new file mode 100644 index 00000000..acd1e620 --- /dev/null +++ b/S2OJ/1817/data/ex_substring1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f7ded8e88db913e3d430edf152d3b2ca2d27f08a0db007ef001173ce5ae1cf4b +size 32 diff --git a/S2OJ/1817/data/ex_substring2.ans b/S2OJ/1817/data/ex_substring2.ans new file mode 100644 index 00000000..0ed8c14a --- /dev/null +++ b/S2OJ/1817/data/ex_substring2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb8ce9e98bb7d3871406b2c9d1d19b17352a25c5b08452f265a22c0c8f07dde7 +size 493 diff --git a/S2OJ/1817/data/ex_substring2.in b/S2OJ/1817/data/ex_substring2.in new file mode 100644 index 00000000..eb342840 --- /dev/null +++ b/S2OJ/1817/data/ex_substring2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e81063780915f0c2b11541112fdc228b7087b19e4d398260633d227b9644b8e +size 7659 diff --git a/S2OJ/1817/data/problem.conf b/S2OJ/1817/data/problem.conf new file mode 100644 index 00000000..1c7623e5 --- /dev/null +++ b/S2OJ/1817/data/problem.conf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dd9f46f239fd11feb279ac46bacd246629c4cbf301f2a7ab506267a2da6df1ad +size 187 diff --git a/S2OJ/1817/data/substring1.ans b/S2OJ/1817/data/substring1.ans new file mode 100644 index 00000000..657d4a6d --- /dev/null +++ b/S2OJ/1817/data/substring1.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:2f3570a5779668e2d131529860fb516821efa3950b45b7089433b98cf34894b0 +size 194 diff --git a/S2OJ/1817/data/substring1.in b/S2OJ/1817/data/substring1.in new file mode 100644 index 00000000..47837746 --- /dev/null +++ b/S2OJ/1817/data/substring1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ec8f1cabda3f7053a05daab2fbd1b1bc0a00746f9ae7e055053338029e839300 +size 246 diff --git a/S2OJ/1817/data/substring10.ans b/S2OJ/1817/data/substring10.ans new file mode 100644 index 00000000..7071c3a9 --- /dev/null +++ b/S2OJ/1817/data/substring10.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3ac68c6de7c7a73067662312d2c9a73d6f52506cf36de60084536c9c5c105912 +size 490 diff --git a/S2OJ/1817/data/substring10.in b/S2OJ/1817/data/substring10.in new file mode 100644 index 00000000..079b0c23 --- /dev/null +++ b/S2OJ/1817/data/substring10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9feabb597946acce419fb6359095beefc6459baaabf23cca7c451c9d0255a967 +size 7641 diff --git a/S2OJ/1817/data/substring2.ans b/S2OJ/1817/data/substring2.ans new file mode 100644 index 00000000..8ce4992e --- /dev/null +++ b/S2OJ/1817/data/substring2.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:263794e2c6d4229f90b2a432f0413feccc56e2ffc0953ebad9c7c1ce55ad5793 +size 190 diff --git a/S2OJ/1817/data/substring2.in b/S2OJ/1817/data/substring2.in new file mode 100644 index 00000000..9ec7729d --- /dev/null +++ b/S2OJ/1817/data/substring2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:64ebe750fa160182ffd9ad3ec3da96ed727fed23f228e3e20b3726b712a778a7 +size 251 diff --git a/S2OJ/1817/data/substring3.ans b/S2OJ/1817/data/substring3.ans new file mode 100644 index 00000000..03778320 --- /dev/null +++ b/S2OJ/1817/data/substring3.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:336568ba77a1ca2630319bfdef1836db049870a2af1ee40a4de51753a27fe4a8 +size 199 diff --git a/S2OJ/1817/data/substring3.in b/S2OJ/1817/data/substring3.in new file mode 100644 index 00000000..970658e3 --- /dev/null +++ b/S2OJ/1817/data/substring3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8b2441a3efe59db55912b98e31c8d76232b056a29ac7e0bd4318da00d7669db8 +size 231 diff --git a/S2OJ/1817/data/substring4.ans b/S2OJ/1817/data/substring4.ans new file mode 100644 index 00000000..9558129c --- /dev/null +++ b/S2OJ/1817/data/substring4.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:966c01ef10a1b2fb86af36eccd17a2a502029a8d9c9eee93d43e9c8355a443ba +size 192 diff --git a/S2OJ/1817/data/substring4.in b/S2OJ/1817/data/substring4.in new file mode 100644 index 00000000..41c4beb9 --- /dev/null +++ b/S2OJ/1817/data/substring4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4b1196c8783f3a15da665a944c106db25de6fa6fbfc908ea410eb2e27a7efc7c +size 242 diff --git a/S2OJ/1817/data/substring5.ans b/S2OJ/1817/data/substring5.ans new file mode 100644 index 00000000..4c0a3da8 --- /dev/null +++ b/S2OJ/1817/data/substring5.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1a8e0e6ed7321d7e2e06e75fb73a3facc1de1f60caf43832d13e8c0c8894528c +size 493 diff --git a/S2OJ/1817/data/substring5.in b/S2OJ/1817/data/substring5.in new file mode 100644 index 00000000..7e3b5932 --- /dev/null +++ b/S2OJ/1817/data/substring5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:0f2bd1aeca56e9e772233735c746d2c7aab4cfaf56dbf22b80de7e303ade35a2 +size 7623 diff --git a/S2OJ/1817/data/substring6.ans b/S2OJ/1817/data/substring6.ans new file mode 100644 index 00000000..0ed8c14a --- /dev/null +++ b/S2OJ/1817/data/substring6.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb8ce9e98bb7d3871406b2c9d1d19b17352a25c5b08452f265a22c0c8f07dde7 +size 493 diff --git a/S2OJ/1817/data/substring6.in b/S2OJ/1817/data/substring6.in new file mode 100644 index 00000000..eb342840 --- /dev/null +++ b/S2OJ/1817/data/substring6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1e81063780915f0c2b11541112fdc228b7087b19e4d398260633d227b9644b8e +size 7659 diff --git a/S2OJ/1817/data/substring7.ans b/S2OJ/1817/data/substring7.ans new file mode 100644 index 00000000..85d69305 --- /dev/null +++ b/S2OJ/1817/data/substring7.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dcd6cdbabb24169d4be4b6d000458b3135585ba30da6848b3621707f4d6199ac +size 493 diff --git a/S2OJ/1817/data/substring7.in b/S2OJ/1817/data/substring7.in new file mode 100644 index 00000000..36a52a42 --- /dev/null +++ b/S2OJ/1817/data/substring7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d1b8a8734a5ba32bb29ab417ba52d7c5bdaf8eae89071f73960e522166f6e349 +size 7645 diff --git a/S2OJ/1817/data/substring8.ans b/S2OJ/1817/data/substring8.ans new file mode 100644 index 00000000..e3029c97 --- /dev/null +++ b/S2OJ/1817/data/substring8.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ca7206887139feca33613246922013a267d09a0e860f07803d2ae55f8ac7740a +size 491 diff --git a/S2OJ/1817/data/substring8.in b/S2OJ/1817/data/substring8.in new file mode 100644 index 00000000..fe6ff59e --- /dev/null +++ b/S2OJ/1817/data/substring8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:1b427d17dc35d280de693eb8f1dc9c13bd42023b20d9618da59e018a36fac965 +size 7676 diff --git a/S2OJ/1817/data/substring9.ans b/S2OJ/1817/data/substring9.ans new file mode 100644 index 00000000..ca15543b --- /dev/null +++ b/S2OJ/1817/data/substring9.ans @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:034a6a7ef5584a0d6a8ecdeda0ab12d989e087b9c7c491112e532dfe9f43d0e1 +size 494 diff --git a/S2OJ/1817/data/substring9.in b/S2OJ/1817/data/substring9.in new file mode 100644 index 00000000..ec308c21 --- /dev/null +++ b/S2OJ/1817/data/substring9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3e8219f75fb07e1610219d4ae2d8ba22115720a7083e6989b5c32cb11ab1b204 +size 7660