给出一个长度为N的小写字母串,现在Mirko有M个若干长度为Li字符串。现在Mirko要用这M个字符串去覆盖给出的那个字符串的。覆盖时,必须保证:
1.Mirko的字符串不能拆开,旋转;
2.Mirko的字符串必须和给出的字符串的某一连续段完全一致才能覆盖,
3.若干次覆盖可以部分重叠
4.Mirko的字符串可以无限使用。
求给出的字符串当中,有多少个字母是无法覆盖的。

小朋友们,作为一名长者,我认为我有必要向你们传授一些人生的经验~;

字符串的一堆函数,慎用慎用;

本人只因没有仔细认真,把strlen(s),strlen(ch)之类的函数写在了循环的里面,造成了无限TLE的惨剧;

图如下:

两个strlen在里面的时候:

#01: Accepted (15ms, 322696KB)
#02: Accepted (15ms, 322696KB)
#03: Accepted (187ms, 322696KB)
#04: Accepted (3234ms, 322696KB)
#05: Accepted (3453ms, 322624KB)
#06: Time Limit Exceeded (?, 322624KB)
#07: Time Limit Exceeded (?, 322624KB)
#08: Time Limit Exceeded (?, 322624KB)
#09: Time Limit Exceeded (?, 322624KB)
#10: Time Limit Exceeded (?, 322624KB)
#11: Time Limit Exceeded (?, 322624KB)
#12: Wrong Answer (5000ms, 322624KB)
#13: Wrong Answer (5000ms, 322624KB)
#14: Time Limit Exceeded (?, 322624KB)
#15: Time Limit Exceeded (?, 322624KB)
#16: Time Limit Exceeded (?, 322624KB)
#17: Wrong Answer (5000ms, 322624KB)
#18: Wrong Answer (5000ms, 322624KB)
#19: Time Limit Exceeded (?, 322624KB)

不知道的会以为我暴力枚举;

一个strlen在循环里的时候:

#01: Accepted (15ms, 345056KB)
#02: Accepted (0ms, 345056KB)
#03: Accepted (0ms, 345056KB)
#04: Accepted (218ms, 345056KB)
#05: Accepted (78ms, 345056KB)
#06: Accepted (93ms, 345056KB)
#07: Accepted (46ms, 345056KB)
#08: Accepted (1656ms, 345056KB)
#09: Time Limit Exceeded (?, 345056KB)
#10: Accepted (93ms, 344984KB)
#11: Accepted (125ms, 344984KB)
#12: Accepted (1562ms, 344984KB)
#13: Time Limit Exceeded (?, 344984KB)
#14: Accepted (171ms, 344984KB)
#15: Accepted (500ms, 344984KB)
#16: Accepted (1640ms, 344984KB)
#17: Wrong Answer (5000ms, 344984KB)
#18: Accepted (250ms, 344984KB)
#19: Accepted (734ms, 344984KB)

还是有三组超了;

没有strlen在里面的时候:

#01: Accepted (0ms, 357712KB)
#02: Accepted (0ms, 357712KB)
#03: Accepted (15ms, 357712KB)
#04: Accepted (78ms, 357712KB)
#05: Accepted (93ms, 357712KB)
#06: Accepted (46ms, 357712KB)
#07: Accepted (46ms, 357712KB)
#08: Accepted (375ms, 357712KB)
#09: Accepted (187ms, 357712KB)
#10: Accepted (93ms, 357712KB)
#11: Accepted (31ms, 357712KB)
#12: Accepted (359ms, 357712KB)
#13: Accepted (328ms, 357712KB)
#14: Accepted (187ms, 357712KB)
#15: Accepted (125ms, 357712KB)
#16: Accepted (359ms, 357712KB)
#17: Accepted (468ms, 357712KB)
#18: Accepted (281ms, 357712KB)
#19: Accepted (140ms, 357712KB)

敲完这道题,我热泪盈眶啊;

同时,我通过这道题敲了好几遍AC自动机模板;

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define LL long long
const int maxn=;
int n,m;
char s[],ch[];
bool vis[][][][][];
int linkk[][],flag[maxn],f[maxn],q[maxn],g[maxn],k[maxn],tail=,head=,len=,next[maxn];
void insert(){
int now=,p=strlen(ch);
for(int i=;i<p;i++){
if(!linkk[now][ch[i]])linkk[now][ch[i]]=++len,g[len]=ch[i];
now=linkk[now][ch[i]];
if(i==p-)flag[now]=p;
}
}
void init(){
//printf("first:%d\n",clock());
scanf("%d%s%d",&n,s,&m);
for(int i=;i<n;i++)s[i]=s[i]-'a'+;
for(int i=;i<n;i++)vis[s[i-]][s[i-]][s[i-]][s[i-]][s[i]]=;
//printf("second:%d\n",clock());
for(int i=;i<=m;i++){
scanf("%s",ch);
bool p=;
int u=strlen(ch);
for(int j=;j<u;j++)ch[j]=ch[j]-'a'+;
for(int j=;j<u;j++)
if(!vis[ch[j-]][ch[j-]][ch[j-]][ch[j-]][ch[j]]){p=;break;}
if(!p)insert();
}
//printf("init:%d\n",clock());
}
void bfs(){
head=,tail=;q[++tail]=;int x=,now=,temp;
while(++head<=tail){
x=q[head];
for(int i=;i<=;i++){
if(linkk[x][i]){
now=linkk[x][i],temp=f[x];
if(x){
while(temp&&!linkk[temp][i])temp=f[temp];
f[now]=linkk[temp][i];
if(flag[f[now]])next[now]=f[now];
else next[now]=next[f[now]];
}
q[++tail]=now;
}
}
}
}
void work(){
bfs();
//printf("bfs:%d\n",clock());
int now=;
for(int i=;i<n;i++){
if(!linkk[now][s[i]]){
int temp=f[now];
while(!linkk[temp][s[i]]&&temp)temp=f[temp];
now=linkk[temp][s[i]];
}
else now=linkk[now][s[i]];
int temp=now;
while(temp){
if(flag[temp])k[i-flag[temp]+]=flag[temp];
temp=next[temp];
}
}
//printf("work:%d\n",clock());
int last=-,sum=;
for(int i=;i<n;i++){
if(k[i])last=max(last,k[i]+i-);
if(i>last)sum++;
}
cout<<sum<<endl;
//cout<<clock()<<endl;
}
int main(){
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
init();
work();
}

[coci2012]覆盖字符串 AC自动机的更多相关文章

  1. JZYZOJ1369 [coci2012]覆盖字符串 AC自动机

    http://172.20.6.3/Problem_Show.asp?id=1369 trie树如果不优化就这么往里面放这么多单词肯定超空间+超时,所以需要去掉无用的字符串(不属于原字符串的),但是一 ...

  2. 模板—字符串—AC自动机(多模式串,单文本串)

    模板—字符串—AC自动机(多模式串,单文本串) Code: #include <queue> #include <cstdio> #include <cstring> ...

  3. 【bzoj1030】: [JSOI2007]文本生成器 字符串-AC自动机-DP

    [bzoj1030]: [JSOI2007]文本生成器 首先把匹配任意一个的个数的问题转化为总个数-没有一个匹配的个数 先构造AC自动机,然后枚举每一位的字母以及在自动机上的位置 f[i][j]为第i ...

  4. 【bzoj3172】: [Tjoi2013]单词 字符串-AC自动机

    [bzoj3172]: [Tjoi2013]单词 先用所有单词构造一个AC自动机 题目要求的是每个单词在这个AC自动机里匹配到的次数 每次insert一个单词的时候把路径上的cnt++ 那么点p-&g ...

  5. 字符串——AC自动机

    目录 一.前言 二.思路 三.代码 四.参考资料 一.前言 以前一直没学AC自动机,主要是被名字吓到了,自动AC,这么强的名字肯定很难,学了后才发现,其实不难. AC自动机并不是Acept autom ...

  6. 【bzoj2434】: [Noi2011]阿狸的打字机 字符串-AC自动机-BIT

    [bzoj2434]: [Noi2011]阿狸的打字机 x串在y串上的匹配次数就是y在自动机所有节点上能够通过fail走到x最后一个节点的个数 (就是y串任意一个前缀的后缀能匹配到x的个数)和[bzo ...

  7. bzoj 3172 单词 ac自动机|后缀数组

    题目大意: 给定n个字符串连成了一篇文章,问每个字符串在这篇文章中出现的次数,可重复覆盖 这里ac自动机和后缀数组都可以做 当然后缀数组很容易就解决,但是相对时间消耗高 这里就只讲ac自动机了 将每个 ...

  8. AC自动机相关Fail树和Trie图相关基础知识

    装载自55242字符串AC自动机专栏 fail树 定义 把所有fail指针逆向,这样就得到了一棵树 (因为每个节点的出度都为1,所以逆向后每个节点入度为1,所以得到的是一棵树) 还账- 有了这个东西, ...

  9. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

随机推荐

  1. [java基础] 001 - 记一次堆栈溢出异常(StackOverFlowError)

    上午经理发来一个任务,解决某个接口异常,此接口第一次调用成功返回: {ret=Y, orderResultList=[{itemno=31920190521083622032, sub_msg=成功, ...

  2. GOF 23种设计摩搜-建造者模式

    • 场景: – 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样 一个问题需要处理: • 装配这些子组件是不是有个步骤问题? – 实际开发中,我们所需要的对象构建时 ...

  3. [反汇编练习] 160个CrackMe之029

    [反汇编练习] 160个CrackMe之029. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

  4. ann搜索算法(Approximate Nearest Neighbor)

    ANN的方法分为三大类:基于树的方法.哈希方法.矢量量化方法.brute-force搜索的方式是在全空间进行搜索,为了加快查找的速度,几乎所有的ANN方法都是通过对全空间分割,将其分割成很多小的子空间 ...

  5. Allegro基本操作——PCB布线

    转:http://blog.sina.com.cn/s/blog_1538bc9470102vyyq.html http://www.elecfans.com/article/80/110/2010/ ...

  6. 笔记13 winform

    一:panel 和 tablelayoutpanel,FlowLayoutPanel FlowLayoutPanel和TableLayoutPanel是.NET Framework的新增控件.顾名思义 ...

  7. python--网络编程--socket

    网络通信标准---网络协议 互联网协议--osi七层协议 五层协议:应用层:应用层.表示层.会话层          传输层:传输层    网络层:网络层    数据链路层:数据链路层    物理层: ...

  8. python--函数嵌套 命名空间

    函数的嵌套调用 def func(): print(123) def func2(): func() print(345) func2() #结果:123 345 函数的嵌套定义 def func() ...

  9. canvas转盘抽奖的实现(一)

    网络上已经有了很多转盘抽奖的代码,但大多是用jQuery插件实现的,其中的原理比较难弄明白,于是自己摸索了一个.最终效果如下:     // = totalTime) { stopRotation() ...

  10. dubbo 实战1

    准备:先安装zookeeper,然后进入一下步骤 服务端 1.新建 maven项目 2.pom.xml 内容如下 <?xml version="1.0" encoding=& ...