jzoj2742. 【PKU1625】Censored!
Description
给出p个长度不超过10的字符串,字符集大小为n。
如果这p个串都不是字符串s的子串,则认为s是幸运的。
求长度为m的幸运串个数。
Input
第一行三个正整数n,m,p。
第二行n个不同的字符,表示字符集,其ASCII码大于32。
接下来p行每行一个字符串,表示不允许被包含的串。
Output
一行一个整数,表示幸运串个数。
Sample Input
2 3 1
ab
bb
Sample Output
5
Data Constraint
对于20%的数据,m ≤ 6,n ≤ 10;
对于50%的数据,p个串的长度不超过4,m ≤ 50,n ≤ 10;
对于100%的数据,n ≤ 50,m ≤ 50,p ≤ 10。
Hint
aaa aab aba baa bab 共5个串为幸运串。
题解
这题首先我们看到找出p字符串不为子串的字符串方案数。
想到什么?AC_Automation of course
如果不会AC_Automation也就是AC自动机的话,先学学也可。
其实这玩意就是trie+kmp。
于是我们把p字符串建立一颗AC自动机,然后在结尾挂上一个标记(注意fail边要下传!!!)
接下来我们考虑DP
设f[i,j]f_{[i,j]}f[i,j]表示当前建立新字符串建到第iii位时,AC自动机上走到了jjj这个位置。
初始化f[0,0]=1f_{[0,0]}=1f[0,0]=1
然后我们枚举i−1i-1i−1走到了jjj状态,再枚举当前位放第kkk个字符。
然后跳fail链,跳到的位置记为lastlastlast。同时判断一下有没有走到标记即可
方程:
f[i,last]+=f[i−1,j]f_{[i,last]}+=f_{[i-1,j]}f[i,last]+=f[i−1,j]
很清真,很简单。
然后出题人就硬是要你套个高精度加法。
时间复杂度:O(m∗n∗len(表示p字符串总大小)∗x(高精度的时间))O(m*n*len(表示p字符串总大小)*x(高精度的时间))O(m∗n∗len(表示p字符串总大小)∗x(高精度的时间))
代码
uses math;
vari,j,k,l,n,m,p,tot:longint;id:array[1..1000] of longint;tree:array[0..5000,0..200] of longint;d,next:array[0..50000] of longint;an,ka,kb:array[1..200] of longint;f:array[0..100,0..200] of ansistring;bz:array[1..5000] of boolean;s,c:string;ans:ansistring;
function jia(a,b:ansistring):ansistring;
vari,j,k,l,lena,lenb,len:longint;c:ansistring;
beginlena:=length(a);lenb:=length(b);if (lena>lenb) or ((lena=lenb) and (a>b)) thenbeginc:=a;a:=b;b:=c;l:=lena;lena:=lenb;lenb:=l;end;j:=1;for i:=1 to lena do begin ka[i]:=ord(a[j])-48; inc(j); end;j:=1;for i:=1 to lenb do begin kb[i]:=ord(b[j])-48; inc(j); end;an:=kb;k:=0;j:=0;l:=lenb;for i:=lena downto 1 dobeginj:=ka[i]+kb[l]+k;k:=j div 10;j:=j mod 10;an[l]:=j;dec(l);end;c:='';while k>0 dobeginif l>0 thenbeginj:=kb[l]+k;k:=j div 10;j:=j mod 10;an[l]:=j;dec(l);endelsebeginc:=c+chr(k+48);break;end;end;for i:=1 to lenb dobeginc:=c+chr(an[i]+48);end;exit(c);
end;
procedure build_ac_automation;
vari,j,k,l,head,tail,took,x,y,dep:longint;
beginhead:=1;tail:=1;took:=1;repeatfor dep:=head to tail dobeginfor i:=1 to n dobeginif tree[d[dep],i]>0 thenbeginy:=tree[d[dep],i];x:=next[d[dep]];while (x>0) and (tree[x,i]=0) do x:=next[x];if d[dep]>0 thenbeginnext[y]:=tree[x,i];end;if y<>tree[x,i] thenbeginif bz[tree[x,i]] then bz[y]:=true;end;inc(took);d[took]:=y;end;end;end;head:=tail+1;tail:=took;until head>tail;
end;
procedure trie(x,i,up:longint);
varj,k,l:longint;
beginif i=up thenbeginbz[x]:=true;exit;end;if tree[x,id[ord(s[i+1])]]>0 thenbegintrie(tree[x,id[ord(s[i+1])]],i+1,up);endelsebegininc(tot);tree[x,id[ord(s[i+1])]]:=tot;trie(tree[x,id[ord(s[i+1])]],i+1,up);end;
end;
begin//assign(input,'consored.in');reset(input);readln(n,m,p);readln(c);tot:=0;for i:=1 to n dobegininc(tot);id[ord(c[i])]:=tot;tree[0,tot]:=tot;end;for i:=1 to p dobeginreadln(s);trie(0,0,length(s));end;build_ac_automation;f[0,0]:='1';for i:=1 to m dobeginfor j:=0 to tot dobeginif (length(f[i-1,j])>0) thenbeginfor k:=1 to n dobeginl:=j;while (l>0) and (tree[l,k]=0) do l:=next[l];if not bz[tree[l,k]] thenbeginif (length(f[i,tree[l,k]])=0) then f[i,tree[l,k]]:='0';f[i,tree[l,k]]:=jia(f[i,tree[l,k]],f[i-1,j]);end;end;end;end;end;for i:=1 to tot dobeginans:=jia(ans,f[m,i]);end;writeln(ans);
end.
jzoj2742. 【PKU1625】Censored!相关推荐
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
- 【编程语言】Scala 函数式编程
函数是Scala 中的一等公民. 本文讨论Scala函数式编程的一些基本原理.你将会学到如何使用高阶函数,以及重用已有代码时,遵守 DRY 原则. Scala 的集合库很棒 # So what doe ...
- 【CentOS】利用Kubeadm部署Kubernetes (K8s)
[CentOS]利用Kubeadm部署Kubernetes (K8s)[阅读时间:约10分钟] 一.概述 二.系统环境&项目介绍 1.系统环境 2.项目的任务要求 三.具体实验流程 1 系统准 ...
- 【Spring】框架简介
[Spring]框架简介 Spring是什么 Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IOC(Inverse Of Control:反转控制)和AOP(Asp ...
- 【C#】类——里式转换
类是由面对对象程序设计中产生的,在面向结构的程序设计例如C语言中是没有类这个概念的!C语言中有传值调用和传址调用的两种方式!在c语言中,主方法调用方法,通过传递参数等完成一些操作,其中比较常用的的数据 ...
- 【C#】Out与ref是干什么的?
关于return: 1.最后没有写 return 语句的话,表示程序正常退出 2.不需要返回值时,存在return的作用 例子 void main() {return; //return退出该程序的作 ...
- 【软件工程】RUP与软件开发5大模型
软件开发的5大模型 1.瀑布模型:按照人的思维一步一步的开发下去,如果需求分析得当,每个阶段顺利,结果还不错! 2.快速原型模型:后来人们发现,自己不可能一下子就把所有的需求搞清楚,总是在开发的过程中 ...
- 【VB】学生信息管理系统5——数据库代码
这次学生信息管理系统在代码的理解过程中遇到了一些问题.总结如下: 1. sql server的安装过程各个步骤的意思.在安装SQL Server的时候按照网上的步骤,我觉得这个需要学完整个数据库再返回 ...
- 白化(预处理步骤)【转】
白化(预处理步骤)[转] 介绍 我们已经了解了如何使用PCA降低数据维度.在一些算法中还需要一个与之相关的预处理步骤,这个预处理过程称为白化.举例来说,假设训练数据是图像,由于图像中相邻像素之间具有很 ...
最新文章
- 初中数学分几个模块_【初中数学】8大模块61个必考易错知识点!
- 获取列表中最大N个数的索引
- 启明云端分享 | Sigmastar SSD201\SSD202D核心板在智能料理机方案的应用(4.3寸彩屏应用)
- centos 下载 哪个版本_生信分析平台搭建(十六):CentOS
- 清北NOIP训练营集训笔记——图论(提高组精英班)
- 实际开发---php时间函数
- centOS 6 rpm安装MySQL小记
- c# 命名空间命名规范_C#命名空间能力问题和解答 套装2
- Linux 添加alisa,Linux/shell命令的实际应用——alias增加自定义命令
- Python使用字典get()方法TypeError: get() takes no keyword arguments
- javascript中的计时器
- SOLIDWORKS软件二十四年来的进化发展史
- Pygame安装方法(Windows10, Python-3.7.2)
- 比“虚幻4”还牛逼的游戏引擎 到底有多神奇?
- WIN10计算机不支持3D游戏怎么办,教你如何在win10系统中开启3D加速?
- Blizzard的MPQ文件格式搜索算法
- 评价效率DEA方法原理
- 第八届ACM趣味编程循环赛重现赛(部分题目)
- 从零开始学MATLAB(一)MATLAB基础知识
- 我用python代码表白,泡到了大学小师妹
热门文章
- 筑巢引凤、珠联璧合、潜龙出海,蚂蚁金服开放平台如何将“开放”做好?
- Java集合类ArrayList线程不安全验证和解决
- android 通知栏授权,Android 消息通知栏Notification使用和权限
- 微信公众号推送天气教程,自动定时推送【Java版】开发者和小白详细教程
- 麦田的守望者背景与分析
- GDC API下载肿瘤数据
- 一种NET软件加壳技术的设计与实现
- Jquery颜色选择插件使用
- 简洁大气资源站emlog模板
- 主干(trunk)、分支(branch )、标记(tag) 用法示例 + 图解