18.10.29 POJ 3987 Computer Virus on Planet Pandora(AC自动机+字符串处理)

描述

Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On planet Pandora, hackers make computer virus, so they also have anti-virus software. Of course they learned virus scanning algorithm from the Earth. Every virus has a pattern string which consists of only capital letters. If a virus’s pattern string is a substring of a program, or the pattern string is a substring of the reverse of that program, they can say the program is infected by that virus. Give you a program and a list of virus pattern strings, please write a program to figure out how many viruses the program is infected by.输入There are multiple test cases. The first line in the input is an integer T ( T <= 10) indicating the number of test cases.

For each test case:

The first line is a integer n( 0 < n <= 250) indicating the number of virus pattern strings.

Then n lines follows, each represents a virus pattern string. Every pattern string stands for a virus. It’s guaranteed that those n pattern strings are all different so there are n different viruses. The length of pattern string is no more than 1,000 and a pattern string at least consists of one letter.

The last line of a test case is the program. The program may be described in a compressed format. A compressed program consists of capital letters and “compressors”. A “compressor” is in the following format:

[qx]

q is a number( 0 < q <= 5,000,000)and x is a capital letter. It means q consecutive letter xs in the original uncompressed program. For example, [6K] means ‘KKKKKK’ in the original program. So, if a compressed program is like:

AB[2D]E[7K]G

It actually is ABDDEKKKKKKKG after decompressed to original format.
The length of the program is at least 1 and at most 5,100,000, no matter in the compressed format or after it is decompressed to original format.输出For each test case, print an integer K in a line meaning that the program is infected by K viruses.

样例输入

3
2
AB
DCB
DACB
3
ABC
CDE
GHI
ABCCDEFIHG
4
ABB
ACDEE
BBB
FEEE
A[2B]CD[4E]F

样例输出

0
3
2

提示

In the second case in the sample input, the reverse of the program is ‘GHIFEDCCBA’, and ‘GHI’ is a substring of the reverse, so the program is infected by virus ‘GHI’.

  1 #include <iostream>
  2 #include <string.h>
  3 #include <algorithm>
  4 #include <stack>
  5 #include <string>
  6 #include <math.h>
  7 #include <queue>
  8 #include <stdio.h>
  9 #include <string.h>
 10 #include <vector>
 11 #include <fstream>
 12 #include <set>
 13
 14 using namespace std;
 15 const int maxn = 255;
 16 char line[5100005];
 17 int kase, n, infecsum = 0, nodecou,l;
 18 struct node {
 19     node*next[26];
 20     node*prev;
 21     int count;
 22     node() {
 23         memset(next, 0, sizeof(next));
 24         prev = NULL;
 25         count = 0;
 26     }
 27 }tree[maxn * 1000];
 28
 29 void build() {
 30     for (int i = 0; i < 26; i++)
 31         tree[0].next[i] = tree + 1;
 32     tree[0].prev = NULL;
 33     tree[1].prev = tree;
 34     queue<node*>q;
 35     q.push(tree + 1);
 36     while (!q.empty()) {
 37         node*now = q.front();
 38         q.pop();
 39         for (int i = 0; i < 26; i++) {
 40             node*child = now->next[i];
 41             if (child) {
 42                 node*prev = now->prev;
 43                 while (prev->next[i] == NULL)
 44                     prev = prev->prev;
 45                 child->prev = prev->next[i];
 46                 q.push(child);
 47             }
 48         }
 49     }
 50 }
 51
 52 int search(char *str) {
 53     int ans = 0;
 54     node*first = tree + 1;
 55     for (int i = 0; str[i] != ‘\0‘; i++) {
 56         while (first->next[str[i] - ‘A‘] == NULL&&first!=(tree+1))
 57             first = first->prev;
 58         if (first->next[str[i] - ‘A‘])
 59             first = first->next[str[i] - ‘A‘];
 60         node*tmp = first;
 61         while (tmp != NULL && tmp->count != -1) {
 62             ans += tmp->count;
 63             tmp->count = -1;
 64             tmp = tmp->prev;
 65         }
 66     }
 67     return ans;
 68 }
 69
 70 void insert(node*rt, char* line) {
 71     int l = strlen(line);
 72     for (int i = 0; i < l; i++) {
 73         if (rt->next[line[i] - ‘A‘] == NULL) {
 74             nodecou++;
 75             rt->next[line[i] - ‘A‘] = tree + nodecou;
 76         }
 77         rt = rt->next[line[i] - ‘A‘];
 78     }
 79     rt->count++;
 80 }
 81
 82 void stringin() {
 83     char ch;
 84     scanf("\n");
 85     while (scanf("%c", &ch)) {
 86         if (ch == ‘\n‘)
 87             break;
 88         if (ch == ‘[‘) {
 89             int c;
 90             scanf("%d", &c);
 91             scanf("%c", &ch);
 92             for (int i = 1; i <= c; i++)line[l++] = ch;
 93             scanf("%c", &ch);
 94         }
 95         else
 96             line[l++] = ch;
 97     }
 98     line[l] = ‘\0‘;
 99 }
100
101 void init() {
102     scanf("%d", &kase);
103     while (kase--) {
104         memset(tree, 0, sizeof(tree));
105         infecsum = 0, nodecou = 1,l=0;
106         scanf("%d", &n);
107         for (int i = 1; i <= n; i++) {
108             scanf("%s",line);
109             insert(tree + 1, line);
110         }
111         build();
112         stringin();
113         infecsum=search(line);
114         reverse(line,line+l);
115         infecsum+=search(line);
116         printf("%d\n", infecsum);
117     }
118 }
119
120 int main()
121 {
122     init();
123     return 0;
124 }

这题杀我……!

果然自动AC什么的都是假的QwQ

一开始倔强地使用了string结果无限TLE

后来又因为中间少了一句迭代而WA

die了

再也不想看到这道题

开学来做得最悲伤的一道题没有之一

主要是要考虑子串中套子串的问题如果不加标记会TLE(我看见有人没有考虑AC了??逗我??)

稍微处理一下字符串

原文地址:https://www.cnblogs.com/yalphait/p/9874022.html

时间: 2024-11-05 11:54:28

18.10.29 POJ 3987 Computer Virus on Planet Pandora(AC自动机+字符串处理)的相关文章

hdu 3695 Computer Virus on Planet Pandora(AC自动机)

题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求判断说给定串中包含几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后匹配一次后反转后再匹配一次. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include <algor

hdu 3695 10 福州 现场 F - Computer Virus on Planet Pandora 暴力 ac自动机

F - Computer Virus on Planet Pandora Time Limit:2000MS     Memory Limit:128000KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3695 Appoint description:  System Crawler  (2014-11-05) Description Aliens on planet Pandora also write comp

hdu 3695 Computer Virus on Planet Pandora(AC自己主动机)

题目连接:hdu 3695 Computer Virus on Planet Pandora 题目大意:给定一些病毒串,要求推断说给定串中包括几个病毒串,包括反转. 解题思路:将给定的字符串展开,然后匹配一次后反转后再匹配一次. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include <algor

hdu 3695:Computer Virus on Planet Pandora(AC自动机,入门题)

Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others)Total Submission(s): 2578    Accepted Submission(s): 713 Problem Description Aliens on planet Pandora also write computer programs l

HDU 3695 Computer Virus on Planet Pandora

Computer Virus on Planet Pandora Problem Description Aliens on planet Pandora also write computer programs like us. Their programs only consist of capital letters (‘A’ to ‘Z’) which they learned from the Earth. On planet Pandora, hackers make compute

HDOJ 题目3695 Computer Virus on Planet Pandora(AC自动机)

Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/128000 K (Java/Others) Total Submission(s): 3169    Accepted Submission(s): 867 Problem Description Aliens on planet Pandora also write computer programs

HDU 3695 Computer Virus on Planet Pandora (AC自动机)

题意:有n种病毒序列(字符串),一个模式串,问这个字符串包含几种病毒. 包含相反的病毒也算,字符串中[qx]表示有q个x字符.详细见案列. 0 < q <= 5,000,000尽然不会超,无解 3 2 AB DCB DACB 3 ABC CDE GHI ABCCDEFIHG 4 ABB ACDEE BBB FEEE A[2B]CD[4E]F Sample Output 0 3 2 Hint In the second case in the sample input, the reverse

[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>

18.10.29 POJ 3691 DNA repair(AC自动机+dp)

描述 Biologists finally invent techniques of repairing DNA that contains segments causing kinds of inherited diseases. For the sake of simplicity, a DNA is represented as a string containing characters 'A', 'G' , 'C' and 'T'. The repairing techniques a