题目链接:https://cn.vjudge.net/problem/HDU-3065

题意

跟上一道题是几乎一模一样,这次是统计关键词的出现次数

一个相当坑的地方,注意多组样例

思路

套模版

改insert方法,这次我们指定每个关键词的idx

改query方法,统计value_counter(话说最近几天在用pandas,value_counts确实方便)

提交过程

WA*n 注意多组样例
WA 注意用128大小的分支,用26个同时判断待匹配串的话会WA?
AC

代码

#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn=1000+20, maxw=50+20, maxl=2000000+20;
const int ACSize=maxn*maxw, maxitem=128;
char word[maxw], line[maxl];
map<int, string> tostr;
struct ACauto{
int next[ACSize][maxitem], fail[ACSize], cnt[ACSize];
int root, total;
int newnode(void){
for(int pos=0; pos<maxitem; pos++)
next[total][pos]=-1;
cnt[total]=0;
return total++;
}
void init(void){
total=0;
root=newnode();
}
int getPos(char ch){
return ch;
} void insert(char buf[], int idx){
int now=root;
for(int i=0; buf[i]; i++){
int pos=getPos(buf[i]);
if(next[now][pos]==-1)
next[now][pos]=newnode();
now=next[now][pos];
}
cnt[now]=idx; //++itemCounter;
} void build(void){
queue<int> que;
fail[root]=root;
for(int i=0; i<maxitem; i++)
if(next[root][i]==-1)
next[root][i]=root;
else{
fail[next[root][i]]=root;
que.push(next[root][i]);
} while(!que.empty()){
int now=que.front(); que.pop(); for(int pos=0; pos<maxitem; pos++)
if(next[now][pos]==-1)
next[now][pos]=next[fail[now]][pos];
else{
fail[next[now][pos]]=next[fail[now]][pos];
que.push(next[now][pos]);
}
}
} void query(char buf[], int counter[]){
int now=root;
for(int i=0; buf[i]; i++){
int pos=getPos(buf[i]);
// if (pos<0 || pos>=maxitem){pos=root; continue;}
now=next[now][pos];
for (int tmp=now; tmp!=root; tmp=fail[tmp]) if (cnt[tmp])
counter[cnt[tmp]]++;
}
}
}AC; int main(void){
int m, n; while (scanf("%d", &n)==1 &&n){
AC.init();
for (int i=1; i<=n; i++){
scanf("%s", word);
AC.insert(word, i);
tostr[i]=string(word);
}AC.build(); int counter[maxn]={0};
scanf("%s", line);
AC.query(line, counter); for (int i=1; i<=n; i++) if (counter[i])
printf("%s: %d\n", tostr[i].c_str(), counter[i]);
} return 0;
}
Time Memory Length Lang Submitted
234ms 18004kB 2417 C++ 2018-08-02 17:39:24

HDU-3065 病毒侵袭持续中 字符串问题 AC自动机的更多相关文章

  1. HDU 3065 病毒侵袭持续中

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

  2. hdu 3065病毒侵袭持续中

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

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

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

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

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

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

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

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

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

  7. hdu 3065病毒侵袭持续中(ac自动机)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3065 中文题题意不解释了. 依旧稍微改一下ac自动机模版就能过了.还有一个坑点!是多组数据!!! #i ...

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

    这题数据太水,一开始没有加上Get的方法也能AC..话说AC自动机中一定要注意加上Get的方法!(不然,同一个后缀的其他单词就没被算上了.) 代码如下: #include <stdio.h> ...

  9. HDU 3065 病毒侵袭持续中(AC自动机(每个模式串出现次数))

    http://acm.hdu.edu.cn/showproblem.php?pid=3065 题意:求每个模式串出现的次数. 思路: 不难,把模板修改一下即可. #include<iostrea ...

随机推荐

  1. 记一次redis-cluster的切换

    # redis-cli -h 10.5.8.18 -c -p 8001 cluster nodes|grep master 6d2f817064a10631648f24f450a37237b3d53f ...

  2. fullcalendar日历插件

    https://www.helloweba.net/javascript/231.html

  3. 进程线程之pid,tid

    Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型pthread_t,由pthread_self()取得,该id由线程维护,其i ...

  4. js获取路径参数对象

    /** * 获取页面路径参数值 */ function getParams(key) { var result = {}; var paramStr = encodeURI(window.docume ...

  5. HDU 1348 Wall ( 凸包周长 )

    链接:传送门 题意:给出二维坐标轴上 n 个点,这 n 个点构成了一个城堡,国王想建一堵墙,城墙与城堡之间的距离总不小于一个数 L ,求城墙的最小长度,答案四舍五入 思路:城墙与城堡直线长度是相等的, ...

  6. [读书笔记] R语言实战 (十三) 广义线性模型

    广义线性模型扩展了线性模型的框架,它包含了非正态的因变量分析 广义线性模型拟合形式: $$g(\mu_\lambda) = \beta_0 + \sum_{j=1}^m\beta_jX_j$$ $g( ...

  7. centos7最小化安装Oracle11gR2

    1.准备CentOS 7 系统环境 我以 CentOS-7-x86_64-DVD-1511.iso 为例,简述Oracle 11g的安装过程. 由于是使用静默模式(silent)安装的,无需使用图形化 ...

  8. Map的四种遍历方法

    1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...

  9. 如何成为资深的python专家

    相信很多人有这种感受,python很简单,几天就学会了:做了一段时间就觉得python没什么好玩的,就这样. 一种语言有火这么久,必有它存在的道理. 第一.我们要相信她,她就像你的新女朋友一样,她会给 ...

  10. mybatis-plus注解版实现多表联查(sql)

    mybatis注解版实现多表联查 需求: 用户有角色,角色有权限,需要一次取用户信息包含角色信息及其对应权限 实体类: package cn.zytao.taosir.common.model.use ...