题目链接

思路

假设让求长度为LLL且不包含词根的个数,对所有的词根建acacac自动机,然后用矩阵MMM表示可转移状态,最后最快速幂即可。
如何求长度不超过LLL且不包含LLL且不包含词根的个数,可以利用这个矩阵[M101]\begin{gathered} \begin{bmatrix} M & 1 \\ 0 & 1 \end{bmatrix} \end{gathered}[M0​11​]​,这样就可以得到∑i=0nMi\sum\limits_{i=0}^{n}M^ii=0∑n​Mi,最后用总的状态数[26101]n\begin{bmatrix} 26 & 1 \\ 0 & 1\end{bmatrix} ^ n[260​11​]n减去即可

#include <vector>
#include <queue>
#include <stdio.h>
#include <iostream>
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
using namespace std;
struct Matrix{int n;unsigned long long mat[151][151];Matrix(){}Matrix(int _n) {n = _n;for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {mat[i][j] = 0;}}}Matrix operator *(const Matrix &b) const{Matrix ans = Matrix(n);for (int i = 0; i < n; ++i) {for (int j = 0; j < n; ++j) {for (int k = 0; k < n; ++k) {ans.mat[i][j] += mat[i][k] * b.mat[k][j];// ans.mat[i][j] = (ans.mat[i][j] + mat[i][k] * b.mat[k][j] % mod) % mod;}}}return ans;}
};
struct Trie{int nex[maxn][26], fail[maxn], end[maxn];int root, p;inline int newnode() {for (int i = 0; i < 26; ++i) {nex[p][i] = -1;}end[p++] = 0;return p - 1;}inline void init() {p = 0;root = newnode();}inline void insert(char *buf) {int now = root;for (int i = 0; buf[i]; ++i) {if (nex[now][buf[i]-'a'] == -1) nex[now][buf[i]-'a'] = newnode();now = nex[now][buf[i]-'a'];}end[now]++;} inline void build() {queue<int> que;fail[root] = root;for (int i = 0; i < 26; ++i) {if (nex[root][i] == -1)nex[root][i] = root;else {fail[nex[root][i]] = root;que.push(nex[root][i]);}}while (!que.empty()) {int now = que.front();que.pop();if (end[fail[now]]) end[now] = 1; for (int i = 0; i < 26; ++i) {if (nex[now][i] == -1) nex[now][i] = nex[fail[now]][i];else {fail[nex[now][i]] = nex[fail[now]][i];que.push(nex[now][i]);}}}}inline Matrix get() {Matrix tmp = Matrix(p*2);for (int i = 0; i < p; ++i) {for (int j = 0; j < 26; ++j) {if (end[i] || end[nex[i][j]]) continue;tmp.mat[i][nex[i][j]]++;}}for (int i = 0; i < p; ++i) tmp.mat[i][i+p] = tmp.mat[i+p][i+p] = 1;return tmp;}
}ac;
Matrix Pow(Matrix a, int n) {Matrix ans = Matrix(a.n);Matrix tmp = a;for (int i = 0; i < a.n; ++i) {ans.mat[i][i] = 1;}while (n) {if (n & 1) ans = ans * tmp;tmp = tmp * tmp;n >>= 1;}return ans;
}
char s[maxn];
int main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n, m;while (scanf("%d %d\n", &n, &m) != EOF) {ac.init();for (int i = 0; i < n; ++i) scanf("%s", s), ac.insert(s);ac.build();Matrix ans = ac.get();ans = Pow(ans, m);Matrix tmp = Matrix(2);tmp.mat[0][0] = 26;tmp.mat[0][1] = 1;tmp.mat[1][1] = 1;tmp = Pow(tmp, m);unsigned long long sum = tmp.mat[0][0] + tmp.mat[0][1] - 1;for (int j = 0; j < 2*ac.p; ++j) sum -= ans.mat[0][j];printf("%llu\n", sum+1);}return 0;
}

HDU -2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)相关推荐

  1. HDU - 2243 考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    题目链接:点击查看 题目大意:给出 n 个词根,现在要求出长度不大于 len 的单词中,有多少单词包含至少一个词根 题目分析:如果我们反过来想,也就是求出来总的单词数,然后减去不包含词根的单词数,剩下 ...

  2. [hdu2243]考研路茫茫——单词情结(AC自动机+矩阵快速幂)

    题意:长度不超过L,只由小写字母组成的,至少包含一个词根的单词,一共可能有多少个. 解题关键:利用补集转化的思想,先求一个词根也不包含的单词个数,然后用总的减去即可.长度不超过L需要用矩阵维数增加一倍 ...

  3. hdu 2243 考研路茫茫——单词情结(AC自动+矩阵)

    考研路茫茫--单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  4. hdu_2243_考研路茫茫——单词情结(AC自动机+矩阵)

    题目链接:hdu_2243_考研路茫茫--单词情结 题意: 让你求包含这些模式串并且长度不小于L的单词种类 题解: 这题是poj2788的升级版,没做过的强烈建议先做那题. 我们用poj2778的方法 ...

  5. HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法

    http://acm.hdu.edu.cn/showproblem.php?pid=2243 这是一题AC自动机 + 矩阵快速幂的题目, 首先知道总答案应该是26^1 + 26^2 + 26^3 .. ...

  6. HDU 2243考研路茫茫——单词情结 (AC自动机+矩阵快速幂)

    背单词,始终是复习英语的重要环节.在荒废了3年大学生涯后,Lele也终于要开始背单词了. 一天,Lele在某本单词书上看到了一个根据词根来背单词的方法.比如"ab",放在单词前一般 ...

  7. HDU 2243 考研路茫茫——单词情结(自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2243 题意:给出m个串,问有多少不大于n的串至少包含m个串中的一个? 思路:首先求出不包含m个串的长度 ...

  8. 考研路茫茫――单词情结 HDU - 2243(ac自动机 + 矩阵快速幂)

    考研路茫茫--单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 【HDU No. 2243】单词情结 考研路茫茫——单词情结

    [HDU No. 2243]单词情结 考研路茫茫--单词情结 杭电OJ 题目地址 [题意] 单词和词根仅由小写字母组成.给定N个词根,求长度不超过L 且至少包含一个词根的单词可能有多少个? 若有两个词 ...

最新文章

  1. SpringBoot数据访问配置
  2. 利用prototxt文件绘制网络的结构图
  3. GCC中的分支预测(likely和unlikey)
  4. REVERSE-PRACTICE-CTFSHOW-5
  5. ucore和linux区别,附录 - 附录A—ucore历史 - 《操作系统的基本原理与简单实现》 - 书栈网 · BookStack...
  6. Android立刻终止一个线程
  7. 如何面试一位前端工程师
  8. jpa 原生sql 查询返回一个实体_JPA查询--使用原生sql 并且把查询结果转为实体对象...
  9. Project Euler Problem 27 Quadratic primes
  10. bzoj 2844: albus就是要第一个出场(线性基)
  11. Oracle中job_type,【学习笔记】Oracle DBMS_SCHEDULER详细介绍与使用案例
  12. (二)docker常用命令
  13. Odoo的采购入库单、销售发货单整单被取消,或选择了不生成欠单后又想继续入库或发货,如何处理?
  14. 电商分析公式和指标整理
  15. 北京理工大学软件工程复试之路
  16. 问题解决:下载的网页打开后自动跳转到首页
  17. 组合业务流程管理与区块链
  18. CATIA软件VBA二次开发:Excel文件中点坐标数据导入与生成点应用程序编写
  19. 记一次安装 ubuntu 18.04 双系统 (双硬盘)
  20. 图像的拉普拉斯算子之c++实现(qt + 不调包)

热门文章

  1. 实验吧逆向catalyst-system——WP
  2. Android基础——项目的文件结构(三)
  3. 通用shellcode代码
  4. NodeJS学习日记--VSCode下调试
  5. CodeForces 361B Levko and Permutation
  6. xss_url通关_1-10
  7. [网络安全自学篇] 五十三.Windows系统安全之Metasploit实现栈溢出攻击及反弹shell原理解析
  8. C# 连接SQLServer数据库及登录验证知识
  9. Git之变基方式Rebase的使用
  10. 【数据结构与算法】之深入解析“不同的二叉搜索树”的求解思路与算法示例