描述

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 }

View Code

这题杀我……!

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

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

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

die了

再也不想看到这道题

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

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

稍微处理一下字符串

转载于:https://www.cnblogs.com/yalphait/p/9874022.html

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

  1. AC自动机 - 多模式串的匹配 --- HDU 3695 Computer Virus on Planet Pandora

    Problem's Link Mean: 有n个模式串和一篇文章,统计有多少模式串在文章中出现(正反统计两次). analyse: 好久没写AC自动机了,回顾一下AC自动机的知识. 本题在构造文章的时 ...

  2. HDU 3695 Computer Virus on Planet Pandora (AC自己主动机)

    题意:有n种病毒序列(字符串),一个模式串,问这个字符串包括几种病毒. 包括相反的病毒也算.字符串中[qx]表示有q个x字符.具体见案列. 0 < q <= 5,000,000尽然不会超, ...

  3. 计算机病毒及其防治 Computer Virus Analysis and Antivirus

    Episode I 计算机病毒的历史 FUDAN UNIVERSITY 3 世界上第一台计算机诞生 • 1946年2月14日,宾夕法尼亚大学莫尔电气学院 (Moore School of Electr ...

  4. 18.06.27 POJ百练 4124海贼王之伟大航路

    描述 "我是要成为海贼王的男人!",路飞一边喊着这样的口号,一边和他的伙伴们一起踏上了伟大航路的艰险历程. 路飞他们伟大航路行程的起点是罗格镇,终点是拉夫德鲁(那里藏匿着" ...

  5. 【不忘初心】Win10_20H2_2009_19042.572_X64_六合一_[纯净精简版](2020.10.29)

    母版来自MSDN  WIN10_20H2.19042.508,集成补到19041.572,20H2相比1909 2004版本要稳定很多,精简起来也比较顺手,相对来说体积比之前的要小一些,精简方法基本上 ...

  6. 武汉理工大学计算机专业英语,第56讲:Computer Virus

    引言 Part1 Computer Hardware Chapter1 Principles of Computer Organization 1.1 Computer Hardware 1.3 Wh ...

  7. 滴滴KDD2017论文:基于组合优化的出租车分单模型 By 机器之心2017年8月14日 10:29 数据挖掘顶会 KDD 2017 已经开幕,国内有众多来自产业界的论文被 KDD 2017 接收。

    滴滴KDD2017论文:基于组合优化的出租车分单模型 By 机器之心2017年8月14日 10:29 数据挖掘顶会 KDD 2017 已经开幕,国内有众多来自产业界的论文被 KDD 2017 接收.本 ...

  8. anguarjs 上传图片预览_MIUI12 20.10.29更新,新版「模糊预览图」

    又到了本周的最后一个内测更新版本,当然也是10月份的最后一个开发版内测版本了,接下来就是11月份了,感觉这个月又偷偷的溜走了~ 那么本周的最后一个内测版本的更新内容又会是什么呢?我们就直接进入本次的文 ...

  9. Interview:算法岗位面试—10.29下午上海某电子(偏传统ML算法,外企)数据结构算法+晚上国内某保险公司(偏AI算法,世界500强)技术面试之分类算法、回归算法、聚类算法等细节考察

    ML岗位面试:10.29下午上海某电子(偏传统ML算法,外企)数据结构算法+晚上国内某保险公司(偏AI算法,世界500强)技术面试之分类算法.回归算法.聚类算法等细节考察 Interview:算法岗位 ...

  10. ARM的存储器映射与存储器重映射【转载】2009-12-14 10:29最近在用LPC2148,看到了一篇文章,感觉很有帮助,就转了过来。

    ARM的存储器映射与存储器重映射[转载]2009-12-14 10:29最近在用LPC2148,看到了一篇文章,感觉很有帮助,就转了过来. arm处理器本身所产生的地址为虚拟地址,每一个arm芯片内都 ...

最新文章

  1. 内联函数inline
  2. plotly基于dataframe数据绘制股票OHLC图
  3. php errno 28,php7.28 编译出错 一直通不过去
  4. JavaScript 函数 伪数组 arguments
  5. 解决stackoverflow打开慢不能注册登录
  6. 利用vector进行图的存储
  7. Palo Alto Networks 支持仪表盘漏洞泄露数千份客户支持工单
  8. chrome 设置是否缓存
  9. 【BZOJ2038】【2009国家集训队】小Z的袜子(hose) 分块+莫队
  10. Go开源项目 - gorp使用方法
  11. python环境变量设置失败
  12. 有关自己人事档案(学籍)怎么查询攻略
  13. Vm虚拟机安装Linux系统教程
  14. Mac eclipse下载地址 Java开发
  15. 投入产出实例matlab,基于MATLAB的投入产出分析
  16. vue 取数组第一个值_vue里如何取出数组中的数组(的某一个元素)
  17. 对路径“C:\”的访问被拒绝
  18. 当AI走进工厂,“小轴承”也可以转动“大产业”
  19. 用C#调整Excel 的行高和列宽
  20. java:上传微信临时文件的素材

热门文章

  1. 计算机专业可以评电力工程职称吗,电力工程类职称评审专业范围,你了解多少?...
  2. word选择性粘贴html和rtf,Word“选择性粘贴”功能有妙用
  3. 数字转日期 matlab,excel – 如何在MATLAB中将日期转换为数字并再返回
  4. 再看快速排序(QuickSort)
  5. 解决小程序直播组件live-player全屏问题
  6. 弘玑Cyclone发布全线产品 | 多个产品与功能系行业首创
  7. 山西好点的计算机专科学校排名及分数线,2019山西十大专科学校排名及高考录取分数线...
  8. latex 在线表格编辑器
  9. 小肩膀易语言四期POST+JS
  10. Java集成DataX