【题目链接】:

https://loj.ac/problem/10053

【题意】:

给出n个模式串。请问文本串是由多少个模式串组成的。

【题解】:

当我学完AC自动机后,发现这个题目也太简单了吧.

不过当时我还是不会,后来看了看洛谷有位大佬的题解。

简直醍醐灌顶。纯Trie树也能写出这个题来。

就是把对应的位置标记上,好比dp的转移。

这个转移是,开一个Mark数组,看看文本串中哪个位置能通过这些模式串匹配出来的。

然后一直转移即可。

【代码】:

 //L语言
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 3e4 ;
const int M = 3e4 * ;
const int LEN = 2e6;
int Son[M][],idx;
bool cnt[M];
bool Mark[LEN];
char s[LEN];
int n,m;
void Insert(){
int p = ;
for(int i=;s[i];i++){
int t = s[i]-'a';
if( !Son[p][t] ) Son[p][t] = ++idx;
p = Son[p][t];
}
cnt[p] = true;
}
void Query(){
memset( Mark , false , sizeof Mark );
int p = ;
for(int i=;s[i];i++){
int t = s[i] - 'a';
if ( !Son[p][t] ) break;
p = Son[p][t];
if( cnt[p] ) Mark[i] = true;
} int ans = ;
for(int i= ; s[i] ; i++ ){
if( !Mark[i] ) continue;
else ans = i+ ;
p = ;
for(int j=i+;s[j];j++){
int t = s[j] - 'a';
if ( !Son[p][t] ) break;
p = Son[p][t];
if( cnt[p] ) Mark[j] = true;
}
}
printf("%d\n",ans);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%s",s);
Insert();
}
for(int i=;i<m;i++){
scanf("%s",s);
Query();
}
return ;
}

【Trie】L 语言的更多相关文章

  1. 【BZOJ1212】[HNOI2004]L语言 Trie树

    [BZOJ1212][HNOI2004]L语言 Description 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构 ...

  2. BZOJ 1212: [HNOI2004]L语言( dp + trie )

    因为单词很短...用trie然后每次dp暴力查找...用哈希+dp应该也是可以的.... ------------------------------------------------------- ...

  3. 洛谷:P2292 [HNOI2004]L语言(DP+Trie树)

    P2292 [HNOI2004]L语言 题目链接:https://www.luogu.org/problemnew/show/P2292 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有 ...

  4. Luogu P2292 [HNOI2004]L语言(Trie+dp)

    P2292 [HNOI2004]L语言 题面 题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章 \(T\) 是由若干小写字母构成. ...

  5. 2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机)

    2021.11.09 P2292 [HNOI2004]L语言(trie树+AC自动机) https://www.luogu.com.cn/problem/P2292 题意: 标点符号的出现晚于文字的出 ...

  6. BZOJ 1212: [HNOI2004]L语言 [AC自动机 DP]

    1212: [HNOI2004]L语言 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1367  Solved: 598[Submit][Status ...

  7. [HNOI2004]Language L语言

    2777: [HNOI2004]Language L语言 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 10  Solved: 5[Submit][S ...

  8. 「HNOI2004」「LuoguP2292」L语言(AC自动机

    题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...

  9. 洛谷(cogs 1293/bzoj 1212) P2292 [HNOI2004]L语言

    1293. [HNOI2004] L语言 ★★★   输入文件:language.in   输出文件:language.out   简单对比时间限制:1 s   内存限制:162 MB [题目描述] ...

随机推荐

  1. 大整数乘法(c++)【转载】

    摘自<c++数据结构原理与经典问题求解> #include #include #include using namespace std; //返回位数为size1+size2 int* m ...

  2. centos7 设置 tomcat 开机自启

    1.创建文件 # vi /etc/systemd/system/tomcat.service [Unit] Description=Tomcat8540 After=syslog.target net ...

  3. 移动端—— 兼容PC端,移动端的点击事件

    移动设备上不支持鼠标事件,好在webkit内核的移动浏览器支持 touch 事件,所以触摸事件是移动应用中所必须的.touchstart.touchmove.touchend事件可以类比于moused ...

  4. 源码编译vi过程中进行配置时报“checking if compile and link flags for Python are sane... no: PYTHON DISABLED”怎么办?

    答: 需要安装python开发库(如果不安装这个库,那么在配置时执行./configure --enable-pythoninterp=yes将不会生效,以至于vi的python特性并没有被开启) 如 ...

  5. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化

    把resource拷贝到test目录下 只保留文件夹结构和test1.ftl这个模板文件就可以了. 新建一个包 编写测试类 使用freemaker提供的方法生成静态文件 Configuration是i ...

  6. java.lang.IllegalStateException: No primary or default constructor found for class java.time.LocalDate

    转载自:https://blog.csdn.net/Coder_Arley/article/details/81910705 springboot中报错如下: springmvc也可以使用类似处理方法 ...

  7. LeetCode_14. Longest Common Prefix

    14. Longest Common Prefix Easy Write a function to find the longest common prefix string amongst an ...

  8. JAVA 基础编程练习题46 【程序 46 字符串连接】

    46 [程序 46 字符串连接] 题目:两个字符串连接程序 package cskaoyan; public class cskaoyan46 { public static void main(St ...

  9. sequelize 字段无法操作

    sequelize 操作的字段都必须先  define ,不然无法操作

  10. img标签替换为mip-img标签的方法

    function replaceMipImages($content){ preg_match_all('/<img (.*?)\>/', $content, $images); if(! ...