NEFU704(AC自动机+状态压缩)
生活随笔
收集整理的這篇文章主要介紹了
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自动机+状态压缩)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU3388(二分+容斥原理)
- 下一篇: NEFU705(数论+DP)