51nod1647 小Z的trie
题意:给你n个字符串,m次查询,每次问你第p个字符串的s到t的字符串在n个字符串建成的字典树上出现了多少次
题解:先建出字典树,在字典树上拓展sam,记录每个子串的出现次数.查询时只需找出在字典树上的t在sam中的位置,每次往fa跳(即后缀相同,长度减小)找到第一个长度比查询串的小于等于的位置就是答案.往fa上跳的过程,能用倍增优化.(预处理倍增数组写错,wa了很久....)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}
using namespace std;
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+10,maxn=1000000+10,inf=0x3f3f3f3f;
struct SAM{
int last,cnt;
int ch[N<<1][26],fa[N<<1],l[N<<1],sz[N<<1];
int a[N<<1],c[N<<1],father[N<<1][21];
int ins(int y,int x)
{
last=y;
int p=last,np=++cnt;last=np;l[np]=l[p]+1;
for(;p&&!ch[p][x];p=fa[p])ch[p][x]=np;
if(!p)fa[np]=1;
else
{
int q=ch[p][x];
if(l[q]==l[p]+1)fa[np]=q;
else
{
int nq=++cnt;l[nq]=l[p]+1;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][x]==q;p=fa[p])ch[p][x]=nq;
}
}
sz[np]=1;
return last;
}
void build()
{
cnt=last=1;
}
void topo()
{
for(int i=1;i<=cnt;i++)c[l[i]]++;
for(int i=1;i<=cnt;i++)c[i]+=c[i-1];
for(int i=1;i<=cnt;i++)a[c[l[i]]--]=i;
}
void gao()
{
topo();
for(int i=cnt;i>=1;i--)
{
int p=a[i];
sz[fa[p]]+=sz[p];
father[i][0]=fa[i];
}
for(int i=1;i<=20;i++)for(int j=1;j<=cnt;j++)
father[j][i]=father[father[j][i-1]][i-1];
}
}sam;
char s[N];
vector<int>v[100010];
struct Tire{
int tot,ch[N][26],id[N],root;
int newnode()
{
memset(ch[tot],0,sizeof ch[tot]);
return ++tot;
}
Tire()
{
tot=0;
root=newnode();
}
void ins(int id)
{
int now=root,len=strlen(s+1);
v[id].resize(len+2);
for(int i=1;i<=len;i++)
{
if(!ch[now][s[i]-'a'])ch[now][s[i]-'a']=newnode();
now=ch[now][s[i]-'a'];
v[id][i]=now;
}
}
void bfs()
{
queue<int>q;q.push(root);id[root]=1;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=0;i<26;i++)if(ch[u][i])
{
id[ch[u][i]]=sam.ins(id[u],i);
q.push(ch[u][i]);
}
}
}
}t;
int main()
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",s+1);
t.ins(i);
}
sam.build();
t.bfs();
sam.gao();
int m;scanf("%d",&m);
while(m--)
{
int p,x,y;scanf("%d%d%d",&p,&x,&y);
p=t.id[v[p][y]];x=y-x+1;
for(int i=20;i>=0;i--)if(sam.l[sam.father[p][i]]>=x)p=sam.father[p][i];
printf("%d\n",sam.sz[p]);
}
return 0;
}
/********************
********************/
51nod1647 小Z的trie的更多相关文章
- 51nod 小Z的trie(Trie+广义SAM)
[题目链接] http://www.51nod.com/contest/problem.html#!problemId=1647 [题意] 给定一个n个字符串的Trie,每次询问一个字符串在Trie上 ...
- 【20170920校内模拟赛】小Z爱学习
所有题目开启-O2优化,开大栈空间,评测机效率为4亿左右. T1 小 Z 学数学(math) Description 要说小 Z 最不擅长的学科,那一定就是数学了.这不,他最近正在学习加法运算.老 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose)
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7676 Solved: 3509[Subm ...
- BZOJ2038: [2009国家集训队]小Z的袜子(hose)
Time Limit: 20 Sec Memory Limit: 259 MB Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天, ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
- 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...
- BZOJ 2038 小z的袜子 & 莫队算法(不就是个暴力么..)
题意: 给一段序列,询问一个区间,求出区间中.....woc! 贴原题! 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过 ...
- 【BZOJ2038】【2009国家集训队】小Z的袜子(hose) 分块+莫队
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...
随机推荐
- P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
吐槽 数据太水了吧,我AC自动机的trie建错了结果只是RE了两个点,还以为数组开小了改了好久 思路 看到多模板串,字符串匹配,且模板串总长度不长,就想到AC自动机 然后用栈维护当前的字符串位置,如果 ...
- JZ2440之GPIO篇
买来开发板已经有一段时间了,刚接触时兴奋至极,后来跟着视频看下去发现似乎自己并没有学到太多东西,于是发现自己可能欠缺的太多以致从课程中无法提取出重要的东西来,所以并没有得到太多的营养成分.因此我个人认 ...
- Nginx配置示例
server {listen 6080;server_name local.boheadmin; location / {proxy_pass http://127.0.0.1:8087;} loca ...
- 【Entity framework】Code First Approach
开篇之前感谢 china_fucan的文章给我的帮助,下面的评论也解决了很多问题同样给予感谢. code first 项目中的ORM框架如果采用的是EF,那么可能会采用code first的方式去使用 ...
- 2、iptables基本应用
iptables:规则管理工具 添加.修改.删除.显示等: 规则和链有计数器: pkts: 由规则或链所匹配到的报文的个数: bytes:由规则或链匹配到的所有报文大小之和: iptables命令: ...
- Spring框架学习
没有状态变化的对象(无状态对象):应当做成单例. Spring-framework的下载:http://repo.spring.io/release/org/springframework/sprin ...
- C++类模板和模板类
C++ 中有一个重要特性,那就是模板类型.类似于Objective-C中的泛型.C++通过类模板来实现泛型支持. 1 基础的类模板 类模板,可以定义相同的操作,拥有不同数据类型的成员属性. 通常使用t ...
- C#调用cmd命令
using System.Diagnostics; public class CmdHelper { private static string CmdPath = @"C:\Windows ...
- List<Model>转String 、String 转List<string>
var ltCode = from item in psw.VehicleInsuranceItem select item.Code; string code = string.Join(" ...
- ES6中Promise的入门(结合例子)
一.Promise的前言 解决回调地狱 //以往回调方式 函数1(function(){ //代码执行...(ajax1) 函数2(function(){ //代码执行...(ajax2) 函数3(f ...