light oj 1427(ac自动机)
2024-08-31 04:14:52
#include <bits/stdc++.h> using namespace std; const int N = *;
const int M = ; map<string,int>Map;
struct Trie
{
int next[N][M],fail[N],end[N];
int root,L;
int newnode()
{
for(int i = ; i < ; i++)
next[L][i] = -;
end[L++] = -;
return L - ;
}
void init()
{
L = ;
root = newnode();
}
void insert(string s,int id)
{
int len = s.size();
int now = root;
for(int i = ; i < len; i++)
{
if(next[now][s[i]-'a'] == -)
next[now][s[i] - 'a'] = newnode();
now = next[now][s[i] - 'a'];
}
end[now] = id;
}
void build()
{
queue<int>Q;
fail[root] = root;
for(int i = ; i < ; i++)
{
if(next[root][i] == -)
next[root][i] = root;
else
{
fail[next[root][i]] = root;
Q.push(next[root][i]);
}
}
while(!Q.empty())
{
int now = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
if(next[now][i] == -)
next[now][i] = next[fail[now]][i];
else
{
fail[next[now][i]] = next[fail[now]][i];
Q.push(next[now][i]);
}
}
}
}
int num[];
void query(char buf[],int n,int mm[])
{
for(int i = ; i <= n; i++)
num[i] = ;
int len = (int)strlen(buf);
int now = root;
for(int i = ; i < len; i++)
{
now = next[now][buf[i]-'a'];
int temp = now;
while( temp != root )
{
if( end[temp] != -)
num[end[temp]]++;
temp = fail[temp];
}
}
for(int i = ; i <= n; i++)
printf("%d\n",num[mm[i]]);
}
}; char buf[];
Trie ac; void solve()
{
ac.init();
int n, l = ;
scanf("%d",&n);
scanf("%s",buf);
Map.clear();
int twice[];
memset(twice,,sizeof(twice));
for(int i = ; i <= n; i++)
{
string ss;
cin>>ss;
int c = Map[ss];
if(c == )
{
Map[ss] = ++l;
ac.insert(ss,l);
}
twice[i] = Map[ss];
//int tmp = Map[ss]; }
ac.build(); ac.query(buf,n,twice);
} int main(void)
{
int t,cnt = ;
scanf("%d",&t);
while(t--)
{
printf("Case %d:\n",++cnt);
solve();
} return ;
}
最新文章
- Xamarin体验:使用C#开发iOS/Android应用
- C#定时任务组件之FluentScheduler
- 有shi以来最详细的正则表达式入门教程
- Linux shell程序一
- curl毫秒超时使用的坑
- Java中的Set, List, Map漫谈
- Quirks模式是什么?
- Git@OSC &; SSH配置
- 转:sql SELECT时的with(nolock)选项说明
- HDU 4006 The kth great number 优先队列、平衡树模板题(SBT)
- FrameBuffer系列 之 介绍
- .NET接入接口/请求服务器
- 将python2代码转为python3
- (摘)使用 .NET Core 实现依赖关系注入
- [转]linux下编译boost.python
- .Net MVC TextBoxFor 扩展 placeholder 与 class 属性
- 关于MySQL中添加数据的两种方法
- JMeter学习(三十四)使用jmeter来发送json/gzip格式数据(转载)
- openstack(Pike 版)集群部署(五)--- Neutron 部署
- Selenium WebDriver VS Selenium RC
热门文章
- reg命令详解
- LeetCode第一题—— Two Sum(寻找两数,要求和为target)
- 使用Cookie实现用户商品历史浏览记录
- duilib教程之duilib入门简明教程3.第一个程序 Hello World
- 【BZOJ 1257】[CQOI2007]余数之和sum
- python中的多线程编程与暂停、播放音频的结合
- Java笔记 - 线程与并行API
- express 4 使用session和cookies
- mysql80版本—yum安装—图文全过程
- 启动zuul时候报错:The bean &#39;proxyRequestHelper&#39;, defined in class path resource [org/springframework/cloud/netflix/zuul