From 6650b9c93f107a2bfd8ad96d1ad3ad8031091a28 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sun, 18 Sep 2016 09:02:57 +0800 Subject: [PATCH] Create 5880_wangdongkai.cpp From http://www.cnblogs.com/wangdongkai/p/5880122.html --- HDOJ/5880_wangdongkai.cpp | 135 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 HDOJ/5880_wangdongkai.cpp diff --git a/HDOJ/5880_wangdongkai.cpp b/HDOJ/5880_wangdongkai.cpp new file mode 100644 index 0000000..7c4be4c --- /dev/null +++ b/HDOJ/5880_wangdongkai.cpp @@ -0,0 +1,135 @@ +/* + * HDU 5880 Family View + * 把文本串中所有的模式串换为等长度的'*' + * + * AC自动机 + * 将模式串建立AC自动机,然后进行文本串匹配,求出文本串的每个前缀 + * 所包含的最长后缀,并且是模式串,用pos数组记录位置,然后扫描一遍输出即可。 + */ + +#include +#include +#include +#include +#include +#include +using namespace std; + +const int MAXN = 1000000+100; +char str[MAXN]; +int pos[MAXN]; +struct Aho_Corasick +{ + const static int maxnode = 1000000+100; + const static int type = 26; + int next[maxnode][type],fail[maxnode],end[maxnode],l[maxnode]; + int root,L; + int newnode() + { + for(int i = 0;iQ; + fail[root] = root; + for(int i = 0;i < type;i++) + if(next[root][i] == -1) + next[root][i] = root; + else + { + fail[next[root][i]] = root; + Q.push(next[root][i]); + } + while(!Q.empty()) + { + int now = Q.front(); + Q.pop(); + for(int i = 0;i < type;i++) + if(next[now][i] == -1) + next[now][i] = next[fail[now]][i]; + else + { + fail[next[now][i]] = next[fail[now]][i]; + Q.push(next[now][i]); + } + } + } + void work(char* str) + { + int id; + int len = strlen(str); + int now = root; + memset(pos,0,sizeof(pos)); + for(int i = 0;i < len;i++) + { + if(str[i]>='A'&&str[i]<='Z') id=str[i]-'A'; + else if(str[i]>='a'&&str[i]<='z') id=str[i]-'a'; + else continue; + now = next[now][id]; + int temp=now; + while(temp != root) + { + if(end[temp] != -1) + { + pos[i+1]-=1; + pos[i-l[temp]+1]+=1; + break; + } + temp = fail[temp]; + } + } + long long cnt=0; + for(int i=0;i