hdu 2896 病毒侵袭 DFA 主串匹配那几个字符串

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<queue>

#include<algorithm>

using namespace std;

//DFA 求字符串最多匹配多少字符串

const int maxn=2000000;

struct Node

{

int num;//出现次数

int index;//序号

Node *next[128];//后继指针

Node* fail;//失败指针

/*指向树中出现过的S的最长的后缀,换句话说就是在前缀集中出现的最长的S的后缀.*/

};

//字典树

Node temp[maxn];

int tp;

//.........

int n;//字符串个数

Node *root;//头指针

void reset(Node* rot)

{

rot->num=0;

for(int i=0;i<128;i++) rot->next[i]=NULL;

rot->fail=root;//important

if(rot==root) rot->fail=NULL;//important

}

//初始化

void init()

{

tp=0;

root=&temp[tp++];

reset(root);

}

//插入字典树

void insert(char *word,int index)

{

Node *p=root;

for(int i=0;word[i];i++)

{

int x=word[i];

if(p->next[x]==NULL)

{

p->next[x]=&temp[tp++];

reset(p->next[x]);

}

p=p->next[x];

}

p->num=index;

}

//构造失败指针

Node *que[maxn];

int front,rear;

void DFA()

{

Node* p=root;

front=rear=0;

que[rear++]=p;

while(front<rear)

{

Node* t=que[front++];

for(int i=0;i<128;i++)

{

Node* cnt=t->next[i];

if(cnt!=NULL)

{

Node* fath=t->fail;

while(fath!=NULL&&fath->next[i]==NULL)

{

fath=fath->fail;

}

if(fath!=NULL)

{

cnt->fail=fath->next[i];

}

else

{

cnt->fail=p;//important

}

que[rear++]=cnt;

}

}

}

}

int mat[3000];

int l;

//查看str最多匹配多少字符串

void find(char* str)

{

Node* p=root;

Node* fath;

Node* cnt;

for(int i=0;str[i];i++)

{

int x=str[i];

cnt=p->next[x];

if(cnt!=NULL)

{

for(fath=cnt;fath!=NULL;fath=fath->fail)

{

if(fath->num)

{

mat[l++]=fath->num;

}

}

p=cnt;

}

else

{

fath=p->fail;

while(fath!=NULL&&fath->next[x]==NULL)

{

fath=fath->fail;

}

if(fath!=NULL)

{

cnt=fath->next[x];

for(fath=cnt;fath!=NULL;fath=fath->fail)

{

if(fath->num)

{

mat[l++]=fath->num;

}

}

p=cnt;

}

else

{

p=root;

}

}

}

}

 

char str[1000005];

int main()

{

while(scanf("%d ",&n)==1)//important  空格

{

init();//初始化

for(int i=0;i<n;i++)

{

gets(str);

insert(str,i+1);//在字典树上插入字符串

}

DFA();//构造失败指针

int m;scanf("%d",&m);

int cnt=0;

for(int i=1;i<=m;i++)

{

l=0;

scanf("%s",str);

find(str);

sort(mat,mat+l);

l=unique(mat,mat+l)-mat;

if(l)

{

cnt++;

printf("web %d:",i);

for(int j=0;j<l;j++) printf(" %d",mat[j]);

printf("/n");

}

}

printf("total: %d/n",cnt);

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符串哈希滑动窗口是一种用于处理字符串的算法。它主要用于在给定的字符串中找到满足特定条件的子串。 在字符串哈希滑动窗口算法中,我们首先计算原始字符串的哈希值。然后,我们使用一个滑动窗口来遍历字符串,每次滑动一个固定长度的窗口。我们可以通过比较每个窗口内的子串的哈希值来判断是否满足条件。 具体而言,我们可以使用BKDRHash等哈希函数来计算字符串的哈希值。然后,我们枚举每个可能的起点,并使用滑动窗口来计算窗口内的子串的哈希值。通过比较窗口内的子串的哈希值,我们可以判断是否满足条件。 对于滑动窗口的移动,如果窗口内的子串满足条件,我们可以继续将窗口往右移动一个固定的长度。如果窗口内的子串不满足条件,我们将窗口的右边界移到最右端,并依次比较新窗口内的子串的哈希值。 综上所述,字符串哈希滑动窗口算法是通过计算字符串的哈希值,并使用滑动窗口来遍历字符串,以找到满足特定条件的子串。这个算法可以高效地处理字符串,并且能够应用于各种字符串相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [String (字符串哈希+滑动窗口)](https://blog.csdn.net/weixin_43872264/article/details/107571742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【字符串hash+滑动窗口】String HDU - 4821](https://blog.csdn.net/qq_45599865/article/details/111143633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值