Separate String

时间限制: 1 Sec  内存限制: 128 MB
提交: 50  解决: 16

题目描述

You are given a string t and a set S of N different strings. You need to separate t such that each part is included in S.

For example, the following 4 separation methods satisfy the condition when t=abab and S={a,ab,b}.

a,b,a,b
a,b,ab
ab,a,b
ab,ab
Your task is to count the number of ways to separate t. Because the result can be large, you should output the remainder divided by 1,000,000,007.

输入

The input consists of a single test case formatted as follows.

N
s1
:
sN
t
The first line consists of an integer N (1≤N≤100,000) which is the number of the elements of S. The following N lines consist of N distinct strings separated by line breaks. The i-th string si represents the i-th element of S. si consists of lowercase letters and the length is between 1 and 100,000, inclusive. The summation of length of si (1≤i≤N) is at most 200,000. The next line consists of a string t which consists of lowercase letters and represents the string to be separated and the length is between 1 and 100,000, inclusive.

输出

Calculate the number of ways to separate t and print the remainder divided by 1,000,000,007.

样例输入

3
a
b
ab
abab

样例输出

4

题解:在kuangbin的板子当中增加ln(以此为结尾的字符串的长度),true_nex(也可以叫做true_fail,用于表示真正有效的fail值,即存在以此为结尾的字符串)数组;
true_nex数组层层计算即可,因为上层的true_nex肯定已被正确计算。然后在query函数里面跑一个简单的DP即可。 AC代码:
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+;
const int maxn=1e6+;
ll dp[maxn];
struct Trie
{
int nxt[][],fail[],endd[],ln[],true_nex[];
int root,L;
int newnode(){
for(int i=;i<;i++){
nxt[L][i]=-;
}
true_nex[L]=;
ln[L]=;
endd[L++]=;
return L-;
}
void init(){
L=;
root=newnode();
}
void insert(char buf[])
{
int len=strlen(buf);
int now=root;
for(int i=;i<len;i++){
if(nxt[now][buf[i]-'a']==-){
nxt[now][buf[i]-'a']=newnode();
ln[nxt[now][buf[i]-'a']]=ln[now]+;
}
now=nxt[now][buf[i]-'a'];
}
endd[now]++;
}
void build()
{
queue<int> Q;
fail[root]=root;true_nex[root]=root;
for(int i=;i<;i++){
if(nxt[root][i]==-){
nxt[root][i]=root;
}
else{
fail[nxt[root][i]]=root;
true_nex[nxt[root][i]]=root;/**/
Q.push(nxt[root][i]);
}
}
while(!Q.empty())
{
int now=Q.front();
Q.pop();
for(int i=;i<;i++)
{
if(nxt[now][i]==-){
nxt[now][i]=nxt[fail[now]][i];
}
else{
fail[nxt[now][i]]=nxt[fail[now]][i];
if(endd[nxt[fail[now]][i]]>) true_nex[nxt[now][i]]=nxt[fail[now]][i];/**/
else true_nex[nxt[now][i]]=true_nex[nxt[fail[now]][i]];/**/
Q.push(nxt[now][i]);
}
}
}
}
ll query(char buf[])
{
int len=strlen(buf);
int now=root;
int res=;
dp[]=;
for(int i=;i<len;i++)
{
now=nxt[now][buf[i]-'a'];
int temp=now;
while(temp!=root)
{
dp[i+]=(dp[i+]+dp[i+-ln[temp]]*endd[temp])%mod;
temp=true_nex[temp];
}
}
return dp[len];
}
}ac;
int n;
char buf[maxn];
int main()
{
int t=;
for(;t;t--)
{
scanf("%d",&n);
ac.init();
while(n--)
{
scanf("%s",buf);
ac.insert(buf);
}
ac.build();
scanf("%s",buf);
printf("%lld\n",ac.query(buf));
}
return ;
}
 

Separate String的更多相关文章

  1. 10 Things Every Java Programmer Should Know about String

    String in Java is very special class and most frequently used class as well. There are lot many thin ...

  2. 4 .Swift函数|闭包

    在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...

  3. URL Parsing

    [URL Parsing] urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True) Parse a URL into six ...

  4. Swift函数|闭包

    在编程中,我们常把能完成某一特定功能的一组代码,并且带有名字标记类型叫做函数,在C语言中,我们知道函数名就是一个指针,它指向了函数体内代码区的第一行代码的地址,在swift中也具有同样的功效. 在Sw ...

  5. .NET 并行编程——任务并行

    本文内容 并行编程 任务并行 隐式创建和运行任务 显式创建和运行任务 任务 ID 任务创建选项 创建任务延续 创建分离的子任务 创建子任务 等待任务完成 组合任务 任务中的异常处理 取消任务 Task ...

  6. bsdasm

    bsdasm 来源 http://www.int80h.org/bsdasm/ Preface by G. Adam StanislavWhiz Kid Technomagic Assembly la ...

  7. php数组与字符串转换

    1.将字符串转换成数组的几个函数: (1)explode(separate,string) 示例:$str = "Hello world It's a beautiful day" ...

  8. Base Conversion In PHP and javascript

    http://www.exploringbinary.com/base-conversion-in-php-using-built-in-functions/ http://www.binarycon ...

  9. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

随机推荐

  1. 使用聚集索引和非聚集索引对MySQL分页查询的优化

    内容摘录来源:MSSQL123 ,lujun9972.github.io/blog/2018/03/13/如何编写bash-completion-script/ 一.先公布下结论: 1.如果分页排序字 ...

  2. Spring AOP常见面试题

    一.AOP是什么? 与OOP对比,面向切面,传统的OOP开发中的代码逻辑是至上而下的过程中会长生一些横切性问题,这些横切性的问题和我们的主业务逻辑关系不会散落在代码的各个地方,造成难以维护,AOP的编 ...

  3. Linux安装软件的时候出现乱码?

    在Linux的中文操作系统下使用xmanager进行软件安装的时候,可能出现乱码界面,可以通过以下方法进行解决 1 修改环境属性  vi /etc/sysconfig/i18n LANG=" ...

  4. CTF中PHP反序列化和命令注入的一次简单利用

    代码来自第六届防灾科技学院网络安全技能大赛,侵删. 目标 获取Linux服务器根目录下的flag 代码 /*home.php*/ class home{ private $method; privat ...

  5. Linux设备驱动程序 之 内核符号表

    insmod使用公共内核符号表来解析模块中未定义的符号.功能内核符号表中包含了所有全局内核项(函数和变量)的地址,这是实现模块化驱动程序所必须的.当模块装载到内核后,它所导出的任何符号都会变成内核符号 ...

  6. ubuntu18.04安装mysql以及重置密码创建新用户

    1.安装mysqlsudo apt-get install mysql-serversudo apt-get install mysql-clientsudo apt-get install libm ...

  7. <javaScript>谈谈JavaScript中的变量、指针和引用

    1.变量我们可能产生这样一个疑问:编程语言中的变量到底是什么意思呢?事实上,当我们定义了一个变量a时,就是在存储器中指定了一组存储单元,并将这组存储单元命名为a.变量a的值实际上描述的是这组存储单元中 ...

  8. Oracle常用CURD

    -------------------------------------------------------------------------------------通用函数和条件判断函数 使用N ...

  9. Java端使用Batik将SVG转为PNG

    在上篇中,我们需要将Highcharts生成的图通过后台保存到pdf文件中,就需要对SVG进行转换. 这里就介绍一下使用Batik处理SVG代码的方法. 首先是jar包的获取地址,https://xm ...

  10. Android 单元测试学习计划

    网上查了一下Android单元测试相关的知识点,总结了一个学习步骤: 1. 什么是单元测试2. 单元测试正反面: 2.1. 重要性 2.2. 缺陷 2.3. 策略3. 单元测试的基础知识: 3.1. ...