HDU 3695 Computer Virus on Planet Pandora

来源:互联网 发布:淘宝赚钱助手是真的吗 编辑:程序博客网 时间:2024/05/21 17:09
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 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.
 

Input
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.
 

Output
For each test case, print an integer K in a line meaning that the program is infected by K viruses.
 

Sample Input
32ABDCBDACB3ABCCDEGHIABCCDEFIHG4ABBACDEEBBBFEEEA[2B]CD[4E]F
 

Sample Output
032
Hint
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’.
 


ac自动机前后比两次就好了

#include<cstdio>  #include<cstring>  #include<cmath>  #include<algorithm>  #include<queue>  using namespace std;typedef long long ll;const int size = 26;//字典树节点大小 const int base = 'A';//字典树 const int maxn = 5100005;//字典树大小(便于各种操作) class tire{public:tire *next[size], *fail;int cnt, id;//各种节点设置 tire(){ cnt = 0; fail = NULL; memset(next, 0, sizeof(next)); };void clear(){ cnt = 0; fail = NULL; memset(next, 0, sizeof(next)); };};//字典树节点设置 class Ac_machine{private://根节点建立 tire *node[maxn];//与id相对立,便于各种操作 int tot;//字典树大小 char s[maxn];//读入字符串 char ss[maxn];char S[maxn];//母串 public:tire *root;Ac_machine(){}void clear(){ node[0] = root = new tire; tot = 0; }//初始化 void insert(int);//插入多个字符串 int insert();//插入字符串,返回字符串大小 void getfail(); //获取失配指针 int getmother();//获得匹配的母串,返回母串大小 ll work_out();//求解函数,依照题目不同而不同,返回答案 };//ac自动机设置 int Ac_machine::getmother(){scanf("%s", ss);int i, j, k;for (i = 0, j = 0; ss[i]; i++){if (ss[i] == '['){for (i++, k = 0; ss[i] != ']'; i++){if (ss[i] <= '9'&&ss[i] >= '0') k = k * 10 + ss[i] - '0';else for (; k; k--) S[j++] = ss[i];}}else S[j++] = ss[i];}S[j] = 0;return 0; strlen(S);}void Ac_machine::insert(int n){ while (n--) insert(); }int Ac_machine::insert(){scanf("%s", s);int i, k;tire *now = root;for (i = 0, k; s[i]; i++){k = s[i] - base;if (!now->next[k]){++tot;if (node[tot] == NULL) node[tot] = new tire;else node[tot]->clear();now->next[k] = node[tot];now->next[k]->id = tot;}now = now->next[k];}now->cnt++;//可以插入一些字典树的设置 return 0; strlen(s);}void Ac_machine::getfail(){queue<tire*> p;root->fail = root;for (int i = 0; i < size; i++)if (root->next[i]){root->next[i]->fail = root;p.push(root->next[i]);}else root->next[i] = root;tire *now;while (!p.empty()){now = p.front();    p.pop();//now->cnt += now->fail->cnt;//可以插入统计子串个数等操作 for (int i = 0; i < size; i++)if (now->next[i]){now->next[i]->fail = now->fail->next[i];p.push(now->next[i]);}else now->next[i] = now->fail->next[i];}}ll Ac_machine::work_out(){ll ans = 0;getmother();getfail();tire *now = root;int i, k;//for (int i = 0; i < maxn; i++) printf("%d\n", node[i]->cnt); for (i = 0; S[i]; i++){k = S[i] - base;now = now->next[k];for (tire*t = now; t != root&&t->cnt; t = t->fail){ans += t->cnt;t->cnt = 0;}}now = root;for (i--; i >= 0; i--){k = S[i] - base;now = now->next[k];for (tire*t = now; t != root&&t->cnt; t = t->fail){ans += t->cnt;t->cnt = 0;}}return ans;//返回子串出现次数 }int T, n;Ac_machine ac;int main(){scanf("%d", &T);while (T--){ac.clear();scanf("%d", &n);ac.insert(n);printf("%I64d\n", ac.work_out());}return 0;}



0 0
原创粉丝点击