Progressive hard octave rock tunes (so-called “phorts”) are written using a specifific music notation. This flflavor of rock is built on just 13 difffferent note pitches, other pitches (in other octaves)are considered to be an outdated musical ballast. Each note can be either a long one or a shortone. Consequently, there are exactly 26 difffferent notes in the rock.

You are going to compose a phort tune on the occasion of your friend’s birthday and perform itwith your band on the main town square. While composing the phort, you need to avoid certainmusical phrases, which are heavily copyrighted as a result of long research sponsored by bigrecord companies. It has been established that these phrases are very catchy, easy to remember,and could be exploited to bind the listeners subconsciously to a particular music company whichwould utilize these phrases in their production.

The tune is a sequence of notes. A musical phrase is also a sequence of notes and it is consideredto be contained in a tune if its notes form a contiguous subsequence of the tune, which meansthe same notes appear in the tune right after each other in the same order.

Fortunately, only a few forbidden phrases have been patented so far. Thus, you have a relativefreedom in composing your own tunes. In particular, you are interested in the number ofacceptable tunes of some length. An acceptable tune is any tune which does not contain aforbidden phrase. The length of the tune is equal to the number of notes it contains.

Input Specifification
The first line contains two integers N, Q (1 ≤ N ≤ 10^9, 1 ≤ Q ≤ 100)N,Q(1≤N≤10
9
,1≤Q≤100). NN is the length of the tune, QQ is the number of forbidden musical phrases. Each of the Q following lines describes one forbidden phrase. A description of a forbidden phrase starts with a positive integer LL, indicating its length, followed by a string of LL lowercase English letters. Each letter represents one rock note, different letters represent different notes.

The sum of lengths of all forbidden phrases does not exceed 100

Output Specification
Output the number of different acceptable tunes of length NN. Print the result modulo 1 000 000 007.

样例输入1复制
2 3
1 a
1 b
1 c
样例输出1复制
529
样例输入2复制
3 3
2 aa
1 a
1 a
样例输出2复制
15625
样例输入3复制
3 1
2 ab
样例输出3复制
17524

题意:
长度为n的序列,每个位置可以填任意一个小写字母。但要求不能出现m种子串。求方案数。

思路:
你以为你换了个马甲我就不认得了吗(认真脸) ?

这是道原题 POJ 2778
https://blog.csdn.net/tomjobs/article/details/106889258

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>using namespace std;const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
typedef long long ll;struct Trie {int son[30],val,fail;
}t[maxn];struct Matrix {ll m[101][101];
}M;ll m;
int n,cnt,id[505];
char s[maxn];void init() {for(int i = 0;i <= cnt;i++) {memset(t[i].son,0,sizeof(t[i].son));t[i].val = t[i].fail = 0;}cnt = 0;for(int i = 0;i < 26;i++) {id[i + 'a'] = i;}
}void insert(char *s) {int u = 0,len = strlen(s);for(int i = 0;i < len;i++) {int v = id[s[i]];if(!t[u].son[v]) t[u].son[v] = ++cnt;u = t[u].son[v];}t[u].val++;
}void getFail() {queue<int>q;q.push(0);t[0].fail = 0;while(!q.empty()) {int u = q.front();q.pop();if(t[t[u].fail].val) {t[u].val = 1;}for(int i = 0;i < 26;i++) {int v = t[u].son[i];int Fail = t[u].fail;if(!v) {t[u].son[i] = t[Fail].son[i];continue; //直接让不存在的儿子指向失配位置}if(Fail == 0 && t[Fail].son[i] == v) t[v].fail = 0;else t[v].fail = t[Fail].son[i];q.push(v);}}
}void build() {for(int i = 0;i <= cnt;i++) {for(int j = 0;j < 26;j++) {if(!t[i].val && !t[t[i].son[j]].val) {M.m[i][t[i].son[j]]++;}}}
}Matrix mul(Matrix A,Matrix B) {Matrix C;memset(C.m,0,sizeof(C.m));for(int i = 0;i <= cnt;i++) {for(int j = 0;j <= cnt;j++) {C.m[i][j] = 0;for(int k = 0;k <= cnt;k++) {C.m[i][j] = (C.m[i][j] + A.m[i][k] * B.m[k][j]) % mod;}}}return C;
}Matrix Pow(Matrix A,ll n) {Matrix res;memset(res.m,0,sizeof(res.m));for(int i = 0;i <= cnt;i++) res.m[i][i] = 1;while(n) {if(n & 1) {res = mul(res,A);}A = mul(A,A);n >>= 1;}return res;
}int main() {init();scanf("%lld %d",&m,&n);for(int i = 1;i <= n;i++) {int x;scanf("%d",&x);scanf("%s",s);insert(s);}getFail();build();Matrix res = Pow(M,m);ll ans = 0;for(int i = 0;i <= cnt;i++) {ans = (ans + res.m[0][i]) % mod;}printf("%lld\n",ans);return 0;
}

ICPC Central Europe Regional Contest 2019 K. K==S(AC自动机+矩阵快速幂)相关推荐

  1. Central Europe Regional Contest 2019 J. Saba1000kg (并查集+根号讨论)

    链接:https://ac.nowcoder.com/acm/contest/7817/I 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言524 ...

  2. 2021 ICPC Southeastern Europe Regional Contest Werewolves(树上背包)

    2021 ICPC Southeastern Europe Regional Contest Werewolves(树上背包) 链接 题意:给出一个n个节点的树(n≤3000n\le3000n≤300 ...

  3. ICPC Central Russia Regional Contest (CRRC 19)

    ICPC Central Russia Regional Contest (CRRC 19) 文章目录 ICPC Central Russia Regional Contest (CRRC 19) A ...

  4. 2021 ICPC Southeastern Europe Regional Contest(更新至六题)

    2021 ICPC Southeastern Europe Regional Contest A题签到 A. King of String Comparison 题意:给两个字符串,找出有多少对(l, ...

  5. 2015多校10 1006.CRB and Puzzle HDU5411(邻接矩阵求k长路条数,矩阵快速幂

    题意:有若干字符,现在要把它们连成一个字符串,每种字符后面只能接特定种类的字符,现在询问能连接出的长度小于等于m的字符串有多少种. 思路:我们可以把这个转移关系看成一个图,如果字符a后面可以接b,那么 ...

  6. gym 102302 2019 USP-ICMC H-Log Concave Sequences (dp + 矩阵快速幂优化)

    题目: 传送门 思路:       我们可以先写出转移方程,发现该方程是一个不变的递推式,我们考虑用矩阵快速幂来优化这个递推式. 完结撒花- AC_Code #include <iostream ...

  7. Central Europe Regional Contest 2012 Problem c: Chemist’s vows

    字符串处理的题目: 学习了一下string类的一些用法: 这个代码花的时间很长,其实可以更加优化: 代码: 1 #include<iostream> 2 #include<strin ...

  8. 2021 ICPC Southeastern Europe Regional Contest ABFGJKLN

    |–>传送门<–| A. King of String Comparison 双指针 给定字符串AAA和BBB,计算满足Asub[l,r]A_{sub[l, r]}Asub[l,r]​字典 ...

  9. 2021 ICPC Southeastern Europe Regional Contest 树上dfs+思维

    |–>传送门<–| 题目大意 给定一颗nnn个节点的树,可以从任意一个点开始dfsdfsdfs,求最小字典序的后续遍历. 题解 首先,对于字典序最小的要求,我们一定是从叶结点中的最小值开始 ...

最新文章

  1. 变量与字符串的连接 - format、格式化字符串
  2. 学习的过程和挖矿其实很像
  3. PHP操作redis
  4. C++中dynamic_cast的简介
  5. Magento的基本架构
  6. [k8s]debug模式启动集群k8s常见报错集合(on the fly)
  7. Linux故障解决(2)——使用yum安装netcat 及报错问题解决
  8. java生成world文件_Hello World 项目创建与项目配置文件介绍
  9. 非极大值抑制_OpenCV非极大值抑制bug
  10. 计算机桌面右键没有属性,Win7系统右键计算机属性不见了怎么办?
  11. 图像频域处理之高斯滤波器
  12. 时域信号处理——时域采样(抽样)
  13. 免校准的电量计量芯片_请问电能计量芯片CS5463增益如何校准?
  14. 浅谈外网通过反向代理访问内网资源时的权限保护
  15. Mstar 光机遥控器适配
  16. 计算机保研-中科院计算所霸面(笔试面试)
  17. cdn连接失败是什么意思_CDN经常连接失败的原因有哪些?
  18. Android微信App 分享功能调整,Android 微信SDK分享功能(1)
  19. Ultra Fast Structure-aware Deep Lane Detection论文解读
  20. 如何在 XMind 中输入数学方程?LaTeX 简易入门

热门文章

  1. 【Css】移动端用flex实现DIV高度自适应屏幕和带滚动条效果(代码示例)
  2. 双卡双待获取正在联网的运营商的IMSI
  3. [附源码]Java计算机毕业设计SSMjspm计算机学院党员积分管理系统
  4. 2021年月薪多少,才能在北上广深“体面”生活?
  5. Unity连接Mysql数据库
  6. MySQL查询累计值的方法
  7. 通过JS生成蜂巢背景图(六边形背景图)
  8. Oracle清空表内所有数据
  9. 常见的对象数据库有哪些
  10. vmware 强制关闭后无法启动+启动后黑屏