[AC自动机]HDOJ3695 Computer Virus on Planet Pandora
题意:给t、n,t个案例,n个字符串
下面给n+1个字符串,n个互不相同的小串,最后一个是模式串
模式串会出现[qx]的形式,q为数字,x为一个字母
问n个小串在模式串中出现的个数,正着出现、反着出现都算。
蛮裸的ac自动机,本来想着在query里面把找到过的end清零,把模式串展开正着反着找两遍,那即使再找到加零也不影响。但是超时了。。。
于是把找到过的end标为-1,-1了就不再找下去,就可以了~上代码
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <climits>
- #include <cctype>
- #include <cmath>
- #include <string>
- #include <sstream>
- #include <iostream>
- #include <algorithm>
- #include <iomanip>
- using namespace std;
- #include <queue>
- #include <stack>
- #include <vector>
- #include <deque>
- #include <set>
- #include <map>
- typedef long long LL;
- typedef long double LD;
- const double eps=1e-;
- #define pi acos(-1.0)
- #define lson l, m, rt<<1
- #define rson m+1, r, rt<<1|1
- typedef pair<int, int> PI;
- typedef pair<int, PI> PP;
- #ifdef _WIN32
- #define LLD "%I64d"
- #else
- #define LLD "%lld"
- #endif
- //#pragma comment(linker, "/STACK:1024000000,1024000000")
- //LL quick(LL a, LL b){LL ans=1;while(b){if(b & 1)ans*=a;a=a*a;b>>=1;}return ans;}
- //inline int read(){char ch=' ';int ans=0;while(ch<'0' || ch>'9')ch=getchar();while(ch<='9' && ch>='0'){ans=ans*10+ch-'0';ch=getchar();}return ans;}
- //inline void print(LL x){printf(LLD, x);puts("");}
- //inline void read(double &x){char c = getchar();while(c < '0') c = getchar();x = c - '0'; c = getchar();while(c >= '0'){x = x * 10 + (c - '0'); c = getchar();}}
- //inline void sc(LL &x){scanf(LLD, &x);}
- struct Trie
- {
- int next[][], fail[], end[];
- int root, L;
- int newnode()
- {
- for(int i=;i<;i++)
- next[L][i]=-;
- end[L++]=;
- return L-;
- }
- void init()
- {
- L=;
- root=newnode();
- }
- void insert(char buf[])
- {
- int len=strlen(buf);
- int now=root;
- for(int i=;i<len;i++)
- {
- if(next[now][buf[i]-'A']==-)
- next[now][buf[i]-'A']=newnode();
- now=next[now][buf[i]-'A'];
- }
- end[now]++;
- }
- 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 query(char buf[])
- {
- int len=strlen(buf);
- int now=root;
- int res=;
- for(int i=;i<len;i++)
- {
- now=next[now][buf[i]-'A'];
- int tmp=now;
- while(tmp!=root && end[tmp]!=-)
- {
- res+=end[tmp]; //printf("%d %d\n", tmp, end[tmp]);
- end[tmp]=-;
- tmp=fail[tmp];
- }
- }
- return res;
- }
- }ac;
- char buf[], tmp[];
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- int T;
- scanf("%d", &T);
- while(T--)
- {
- int n;
- scanf("%d", &n);
- ac.init();
- for(int i=;i<n;i++)
- {
- scanf("%s", buf);
- ac.insert(buf);
- // int LEN=strlen(buf);
- // reverse(buf, buf+LEN);
- // ac.insert(buf);
- }
- ac.build();
- scanf("%s", tmp);
- int LEN=strlen(tmp), d=;
- for(int i=;i<LEN;i++)
- if(tmp[i]=='[')
- {
- int cur=i+, num=;
- while(isdigit(tmp[cur]))
- num=num*+tmp[cur++]-'';
- fill(buf+d, buf+d+num, tmp[cur]);
- i=cur+, d+=num;
- }
- else
- buf[d++]=tmp[i];
- buf[d]='\0';
- // puts(buf);
- // for(int i=0;i<ac.L;i++)
- // printf("%d %d\n", i, ac.end[i]);
- int ans=ac.query(buf);
- reverse(buf, buf+d);
- printf("%d\n", ans+ac.query(buf));
- }
- return ;
- }
HDOJ 3695
[AC自动机]HDOJ3695 Computer Virus on Planet Pandora的更多相关文章
- hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机 难度:1
F - Computer Virus on Planet Pandora Time Limit:2000MS Memory Limit:128000KB 64bit IO Format ...
- HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)
题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后 ...
- HDU-3695 Computer Virus on Planet Pandora
HDU-3695 Computer Virus on Planet Pandora 题意:电脑中病毒了, 现在n钟病毒指令, 然后有一个电脑指令, 看一下这个电脑指令中了几个病毒, 如果电脑种了某一个 ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
- HDU 3695 / POJ 3987 Computer Virus on Planet Pandora(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora
Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...
随机推荐
- oracle拼接字段用||
① //dual相当于一个临时表.用来测量@@@@H210000000003I4R 的长度用length() select length('@@@@H210000000003I4R') from du ...
- 自己动手写一个简单的(IIS)小型服务器
因为第一次在博客园发表随笔,不太会用,这个笔记是我之前在印象笔记中写好的,然后直接copy过来,有兴趣自己做一个IIS服务器的小伙伴们可以参照下面的流程做一次,也可以叫我要源代码,不过要做完,我觉得花 ...
- linux命令 common 文件比较
比较已经排序的文件 comm [options] file1 file2 comm将逐行比较已经排序的两个文件.显示结果包括3列: 第1列为只在file1中找到的行;第2列为只在file2中找到的行; ...
- C# 编写短信发送Window服务
我们做项目过程中,一般都会有发送短信的需求.最常见的就是户注册或者登录时发送短信验证码.不同类型的短信发送,我们都可以放到到一张短信表中,然后通过一个定时的作业去执行短信发送.而定时作业的执行,我们就 ...
- 解决某些手机RadioGroup中的RadioButton不居中的问题
问题:RadioButton中使用android:gravity="center"使其图片文字居中,在我的华为荣耀7手机上居中显示了,但在HUAWEI G606-T00却显示在右侧 ...
- <转载>国外程序员推荐的免费编程书籍资源
一.George Stocker 提供了一大串,分类如下: How to Design Programs: An Introduction to Computing and Programming 2 ...
- c++Builder 下的文件及目录操作
转自 http://blog.csdn.net/ktcserver/article/details/936329 一.判断目录是否存在: C++ Builder中提供了检查文件 ...
- Oracle PL/SQL 多重选择句
Oracle中语句块的基本格式: declare --变量定义,初始化赋值. begin --变量的赋值,函数调用,if,while等. end: Oracle中的语句:关系运算符:= <> ...
- 访问图像中的像素[OpenCV 笔记16]
再更一发好久没更过的OpenCV,不过其实写到这个部分对计算机视觉算法有所了解的应该可以做到用什么查什么了,所以后面可能会更的慢一点吧,既然开了新坑,还是机器学习更有研究价值吧... 图像在内存中的存 ...
- Poj 1503 Integer Inquiry
1.链接地址: http://poj.org/problem?id=1503 2.题目: Integer Inquiry Time Limit: 1000MS Memory Limit: 1000 ...