UVALive 5103 Computer Virus on Planet Pandora Description 一些新兴需求模式的字符串 AC自己主动机
主题链接: option=com_onlinejudge&Itemid=8&page=show_problem&problem=3104">点击打开链接
题意:
case数
n个模式串
一个母串。
问:n个模式串出现的种数(一个模式串多次出现仅仅算一次)
对于 "ABC" , 若母串出现了"CBA"这种反串。也算出现了。
所以:
1
ABC
CBA
ans = 1
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
const int maxnode = 250*1000+10000;
const int sigma_size = 26; struct Trie{
int ch[maxnode][sigma_size];
int val[maxnode]; //该单词在模式串中出现的次数
int last[maxnode];
int f[maxnode]; //失配数组
int num[maxnode]; //该单词出如今文本串的次数
int pre[maxnode]; //该单词的前驱
int len[maxnode]; //以该单词结尾的单词长度
int Char[maxnode]; //该单词相应的字母
int road[maxnode]; //路径压缩优化 针对计算模式串出现的种数
int sz;
int Newnode()
{
val[sz] = f[sz] = last[sz] = len[sz] = num[sz] = 0;
memset(ch[sz], 0, sizeof ch[sz]);
return sz++;
}
void init(){
sz=0;
Newnode();
}
int idx(char c){ return c-'A'; }
int insert(char *s){
int u = 0;
for(int i = 0, c; s[i] ;i++){
c = idx(s[i]);
if(!ch[u][c])
ch[u][c] = Newnode();
pre[ch[u][c]] = u;
Char[ch[u][c]] = s[i];
len[ch[u][c]] = len[u]+1;
road[ch[u][c]] = 1;
u = ch[u][c];
}
val[u] = 1;
num[u] = 0;
return u;
}
void getFail(){
queue<int> q;
for(int i = 0; i<sigma_size; i++)
if(ch[0][i]) q.push(ch[0][i]);
int r, c, u, v;
while(!q.empty()){
r = q.front(); q.pop();
for(c = 0; c<sigma_size; c++){
u = ch[r][c];
if(!u)continue;
q.push(u);
v = f[r];
while(v && ch[v][c] == 0) v = f[v]; //沿失配边走上去 假设失配后有节点 且 其子节点c存在则结束循环
f[u] = ch[v][c];
}
}
}
void find(char *T){
//计算模式串出现的个数:(每种多次出现算多次)
int j = 0;
for(int i = 0, c, temp; T[i] ; i++){
c = idx(T[i]);
while(j && ch[j][c]==0) j = f[j];
j = ch[j][c]; temp = j;
while(temp){
num[temp]++;
temp = f[temp];
}
}
}
void find_kind(char *T, int &ans){
//计算种数, 反复出现的不再计算(若多个询问则要在此处加for(i=0->sz)lu[i]=1;
int j = 0, i, c, temp;
for(i = 0; T[i]; i++){
c = idx(T[i]);
while(j && ch[j][c] == 0) j = f[j];
j = ch[j][c];
temp = j;
while(temp && road[temp]){
if(val[temp])
{
++ans;
val[temp] = 0;
}
road[temp] = 0;
temp = f[temp];
}
}
}
}ac;
char s[1015], a[5100010], b[5100010], c;
int n, num;
int main() {
int T; scanf("%d", &T);
while (T-->0) {
ac.init();
scanf("%d", &n);
while(n--){
scanf("%s", s);
ac.insert(s);
}
ac.getFail();
int top = 0;
scanf("%s", a);
int i = 0;
while(a[i]){
if(a[i]!='[')
b[top++] = a[i];
else {
num = 0;
i++;
while( '0' <= a[i] && a[i] <= '9')
num = num*10 + a[i]-'0', i++;
c = a[i];
i++;
while(num-- > 0){
b[top++] = c;
}
}
i++;
}
a[top] = b[top] = 0;
for(int i = top-1; i >= 0; i--)a[i] = b[top-i-1];
int ans = 0;
ac.find_kind(b, ans);
ac.find_kind(a, ans);
printf("%d\n", ans);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
UVALive 5103 Computer Virus on Planet Pandora Description 一些新兴需求模式的字符串 AC自己主动机的更多相关文章
- 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自动机)
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 / POJ 3987 Computer Virus on Planet Pandora
Computer Virus on Planet Pandora Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1353 ...
- 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(AC自动机)(2010 Asia Fuzhou Regional Contest)
Description Aliens on planet Pandora also write computer programs like us. Their programs only consi ...
- HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)
题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...
随机推荐
- Invalid embedded descriptor for ".proto".Dependencies passed (Protobufer)解决办法
前言 之前开发的时候,发现居然出现了Dependencies passed to FileDescriptor.buildFrom() don't match those listed in the ...
- 【剑指offer】约瑟夫环问题
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27957407 题目描写叙述: 每年六一儿童节,JOBDU都会准备一些小礼物去看望孤儿院的小 ...
- ON、WHERE、HAVING的差别
ON .WHERE.HAVING都能通过限制条件筛选数据,但他们的使用及其不同.以下我们来分析三者之间的差别. 1. ON 和WHERE 全部的查询都回产生一个中间暂时报表,查询结果就是从 ...
- php学习之道:php中soap的使用实例以及生成WSDL文件,提供自己主动生成WSDL文件的类库——SoapDiscovery.class.php类
1. web service普及: Webservice soap wsdl差别之个人见解 Web Service实现业务诉求: Web Service是真正"办事"的那个,提供 ...
- HTML学习笔记——各种居中对齐
0.前言 水平居中基本方法--指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中. 样式例如以下: 1:margin:0px auto 2:margi ...
- shell 脚本之if、for、while语句
(1)if语句 root@ubuntu:/mnt/shared/shellbox/shellif# cat shellif.sh #!/bin/bash #推断字符串 if [ "$1&qu ...
- [Oracle] - 性能优化工具(4) - AWRDD
AWRDD是用于比較两个AWR快照,从而获得不同一时候期的性能. 运行例如以下语句获得AWRDD: @?/rdbms/admin/awrddrpt.sql 2025 23 2月 2014 07:12 ...
- pygame系列_游戏窗口显示策略
在这篇blog中,我将给出一个demo演示: 当我们按下键盘的‘f’键的时候,演示的窗口会切换到全屏显示和默认显示两种显示模式 并且在后台我们可以看到相关的信息输出: 上面给出了一个简单的例子,当然在 ...
- Linux系统部署规范v1.0
Linux系统部署规范v1.0 目的: 1.尽可能减少线上操作: 2.尽可能实现自动化部署: 3.尽可能减少安装服务和启动的服务: 4.尽可能使用安全协议提供服务: 5.尽可能让业务系统单一: 6.尽 ...
- POJ 3630 Phone List Trie题解
Trie的应用题目. 本题有两个难点了: 1 动态建立Trie会超时,须要静态建立数组,然后构造树 2 推断的时候注意两种情况: 1) Tire树有133,然后插入13333556的时候.2)插入顺序 ...