首页 > 其他分享 >HDU 3695 Computer Virus on Planet Pandora

HDU 3695 Computer Virus on Planet Pandora

时间:2022-11-09 20:41:56浏览次数:70  
标签:HDU int 3695 next Planet program cnt now root


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


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

 

HDU 3695 Computer Virus on Planet Pandora_HDU

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;
}




标签:HDU,int,3695,next,Planet,program,cnt,now,root
From: https://blog.51cto.com/u_15870896/5838710

相关文章

  • HDU 5379 Mahjong tree
    ProblemDescriptionLittlesunisanartist.Todayheisplayingmahjongalone.Hesuddenlyfeelsthatthetreeintheyarddoesn'tlookgood.Sohewant......
  • HDU 3397 Sequence operation
    ProblemDescriptionlxhgwwgotasequencecontainsncharacterswhichareall'0'sor'1's.Wehavefiveoperationshere:Changeoperations:0ab......
  • HDU 1255 覆盖的面积
    ProblemDescription给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试......
  • HDU 2665 Kth number
    ProblemDescriptionGiveyouasequenceandaskyouthekthbignumberofainteval.InputThefirstlineisthenumberofthetestcases. F......
  • HDU 2715 Herd Sums
    DescriptionThecowsinfarmerJohn'sherdarenumberedandbrandedwithconsecutiveintegersfrom1toN(1<=N<=10,000,000).Whenthecowscometot......
  • HDU 5339 Untitled
    ProblemDescriptiona and n integers b1,…,bn.Afterselectingsomenumbersfrom b1,…,bn inanyorder,say c1,…,cr,wewanttom......
  • HDU 3760 Ideal Path
    ProblemDescriptionNewlabyrinthattractionisopeninNewLostlandamusementpark.Thelabyrinthconsistsofnroomsconnectedbympassages.Eachpass......
  • HDU 4101 Ali and Baba
    ProblemDescriptionThereisarectanglearea(withNrowsandMcolumns)infrontofAliandBaba,eachgridmightbeoneofthefollowing:1.Empty......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......