其实这题不超时完全是因为串长度太小,如果串够长,一次匹配后都要往上跳,复杂度是n^2的。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#define mkp make_pair
using namespace std;
const double EPS=1e-;
typedef long long lon;
const lon SZ=,SSZ=,APB=,one=,INF=0x7FFFFFFF,mod=;
int n,cnt,dp[SZ][],pre[SZ],len[SZ];
int nex[SZ][APB],fail[SZ];
struct nd{
int pos,type;
nd(int a=,int b=):pos(a),type(b){}
};
nd qry[SZ];
char ch[SZ],str[]; void add(int x)
{
int cur=;
for(int i=;str[i];++i)
{
int c=str[i]-'a';
if(!nex[cur][c])nex[cur][c]=++cnt;
cur=nex[cur][c];
}
qry[x].pos=cur;
len[cur]=strlen(str+);
} void build()
{
queue<int> q;
q.push();
for(;q.size();)
{
int fr=q.front();
q.pop();
for(int i=;i<APB;++i)
{
int t=nex[fr][i];
if(t)
{
if(!fr)
{
fail[t]=fr;
}
else
{
int u=fail[fr];
for(;u&&!nex[u][i];u=fail[u]);
u=nex[u][i];
fail[t]=u;
}
q.push(t);
}
}
}
} void init()
{
//cin>>n;
scanf("%d",&n);
for(int i=;i<=n;++i)
{
int type;
//cin>>type>>str+1;
scanf("%d",&type);
scanf(" %s",str+);
add(i);
qry[i].type=type;
}
build();
} int getnex(int x,int c)
{
for(;x&&!nex[x][c];x=fail[x]);
x=nex[x][c];
return x;
} void update(int x,int pos)
{
for(;x;x=fail[x])
{
++dp[x][];
if(pos-pre[x]>=len[x])
{
++dp[x][];
pre[x]=pos;
}
}
} void work()
{
int cur=;
for(int i=;ch[i];++i)
{
int c=ch[i]-'a';
cur=getnex(cur,c);
update(cur,i);
}
for(int i=;i<=n;++i)
{
printf("%d\n",dp[qry[i].pos][qry[i].type]);
//cout<<<<endl;
}
} void release()
{
for(int i=;i<=cnt;++i)
{
memset(nex[i],,sizeof(nex[i]));
memset(dp[i],,sizeof(dp[i]));
pre[i]=;
}
cnt=0LL;
} int main()
{
//std::ios::sync_with_stdio(0);
//freopen("d:\\1.txt","r",stdin);
int casenum;
//cin>>casenum;
//cout<<casenum<<endl;
//for(int time=1;time<=casenum;++time)
for(int time=;scanf(" %s",ch+)!=EOF;++time)
{
//cout<<""<<<<endl;
//if(time!=1)
printf("Case %d\n",time);
init();
work();
release();printf("\n");
}
return ;
}

zoj3223的更多相关文章

随机推荐

  1. flask下载文件---文件流

    html: <a name="downloadbtn" class="btn btn-success pull-right" href="/do ...

  2. PKCS RSA执行标准

    RSA是一种算法,但是,在相关应用的时候,还是需要有一些标准的.这就是pkcs.现在的各种程序中,基本都是遵循这个标准来使用RSA的.最近陆续读取RSA相关的内容进行学习. RSA官网:https:/ ...

  3. 将BUG管理工具(禅道)部署到服务器(测试服务器、云服务器)

      禅道是一个开源的项目管理软件,用来记录软件项目的开发过程.bug跟踪以及任务分配,它是基于PHP语言开发的.   https://www.zentao.net/download/80111.htm ...

  4. 从centos镜像创建maven仓库

    创建镜像 1. 使用centos7作为基础镜像2. 将jdk1.8(官方要求1.8)和nexus3解压后的两个文件放进cp进去3. export环境变量后启动一下试一试4. docker commit ...

  5. qemu中的内存管理

    qemu负责模拟虚机的外设,因此虚机的线性地址空间主要由qemu进行管理,也就是确定线性地址空间中哪段地址属于哪个设备或者DRAM或者其他的什么. 1.数据结构 1.RAMBLOCK (最直接接触ho ...

  6. Porsche Piwis Tester II V15.6 with CF30 Laptop or Lenovo E49AL Laptop

    Some of my customers let me recommended which auto diagnostic tool is good for Porsche , I recommend ...

  7. 2个监听器+ dialog + replysubject + extends

    import 'package:flutter/material.dart'; import 'package:rxdart/rxdart.dart'; import 'package:flutter ...

  8. 设计模式理解(八)结构型——装饰者模式(记得加上UML图 --- 未完)

    一段时间没写,又忘了,晕...设计模式这种东西一定要经常用. 装饰者模式(Decorator)用于动态地给一个对象增加一些额外的职责,就增加功能来说,他比生成子类更为灵活. 装饰者模式的原则是: 能用 ...

  9. SAP 打印自动导出PDF本地文件(自己做的例子)

    smartforms导出PDF: 常用函数有: convert_otfconvert_otfspooljob_2_pdfconvert_abapspooljob_2_pdfCONVERT_OTF_2_ ...

  10. python爬取全名k歌

    python3 爬取全名k歌 代码: import re import requests import ssl ssl._create_default_https_context=ssl._creat ...