日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NEFU704(AC自动机+状态压缩)

發布時間:2024/4/11 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NEFU704(AC自动机+状态压缩) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:Password Leakage


#include <iostream> #include <string.h> #include <stdio.h> #include <queue>using namespace std;char S[1000010]; char keyword[51]; char str[51]; char T[51];class Trie {public:int count;Trie *fail;Trie *next[26];Trie(){count=0;fail=NULL;memset(next,NULL,sizeof(next));} };Trie *root;queue<Trie*> Q;void Insert(char *S,int msk) {int len=strlen(S);Trie *p=root;for(int i=0; i<len; i++){int id=S[i]-'a';if(p->next[id]==NULL)p->next[id]=new Trie();p=p->next[id];}p->count|=msk; }int Delete(Trie *T) {if(T==NULL) return 0;for(int i=0; i<26; i++)if(T->next[i]!=NULL)Delete(T->next[i]);delete T;return 0; }void Build_AC() {Q.push(root);root->fail=NULL;while(!Q.empty()){Trie *p=NULL;Trie *tmp=Q.front();Q.pop();for(int i=0; i<26; i++){if(tmp->next[i]){if(tmp==root) tmp->next[i]->fail=root;else{p=tmp->fail;while(p){if(p->next[i]){tmp->next[i]->fail=p->next[i];break;}p=p->fail;}if(p==NULL) tmp->next[i]->fail=root;}Q.push(tmp->next[i]);}}} }int Query() {Trie *p=root;int index,result=0;int len=strlen(S);for(int i=0; i<len; i++){index=S[i]-'a';while(p->next[index]==NULL&&p!=root) p=p->fail;p=p->next[index];if(p==NULL) p=root;Trie *temp=p;while(temp!=root&&temp->count!=-1){result|=temp->count;temp->count=-1;temp=temp->fail;}}return result; }int Bin(int msk) {int t=0;while(msk){if(msk&1) t++;msk>>=1;}return t; }int main() {int n,m,x,y;char tmp;while(~scanf("%s",S)){root=new Trie();x=y=-1;scanf("%d",&m);scanf("%s",str);for(int i=0; i<m; i++){if(str[i]=='1'){if(x==-1) x=i;else y=i;}}scanf("%d",&n);for(int i=0;i<n;i++){scanf("%s",keyword);if(x!=-1){tmp=keyword[x];for(int j=0;j<26;j++){keyword[x]='a'+j;Insert(keyword,1<<i);}keyword[x]=tmp;}if(y!=-1){for(int j=0;j<26;j++){keyword[y]='a'+j;Insert(keyword,1<<i);}}Insert(keyword,1<<i);}Build_AC();printf("%d\n",Bin(Query()));Delete(root);}return 0; }



總結

以上是生活随笔為你收集整理的NEFU704(AC自动机+状态压缩)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。