HDU - 3695 Computer Virus on Planet Pandora AC自动机+优化
来源:互联网 发布:sql多张表合并 union 编辑:程序博客网 时间:2024/05/26 09:57
Computer Virus on Planet Pandora
HDU - 3695Aliens 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.
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.
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:
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,
‘KKKKKK’ in the original program. So, if a compressed program is like:
AB
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.
32ABDCBDACB3ABCCDEGHIABCCDEFIHG4ABBACDEEBBBFEEEA[2B]CD[4E]F
032
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’.
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <queue>#include <map>#include <sstream>using namespace std;typedef long long LL;const int CHAR_SIZE = 26;const int MAX_SIZE = 2.5e5 + 8;struct AC_Machine{ int ch[MAX_SIZE][CHAR_SIZE], danger[MAX_SIZE], fail[MAX_SIZE]; int sz; inline void init(){ sz = 1; memset(ch[0], 0, sizeof ch[0]); memset(danger, 0, sizeof danger); } inline void _insert(char *s){ int n = strlen(s); int u = 0, c; for(int i = 0; i < n; i++){ c = s[i] - 'A'; if(!ch[u][c]){ memset(ch[sz], 0, sizeof ch[sz]); danger[sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; } danger[u]++; } inline void _build(){ queue<int> Q; fail[0] = 0; for(int c = 0, u; c < CHAR_SIZE; c++){ u = ch[0][c]; if(u){Q.push(u); fail[u] = 0;} } int r; while(!Q.empty()){ r = Q.front(); Q.pop(); //danger[r] |= danger[fail[r]]; for(int c = 0, u; c < CHAR_SIZE; c++){ u = ch[r][c]; if(!u){ch[r][c] = ch[fail[r]][c]; continue; } fail[u] = ch[fail[r]][c]; Q.push(u); } } }}ac;const int maxn = 5.1e6 + 8;char s[maxn];int main(){ #ifdef LOCAL freopen("4.in", "r", stdin); //freopen("4.out", "w", stdout); #endif // LOCAL //ios::sync_with_stdio(false); //cin.tie(0); int T, n, len, i, v, ans, now, tmp; char ch; //for(int i = 0; i < 26; i++){mp[(i + 'A')] = i;} scanf("%d", &T); while(T--){ scanf("%d", &n); ac.init(); while(n--){ scanf("%s", s); ac._insert(s); } ac._build(); len = 0, v = 1; getchar(); while(true){ ch = getchar(); if(ch == '['){ scanf("%d", &v); } else if(ch == ']') ; else if(isalpha(ch)){ while(v--){ s[len] = ch; len++; } v = 1; } else break; } s[len] = '\0'; //printf("%s\n", s); ans = 0; now = 0; for(i = 0; i < len; i++){ now = ac.ch[now][s[i] - 'A']; tmp = now; while(tmp){ if(ac.danger[tmp] > 0){ ans += ac.danger[tmp]; ac.danger[tmp] = -1; } else if(ac.danger[tmp] == -1)break; tmp = ac.fail[tmp]; } } now = 0; for(i = len - 1; i >= 0; i--){ now = ac.ch[now][s[i] - 'A']; tmp = now; while(tmp){ if(ac.danger[tmp] > 0){ ans += ac.danger[tmp]; ac.danger[tmp] = -1; } else if(ac.danger[tmp] == -1)break; tmp = ac.fail[tmp]; } } printf("%d\n", ans); } return 0;}
Thank you!
0 0
- HDU - 3695 Computer Virus on Planet Pandora AC自动机+优化
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora(AC自动机)
- hdu 3695 Computer Virus on Planet Pandora AC自动机
- hdu 3695 Computer Virus on Planet Pandora ac自动机
- HDU 3695 Computer Virus on Planet Pandora (AC自动机)
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- 【AC自动机】HDU 3695 Computer Virus on Planet Pandora 裸题
- hdu 3695 Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora (AC自动机)
- HDU - 3695 - Computer Virus on Planet Pandora(AC自动机)
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- HDU 3695 Computer Virus on Planet Pandora AC自动机
- HDU 3695:Computer Virus on Planet Pandora(AC自动机裸题,数组实现AC自动机)
- hdoj 3695 Computer Virus on Planet Pandora 【AC自动机】
- hdu 2896病毒侵袭 && hdu 3695 Computer Virus on Planet Pandora AC自动机
- HDU 3695 Computer Virus on Planet Pandora and HDU 2896 病毒侵袭(AC自动机裸题)
- HDU 3695 Computer Virus on Planet Pandora(10年福州 AC自动机)
- lua的table类型
- hdu1846-Brave Game(BashGame入门)
- HTML入门
- find命令
- LeetCode
- HDU - 3695 Computer Virus on Planet Pandora AC自动机+优化
- CCF201604-1 折点计数(解法二)(100分)
- 排序算法汇总一------插入类算法
- 文章标题
- 对于特殊的边界值特殊考虑的问题
- java 多线程总结
- VIM的使用
- Android学习笔记--多渠道打包
- 将有序数组转变成平衡二叉查找树