http://acm.hdu.edu.cn/showproblem.php?pid=3065

刘汝佳的模板真的非常好用,这道题直接过

学到:

cnt数组记录单词出现次数

以及map存储单词编号与字符串,便于处理相关信息

上代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <map>
using namespace std; const int SIGMA_SIZE=128;
const int MAXNODE = 1005*55;
const int SSIZE = 2000000+100; struct AC
{
int f[MAXNODE];
int ch[MAXNODE][SIGMA_SIZE];
int cnt[MAXNODE];
int val[MAXNODE];
int last[MAXNODE];
int sz;
void init()
{
memset(cnt,0,sizeof(cnt));
memset(ch[0],0,sizeof(ch[0]));
sz=1;
f[0]=last[0]=val[0]=0;
}
inline void clear(){memset(cnt,0,sizeof(cnt));}
void insert(string s,int v)
{
int u=0,n=s.size();
for(int i=0;i<n;i++)
{
int c=s[i];
if(!ch[u][c])
{
val[sz]=0;
memset(ch[sz],0,sizeof(ch[sz]));
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=v;
}
void print(int j)
{
if(j)
{
cnt[val[j]]++;
print(last[j]);
}
}
void getfail()
{
queue<int>q;
for(int c=0;c<SIGMA_SIZE;c++)
{
int u=ch[0][c];
if(u){f[u]=0;q.push(u);last[u]=0;}
}
while(!q.empty())
{
int r=q.front();q.pop();
for(int c=0;c<SIGMA_SIZE;c++)
{
int u=ch[r][c];
if(!u)continue;
q.push(u);
int v=f[r];
while(v && !ch[v][c])v=f[v];
f[u]=ch[v][c];
last[u]=val[f[u]]?f[u]:last[f[u]];
}
}
}
void find(char *T)
{
int n=strlen(T);
int j=0;
for(int i=0;i<n;i++)
{
int c = T[i];
while(j && !ch[j][c])j=f[j];
j=ch[j][c];
if(val[j])print(j);
else
if(last[j])print(last[j]);
}
}
}; AC ac;
char str[SSIZE];
int main()
{
//freopen("hdu3056.txt","r",stdin);
int n;
string word;
while(~scanf("%d",&n))
{
ac.init();
map<int, string> ms;
for(int i=1;i<=n;i++)
{
cin >> word;
ms[i]=word;
ac.insert(word,i);
}
ac.getfail();
scanf("%s",str);
ac.find(str);
for(int i=1;i<=n;i++)
{
////////////////
//printf("%d %d\n",i,ac.cnt[i]);
///////////////////
if(ac.cnt[i])
{
cout << ms[i] << ": " << ac.cnt[i] << endl;
}
}
}
return 0;
}

hdu 3056 病毒侵袭持续中 AC自己主动机的更多相关文章

  1. HDU 3065 病毒侵袭持续中 (AC自动机)

    题目链接 Problem Description 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的"万恶之源".这是一个庞大的病毒 ...

  2. HDU 3065 病毒侵袭持续中

    HDU 3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 3065 病毒侵袭持续中(AC自己主动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3065 Problem Description 小t非常感谢大家帮忙攻克了他的上一个问题.然而病毒侵袭 ...

  4. hdu 3065 病毒侵袭持续中【AC自动机】

    <题目链接> 题目大意: 小t非常感谢大家帮忙解决了他的上一个问题.然而病毒侵袭持续中.在小t的不懈努力下,他发现了网路中的“万恶之源”.这是一个庞大的病毒网站,他有着好多好多的病毒,但是 ...

  5. hdu3065 病毒侵袭持续中 AC自动机入门题 N(N <= 1000)个长度不大于50的模式串(保证所有的模式串都不相同), 一个长度不大于2000000的待匹配串,求模式串在待匹配串中的出现次数。

    /** 题目:hdu3065 病毒侵袭持续中 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:N(N <= 1000)个长度不大于50的 ...

  6. hdu----(3065)病毒侵袭持续中(AC自动机)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. hdu 3065病毒侵袭持续中

    病毒侵袭持续中 http://acm.hdu.edu.cn/showproblem.php?pid=3065 Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  8. HDU3065 病毒侵袭持续中 —— AC自动机

    题目链接:https://vjudge.net/problem/HDU-3065 病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

  9. HDU 3065 病毒侵袭持续中 (模板题)

    病毒侵袭持续中 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. [React Testing] JSX error diffs -- expect-jsx library

    When writing React component tests, it can be hard to decipher the error diffs of broken tests, sinc ...

  2. Linux - Eclipse CDT + GCC 安装(2014.10.2)

    Eclipse CDT + GCC 安装 (2014.10.2) 本文地址:http://blog.csdn.net/caroline_wendy 1. 安装Eclipse,在官方站点下载Eclips ...

  3. javascriipt类型转换

  4. 读取xml文件信息

    static void Main(string[] args) { XmlTextReader reader = new XmlTextReader(@"D:\zy\visual studi ...

  5. css变形几大属性

    1.transform: transform-function() * | none; transform-function: translate().scale().rotate().skew(). ...

  6. 活动指示器UIActivityIndicatorView

    活动指示器UIActivityIndicatorView可以告知用户有一个操作正在进行中 1.创建    UIActivityIndicatorView *activityIndicatorView ...

  7. 最新版 CocoaPods 的安装流程(包括EI Capitan版)

    1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sources -a https://ruby.taoba ...

  8. Enumeration

    Interface Enumeration<E> hasMoreElements() boolean hasMoreElements()    仅当此枚举对象包含至少一个以上元素为真:否则 ...

  9. ICP编程软件配置(烧写KEIL编译后的bin文件)

    1. 安装NuMicro_ICP_Programming_Tool_1.29.6425软件 2. 打开软件后选择目标芯片后点击“连接”按钮 3. 添加程序 4. 点击设定按钮进行设定 5. 点击开始 ...

  10. Width vs Pitch

    1.单位不同,width是像素,pitch是字节.因此一个640*480的8位图和640*480的32位 图他们width一样而pitch不一样. 2.pitch可能大于width个像素所占字节数.w ...