给定p个模式串,求长度为m<=50的串中不包含任何模式串的串的种类数,字符仅由给出的n个字符构成,用mp数组标记下。然后和之前的几道类似,利用end和next数组得到转态转移数组,然后由于题目数据不像之前的那么大,可以用dp[i][j]来进行状态转移,i代表当前串长度,j为当前状态。但是由于答案很大需要用到高精度。代码如下
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;int N;
int mp[300];struct Matrix
{long long mat[110][110];int n;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 ret = Matrix(n);for(int i=0;i<n;i++)for(int j=0;j<n;j++)for(int k=0;k<n;k++)ret.mat[i][j]+=mat[i][k]*b.mat[k][j];return ret;}
};
struct Tree
{int next[110][256],end[110],fail[110];int L,root;int newnode(){for(int i=0;i<255;i++){next[L][i]=-1;}end[L++]=0;return L-1;}void init(){L=0;root=newnode();}void insert(char *s){int len=strlen(s);int p=root;for(int i=0;i<len;i++){if(next[p][mp[s[i]]]==-1){next[p][mp[s[i]]]=newnode();}p=next[p][mp[s[i]]];}end[p]++;}void build(){queue<int>q;int p=root;fail[root]=root;for(int i=0;i<255;i++){if(next[p][i]==-1){next[p][i]=root;}else{fail[next[p][i]]=root;q.push(next[p][i]);}}while(!q.empty()){p=q.front();q.pop();if(end[fail[p]]) end[p]=1;for(int i=0;i<255;i++){if(next[p][i]==-1){next[p][i]=next[fail[p]][i];}else{fail[next[p][i]]=next[fail[p]][i];q.push(next[p][i]);}}}}void query(char *s){int len=strlen(s);int p=root,ans=0;for(int i=0;i<len;i++){int id=s[i]-'a';p=next[p][id];int temp=p;while(temp!=root){if(end[temp]){ans+=end[temp];end[temp]=0;}temp=fail[temp];}}}Matrix getMatrix(){Matrix a(L);for(int i=0;i<a.n;i++){for(int j=0;j<N;j++){if(end[next[i][j]]==0)a.mat[i][next[i][j]]++;}}return a;}void debug(){for(int i = 0;i < L;i++){printf("id = %3d,fail = %3d,end = %3d,chi = [",i,fail[i],end[i]);for(int j = 0;j < 26;j++)printf("%2d",next[i][j]);printf("]\n");}}
};struct BigInt
{const static int mod = 10000;const static int DLEN = 4;int a[600],len;BigInt(){memset(a,0,sizeof(a));len = 1;}BigInt(int v){memset(a,0,sizeof(a));len = 0;do{a[len++] = v%mod;v /= mod;}while(v);}BigInt(const char s[]){memset(a,0,sizeof(a));int L = strlen(s);len = L/DLEN;if(L%DLEN)len++;int index = 0;for(int i = L-1;i >= 0;i -= DLEN){int t = 0;int k = i - DLEN + 1;if(k < 0)k = 0;for(int j = k;j <= i;j++)t = t*10 + s[j] - '0';a[index++] = t;}}BigInt operator +(const BigInt &b)const{BigInt res;res.len = max(len,b.len);for(int i = 0;i <= res.len;i++)res.a[i] = 0;for(int i = 0;i < res.len;i++){res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);res.a[i+1] += res.a[i]/mod;res.a[i] %= mod;}if(res.a[res.len] > 0)res.len++;return res;}BigInt operator *(const BigInt &b)const{BigInt res;for(int i = 0; i < len;i++){int up = 0;for(int j = 0;j < b.len;j++){int temp = a[i]*b.a[j] + res.a[i+j] + up;res.a[i+j] = temp%mod;up = temp/mod;}if(up != 0)res.a[i + b.len] = up;}res.len = len + b.len;while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--;return res;}void output(){printf("%d",a[len-1]);for(int i = len-2;i >=0 ;i--)printf("%04d",a[i]);printf("\n");}
};Tree ac;
char s[55];
BigInt dp[2][110];
int main()
{int m,p;while(~scanf("%d %d %d",&N,&m,&p)){scanf("%s",s);for(int i=0;i<N;i++)mp[s[i]]=i;ac.init();for(int i=0;i<p;i++){scanf("%s",s);ac.insert(s);}ac.build();Matrix a=ac.getMatrix();int now=0;dp[now][0]=1;for(int i=1;i<a.n;i++){dp[now][i]=0;}for(int i=0;i<m;i++){now^=1;for(int j=0;j<a.n;j++){dp[now][j]=0;}for(int j=0;j<a.n;j++){for(int k=0;k<a.n;k++){if(a.mat[j][k]>0){dp[now][k]=dp[now][k]+dp[now^1][j]*a.mat[j][k];}}}}BigInt ans=0;for(int i=0;i<a.n;i++){ans=ans+dp[now][i];}ans.output();}
}

POJ 1625 Censored!相关推荐

  1. [POJ 1625] Censored! (AC自动机+DP+高精度)

    链接 POJ 1625 题意 给出P个模式串,问长度为M且不含有P中任何一个为子串的字符串有多少种. 给出了大小为N的一个字符集,属于ASCII但不一定为英文字母. 最终答案不进行取模,所以可能非常大 ...

  2. POJ - 1625 Censored!

    希望自己能成为日更博主 题目连接:http://poj.org/problem?id=1625  题目大意:给你n个字母,再给你p个字符串,问有多少个长度为m的只由给出的n个字母构成的字符串,且字符串 ...

  3. POJ 1625 Censored ( Trie图 DP 高精度 )

    题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 : 与 POJ 2778 非常相似的一道题目,如果没有做过就尝试去了解 ...

  4. POJ 1625 Censored! (AC自己主动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自己主动机 + 高精度 + 简单DP. 字符有可能会超过128.用map映射一下就可以. 中间的数太大.得上高精度. 用矩阵高速幂会超时,简单的DP就能解决时间 ...

  5. POJ 1625 Censored! (AC自动机 + 高精度 + DP)

    题目链接:Censored! 解析:AC自动机 + 高精度 + 简单DP. 字符有可能会超过128,用map映射一下即可. 中间的数太大,得上高精度. 用矩阵快速幂会超时,简单的DP就能解决时间的问题 ...

  6. POJ - 1625 Censored!(AC自动机+dp+高精度运算)

    题目链接:点击查看 题目大意:给出一个含有 n 个不同字符的字符集,接着规定所有单词的长度为 m ,再给出 k 个病毒串,问有多少个字符串中不含有病毒串 题目分析:这个题目和之前做过的DNA的那个题有 ...

  7. POJ 1625 Censored!(自动机DP+高精度)

    题意:给出包含n个字符的字符集,以下所提字符串均由该字符集中的字符构成.给出p个长度不超过10的字符串,求长为m且不包含上述p个字符串的字符串有多少个. 数据范围:1<=n,m<=50,0 ...

  8. POJ 1625 Censored!

    辣鸡OI毁我青春 Description The alphabet of Freeland consists of exactly N letters. Each sentence of Freela ...

  9. POJ 1625 Censored!(AC自动机-指针版+DP+大数)题解

    题目:给你n个字母,p个模式串,要你写一个长度为m的串,要求这个串不能包含模式串,问你这样的串最多能写几个 思路:dp+AC自动机应该能看出来,万万没想到这题还要加大数...orz 状态转移方程dp[ ...

最新文章

  1. Lumen / Laravel 5.5 使用网易邮箱 SMTP 发送邮件
  2. 【阿里云总监课第四期】时髦的云原生应用怎么写?
  3. 用树莓派的方式打开小米手机:摇晃手机控制小车,前进后退加转弯,成本不到350元 | 开源...
  4. 后台开发经典书籍--图解http
  5. .py与.pyc文件区别
  6. .net开源框架开源类库(整理)
  7. 作者:朱艳华(1982-),女,中国科学院计算机网络信息中心高级工程师
  8. 训练(training)和推理\推断(inference)的关系?
  9. MSDN上的英语结巴
  10. [note] Homebrew的介绍、安装方法与常用命令整理
  11. 官宣|Apache Flink 1.14.0 发布公告
  12. “添加删除WIndows组件”中没有IIS时安装IIS方法
  13. ◎Hspace 和vspace 的设定
  14. sqlserver 中isnull(Null,0) 和isnull(' ',0) 区别
  15. 项目管理网络图概念总结
  16. 2021-2027全球与中国教育ERP套件软件市场现状及未来发展趋势
  17. 2021-10-11 今日总结
  18. Linux scp和sftp
  19. 知乎上的100条简短深刻的回答
  20. box-shadow上下左右四个边框设置阴影样式

热门文章

  1. 学会自我学习(自律性)
  2. *微信开发内置浏览器JS自动关闭当前页面回到微信对话窗口
  3. 一键加速,华为云CDN加速满足多行业需要
  4. java 处理表情字符_使用轻量级工具emoji-java处理emoji表情字符
  5. python 埋点_网站js埋点
  6. 青软实训-锐聘学院-Oracle作业
  7. 如何在香港主机上尽可能多的建站
  8. 免校准的电量计量芯片_【应用】基于高精度免校准电能计量芯片CSE7761的漏电保护设计,可支持单芯片两路计量...
  9. 55ide游戏引擎(原赤兔引擎)教程1:认识引擎
  10. Visual Studio + VAssistX常用快捷键