HDU 2896 病毒侵袭 AC自动机
生活随笔
收集整理的這篇文章主要介紹了
HDU 2896 病毒侵袭 AC自动机
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我表示不是很懂HDU卡內存的優良傳統.......以及他們卡輸出的良好風尚........
AC自動機裸體關鍵在于http://ascii.911cha.com/
#include<cstring> #include<cstdio> #include<vector> #include<iostream> #include<algorithm> using namespace std; struct Trie {Trie *ch[95],*fail,*last;int who,had;Trie(){memset(ch,0,sizeof(ch));fail=last=NULL;who=had=0;} }*root,*q[70005]; int n,m; char c[200],w[10005]; inline void insert(char *s,int x) {int len=strlen(s);Trie *now=root;for(int i=0;i<len;i++){if(now->ch[s[i]-32]==NULL)now->ch[s[i]-32]=new Trie();now=now->ch[s[i]-32];}now->who=x; } inline void build() {q[0]=root;for(int i=0,j=0;i<=j;i++)for(int l=0;l<95;l++)if(q[i]->ch[l]){Trie *now=q[i]->fail;while(now&&!now->ch[l])now=now->fail;q[++j]=q[i]->ch[l];q[j]->fail=now?now->ch[l]:root;q[j]->last=q[j]->who?q[j]:q[j]->fail->last;}elseq[i]->ch[l]=q[i]==root?root:q[i]->fail->ch[l]; } inline void Init() {root=new Trie();scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%s",c);insert(c,i);}build(); } vector<int> have; int ans; inline void job(int x) {scanf("%s",w);int len=strlen(w);have.clear();Trie *now=root;for(int i=0;i<len;i++){now=now->ch[w[i]-32];for(Trie *Now=now->last;Now;Now=Now->last)if(Now->had!=x)have.push_back(Now->who),Now->had=x;elsebreak;}if(have.empty())return;printf("web %d:",x);ans++;sort(have.begin(),have.end());for(int j=0;j<have.size();j++)printf(" %d",have[j]);printf("\n"); } inline void work() {scanf("%d",&m);for(int i=1;i<=m;i++)job(i); } inline void print() {printf("total: %d\n",ans); } int main() {Init(); work();print();return 0; }?
轉載于:https://www.cnblogs.com/TSHugh/p/7131565.html
總結
以上是生活随笔為你收集整理的HDU 2896 病毒侵袭 AC自动机的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ef6+mysql的bug
- 下一篇: 虚幻4引擎角色蓝图Character的M