zoj3223
其实这题不超时完全是因为串长度太小,如果串够长,一次匹配后都要往上跳,复杂度是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的更多相关文章
随机推荐
- IOT
文档地址:http://www.owasp.org.cn/owasp-project/owasp-things?searchterm=iot 今天面试,面试官问到iot
- apache24虚拟安装
1.进入Apache的conf目录 2.打开httpd.conf文件输入: 2.1:查找<IfModule alias_module> 2.2: 在 ScriptAlias ...
- OAuth授权 | 把这一篇丢给他
OAuth授权 一.背景 上一篇我们介绍了单点登录(SSO),它能够实现多个系统的统一认证.今天我们来谈一谈近几年来非常流行的,大名鼎鼎的OAuth.它也能完成 统一认证,而且还能做更多的事情.至于O ...
- Spring boot 源码分析(一)SpringApplication.run(上)
SpringApplication.run(Main.class, args); 从这个方法开始讲吧: public static ConfigurableApplicationContext run ...
- Ubuntu16.04安装Redis
前言 Redis是常用基于内存的Key-Value数据库,比Memcache更先进,支持多种数据结构,高效,快速.用Redis可以很轻松解决高并发的数据访问问题:作为实时监控信号处理也非常不错. 环境 ...
- sys模块
#python run_case.py #在terminal中执行.py文件 在terminal中执行.py文件: 注: 无法使用terminal来打开那个文件 会显示如下:python: can't ...
- 第十八节 JS中的正则表达式
复习字符串操作: search 查找 substring 获取子字符串 charAt 获取某个字符 split 分割字符串,获取数组 var str = 'abcdefghijk'; alert(st ...
- ES6 - Map
含义和基本用法 Map对象就是简单的键值对映射.其中的键和值可以使任意值.(ps : 对象的键只能是字符串 ) JavaScript 的对象(Object),本质上是键值对的集合(Hash 结构) ...
- Java8增强的包装类
为了解决8中基本数据类型的变量不能当成Object 类型变量使用的问题,Java提供了包装类的概念,为8种基本数据类型分别定义了相应的引用类型,并称为基本数据类型的包装类. JDK 1.5提供了自动装 ...
- sqflite插件简单使用 key======================
https://blog.csdn.net/weixin_34183910/article/details/86029912 https://blog.csdn.net/u013255127/arti ...