<题目链接>

经典ACAM。


注意单词之间添加字符,以及对重复单词的处理。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=210,MAXM=1000010,MAXL=1000010;
char str[MAXL],p[MAXN+MAXL];
int n;
class ACAM
{
public:
ACAM(void)
{
cnt=0;
memset(mp,0,sizeof mp);
memset(ans,0,sizeof ans);
memset(s,0,sizeof s);
}
void Insert(char *str,int k)
{
int x=0;
for(int i=0,t;str[i];++i)
{
if(!s[x].c[t=num(str[i])])
s[x].c[t]=++cnt;
x=s[x].c[t];
}
if(!s[x].index)
s[x].index=k;
mp[k]=s[x].index;
}
void GetFail(void)
{
queue<int> q;
for(int i=0,t;i<26;++i)
if(t=s[0].c[i])
q.push(t);
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=0,t,f;i<26;++i)
if(t=s[x].c[i])
{
f=s[t].fail=s[s[x].fail].c[i];
s[t].lst=s[f].index?f:s[f].lst;
q.push(t);
}
else
s[x].c[i]=s[s[x].fail].c[i];
}
}
void Search(char *p)
{
int x=0;
for(int i=0;p[i];++i)
{
if(p[i]==' ')
{
x=0;
continue;
}
if(s[x=s[x].c[num(p[i])]].index)
++ans[s[x].index];
for(int j=s[x].lst;j;j=s[j].lst)
++ans[s[j].index];
}
for(int i=1;i<=n;++i)
printf("%d\n",ans[mp[i]]);
}
private:
int cnt,mp[MAXN],ans[MAXN];
struct node
{
int index,fail,lst,c[26];
}s[MAXM];
int num(char c)
{
return c-'a';
}
}AC;
int main(int argc,char *argv[])
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%s",str);
AC.Insert(str,i);
strcat(p,str);
strcat(p," ");
}
AC.GetFail();
AC.Search(p);
return 0;
}

谢谢阅读。

[Luogu 3966] TJOI 2013 单词的更多相关文章

  1. [TJOI 2013]单词

    Description 题库链接 给出一篇文章的所有单词,询问每个单词出现的次数. 单词总长 \(\leq 10^6\) Solution 算是 \(AC\) 自动机的板子,注意拼成文章的时候要在单词 ...

  2. bzoj-3170 3170: [Tjoi 2013]松鼠聚会(计算几何)

    题目链接: 3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MB Description 有N个小松鼠,它们的家用一个点x,y表 ...

  3. BZOJ3170: [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 531  Solved: 249[Submit][Statu ...

  4. BZOJ 3170: [Tjoi 2013]松鼠聚会 切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  5. BZOJ 3170: [Tjoi 2013]松鼠聚会( sort )

    题目的距离为max(|x1-x2|, |y1-y2|) (切比雪夫距离). 切比雪夫距离(x, y)->曼哈顿距离((x+y)/2, (x-y)/2) (曼哈顿(x, y)->切比雪夫(x ...

  6. [Tjoi 2013]松鼠聚会

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  7. Luogu 1979 NOIP 2013 华容道(搜索,最短路径)

    Luogu 1979 NOIP 2013 华容道(搜索,最短路径) Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面 ...

  8. Bzoj 3170[Tjoi 2013]松鼠聚会 曼哈顿距离与切比雪夫距离

    3170: [Tjoi 2013]松鼠聚会 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1318  Solved: 664[Submit][Stat ...

  9. [BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap)

    [BZOJ 3173] [TJOI 2013] 最长上升子序列(fhq treap) 题面 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数 ...

随机推荐

  1. [mongodb]child process failed, exited with error number 100

    Run the following command first to start the mongo server mongod run --config /usr/local/etc/mongod. ...

  2. rewrite or internal redirection cycle while processing nginx重定向报错

    2018/05/07 15:03:42 [error] 762#0: *3 rewrite or internal redirection cycle while processing "/ ...

  3. $http.get(...).success is not a function 错误解决

    $http.get(...).success is not a function 错误解决 1.6 新版本的AngularJs中用then和catch 代替了success和error,用PRomis ...

  4. html+css基础 - 个人备忘录

    //======================html部分===================// 表现内容<meta http-equiv="Content-Type" ...

  5. JavaScript初探系列之基本概念

    JavaScript的核心语言特性在ECMA-262中是以名为ECMAScript(ECMA, EuropeanComputer Manufacturers Association )的伪语言的形式来 ...

  6. 2019寒假训练营寒假作业(二) MOOC的网络空间安全概论笔记部分

    视频课程--MOOC的网络空间安全概论笔记 第一章 网络空间安全概述 2001年,网络空间概念被首次提出: 网络空间安全框架: 1.设备层安全: 可通过截获电磁辐射获取计算机信息.通过硬件木马(恶意电 ...

  7. 【Redis】- 缓存击穿

    什么是缓存击穿 在谈论缓存击穿之前,我们先来回忆下从缓存中加载数据的逻辑,如下图所示 因此,如果黑客每次故意查询一个在缓存内必然不存在的数据,导致每次请求都要去存储层去查询,这样缓存就失去了意义.如果 ...

  8. 基于实现Controller接口的简单Spring工程

    这个Spring工程的特点是:实现了Controller接口(这样就可以在url中传参数?,待调查) 一下为代码,可运行. 1,web.xml <servlet> <servlet- ...

  9. NetScaler ‘Counters’ Grab-Bag!

    NetScaler ‘Counters’ Grab-Bag! https://www.citrix.com/blogs/author/andrewre/ https://www.citrix.com/ ...

  10. Android <Android应用开发实战> 学习总结杂项

    1.系统相册默认保存地址:android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Ca ...