链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3695
题目大意:
给出n个匹配串,然后给一个长串S,找出S中有多少种匹配串。注意匹配串在S中正反出现的顺序都算是出现。
S可能会有些段是压缩过的,格式为【qx】,q是一个数字,x是一个字母,代表有q个x。 假设 把B【3A】DE“解压缩”之后,就是
BAAADE
分析与总结:
一开始没弄清题意,以为是求所有病毒的总数量,结果一直TLE。。其实这题要求的是源码中有多少种病毒。一旦找到一个病毒,就把Trie中的那个单词标记为false.
另外把“压缩”的网站源码进行“解压缩”的步骤也要细心点,容易出错。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN = 5200005;
const int MAX_NODE = 3000005;
const int SIGMA_SIZE = 26;
char str[MAXN];
char buf[MAXN];
struct node{
node* fail;
node* next[SIGMA_SIZE];
bool isword;
int id;
void init(){
id=0;
isword=false;
memset(next, 0, sizeof(next));
}
};
class AC_Automation{
public:
void init();
void insert(char*,int);
void getFail();
int find(char*);
private:
node* new_node();
int ID(char ch){return ch-'A';}
private:
node Heap[MAX_NODE];
node* root;
int size;
};
node* AC_Automation::new_node(){
Heap[size].init();
return &Heap[size++];
}
void AC_Automation::init(){
size=0;
root=new_node();
}
void AC_Automation::insert(char* str,int id){
node* p=root;
for( ; *str; ++str){
int ch=ID(*str);
if(p->next[ch] == NULL)
p->next[ch] = new_node();
p = p->next[ch];
}
p->isword=true;
p->id=id;
}
void AC_Automation::getFail(){
queue<node*>Q;
Q.push(root);
while(!Q.empty()){
node* tmp=Q.front(); Q.pop();
for(int i=0; i<SIGMA_SIZE; ++i){
node*& now = tmp->next[i];
if(now != NULL){
Q.push(now);
if(tmp==root) now->fail=root;
else{
node* p=tmp->fail;
while(p != NULL){
if(p->next[i] != NULL){
now->fail = p->next[i];
break;
}
p = p->fail;
}
if(p==NULL) now->fail=root;
}
}
else{
if(tmp==root) now = root;
else now=tmp->fail->next[i];
}
}
}
}
int AC_Automation::find(char* str){
node* p=root;
int cnt=0;
for( ; *str; ++str){
int ch=ID(*str);
p = p->next[ch];
if(p==NULL) p=root;
node* tmp=p;
while(tmp!=root && tmp->isword){
++cnt;
tmp->isword=false;
tmp = tmp->fail;
}
}
return cnt;
}
// 字符串转置
inline void rev(char* str, int len){
char ch;
for(int i=0, k=len-1; i<len/2; ++i,--k){
ch=str[i]; str[i]=str[k]; str[k]=ch;
}
}
// 字符串“解压缩”
inline void change(char* str,char* now){
memset(now, 0, sizeof(now));
int len=strlen(str);
int i;
for(i=0; i<len; ++i){
if(str[i]=='['){
++i;
int num=0;
while(str[i]>='0' && str[i]<='9'){
num = num*10 + str[i] - '0';
++i;
}
while(num--){
*now = str[i];
++now;
}
++i;
}
else{
*now=str[i];
++now;
}
}
}
AC_Automation ac;
int main(){
int n,nCase;
char pat[1005];
scanf("%d",&nCase);
while(nCase--){
ac.init();
scanf("%d%*c",&n);
for(int i=1; i<=n; ++i){
gets(pat);
ac.insert(pat, i);
}
ac.getFail();
memset(buf, 0, sizeof(buf));
memset(str, 0, sizeof(str));
gets(buf);
change(buf,str);
int ans=ac.find(str);
rev(str,strlen(str));
printf("%d\n", ans+ac.find(str));
}
return 0;
}
—— 生命的意义,在于赋予它意义士。
原创http://blog.csdn.net/shuangde800,By
D_Double (转载请标明)
分享到:
相关推荐
hdu 3341(ac自动机+状态压缩) 题意:容易理解... 思路:首先一开始容易想到要用到dp,开设一个dp[41][41][41][41][501]的数组来解决,但是明显内存已经超出范围了,于是就想如何减少内存呢?只要知道A、T、C、G其中...
HDU最全ac代码
hdu2000-2014ac代码,虽然只有几道,但都是简单的
收集的部分HDOJ杭电ACM题的代码 大牛勿下 全是基础供初级acmer使用
一个十分简单的程序,能够ac杭电hdu的第2050题,无注释,简单明了
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
hdu杭电所有题目按照ac数量排序,python分析
hdu2101AC代码
hdu 一些简单题目 ac代码 大概100道
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
自己做的HDU ACM已经AC的题目
ACM hdu 代码大全3000例,hdu已经AC的3000例代码,部分代码有详细解析
一款 HDU OJ 的自动刷题工具,搜索来源可选用 百度搜索 / 必应搜索,支持并行以及串行查找,祝早日刷上航电首页哦~ 使用 Python 编写,执行之前你需要在同目录下创建文件 aclog.txt ,然后粘贴进你目前已经 AC 的...
有题,有解题思路,有解题代码 hdu2516、poj1067和hdu1527、hdu2177、hdu2176等等
HDU1059的代码
杭电ACMhdu1163
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
这是我自己的AC代码,都是自己做的,有的很容易出错,大家看的时候要仔细点,加油