字符串 KMP Trie AC自动机 后缀数组
来源:互联网 发布:青岛搜索引擎优化外包 编辑:程序博客网 时间:2024/05/21 03:24
还在看后缀数组,罗穗骞神牛的课件真是非常给力。
今天做了场字符串的练习,包括KMP,Trie,AC自动机和后缀数组。
A. Oulipo
貌似是POJ的,以前做过。直接用KMP水过了 。
B. 统计难题
是HDU的吧,题意就是求一些串是另一些串前缀的个数,直接用Trie搞。
struct trie{ int count ; struct trie *next[26] ; trie(){ mem(next,0) ; count = 0 ; }} ;trie *root = 0 ;void build(char *a){ int l = strlen(a) ; trie *p = root ; trie *temp = 0 ; for (int i = 0 ; i < l ;i ++ ){ int tt = a[i] - 'a' ; if(p -> next[tt] == 0){ temp = new trie ; p -> next[tt] = temp ; } p = p -> next[tt] ; p -> count ++ ; }}int search(char *a){ int l = strlen(a) ; trie *p = root ; bool flag = 0 ; for (int i = 0 ; i < l ; i ++ ){ int tt = a[i] - 'a' ; if(p -> next[tt] == 0){ flag = 1 ; break ; } p = p -> next[tt] ; } if(flag)return 0 ; return p -> count ;}int main() { char a[11111] ; root = new trie ; int d = 5 ; while(gets(a)){ int l = strlen(a) ; if(!l)break ; build(a) ; } while(cin >> a){ cout << search(a) << endl; } return 0 ;}C. Keywords Search
HDU的题,我用了三种方法,用N次KMP ,TLE,N棵Trie树,MLE(纯娱乐。。),AC自动机A掉。
题意就是给你一些字符串,问在目标串里面出现了多少次。
AC自动机的学习课件网上很多,我就说下我自己对AC自动机的理解。
其实AC自动机就是KMP+Trie,他的Fail指针是和KMP的next数组一样的作用。
Fail指针是指向当前节点字母的上一次出现该字母的位置,如果没有则指向root。
具体请看神牛博客。神牛博客
//HDU 2222struct node { node *fail ; node *next[26] ; int count ; node() { fail = 0 ; count = 0 ; mem(next , 0) ; }}*qe[500005] ;node *root = 0 ;//insert a[] .void insert(char *a) { node *p = root ; int l = strlen(a) ; for (int i = 0 ; i < l ; i ++ ) { int tt = a[i] - 'a' ; if(p -> next[tt] == 0) { p -> next[tt] = new node() ; } p = p -> next[tt] ; } p -> count ++ ;}//build *fail .void build() { root -> fail = 0 ; int h = 0 , t = 0 ; qe[h ++ ] = root ; while(h > t) { node *temp = qe[t ++ ] ; node *p = 0 ; for (int i = 0 ; i < 26 ; i ++ ) { if(temp -> next[i] != 0) { if(temp == root)temp -> next[i] -> fail = root ; else { p = temp -> fail ; while(p != 0) { if(p -> next[i] != 0) { temp -> next[i] -> fail = p -> next[i] ;//找到匹配 break ; } p = p -> fail ; } if(p == 0)temp -> next[i] -> fail = root ;//如果没找到,则将fail指向root } qe[h ++ ] = temp -> next[i] ; } } }}int search(char *a) { int l = strlen(a) ; node *p = root ; int ans = 0 ; for (int i = 0 ; i < l ; i ++ ) { int tt = a[i] - 'a' ; while(p -> next[tt] == 0 && p != root)p = p -> fail ; p = p -> next[tt] ; p = (p == 0) ? root : p ; node *temp = p ; while(temp != root && temp -> count != -1) { ans += temp -> count ; temp -> count = -1 ; temp = temp -> fail ; } } return ans ;}char aa[55] ;char bb[1111111] ;int main() { int T ; cin >> T ; while (T -- ) { int n ; root = new node() ; cin >> n ; for (int i = 0 ; i < n ; i ++ ) { scanf("%s",aa) ; insert(aa) ; } build() ; scanf("%s",bb) ; cout << search(bb) << endl; } return 0 ;}
D. Longest Common Substring
题意是给你2个串,问最长公共字串的长度。
后缀数组,正在看罗穗骞神牛的课件。
找两个字符串的最长公共字串的长度。首先将两个字符串连起来,中间用一个没有出现过的字符连接。
然后利用height数组的特性,我们可以找出位于两个不同字符串里的后缀的最大的height。
我们知道height[i] 是 sa[i - 1]和sa[i] 的最长公共前缀。
那么我们只需要找那些sa[i - 1]和sa[i] 位于不同字符串的字串就可以了。
具体判断请看代码。
#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cmath>#include <cstring>#include <queue>#include <set>#include <vector>#include <stack>#include <map>#include <iomanip>#define PI acos(-1.0)#define Max 2505#define inf 1<<28#define LL(x) ( x << 1 )#define RR(x) ( x << 1 | 1 )#define REP(i,s,t) for( int i = ( s ) ; i <= ( t ) ; ++ i )#define mem(a,b) memset(a,b,sizeof(a))#define mp(a,b) make_pair(a,b)#define PII pair<int,int>using namespace std;#define N 200005/****后缀数组模版****/#define F(x)((x)/3+((x)%3==1?0:tb)) //F(x)求出原字符串的suffix(x)在新的字符串中的起始位置#define G(x)((x)<tb?(x)*3+1:((x)-tb)*3+2) //G(x)是计算新字符串的suffix(x)在原字符串中的位置,和F(x)为互逆运算int wa[N],wb[N],wv[N],WS[N];int sa[N*3] ;int rank1[N],height[N];int r[N*3];int c0(int *r,int a,int b) { return r[a]==r[b] && r[a+1]==r[b+1] && r[a+2]==r[b+2];}int c12(int k,int *r,int a,int b) { if(k==2) return r[a]<r[b] || ( r[a]==r[b] && c12(1,r,a+1,b+1) ); else return r[a]<r[b] || ( r[a]==r[b] && wv[a+1]<wv[b+1] );}void sort(int *r,int *a,int *b,int n,int m) { int i; for(i=0; i<n; i++) wv[i]=r[a[i]]; for(i=0; i<m; i++) WS[i]=0; for(i=0; i<n; i++) WS[wv[i]]++; for(i=1; i<m; i++) WS[i]+=WS[i-1]; for(i=n-1; i>=0; i--) b[--WS[wv[i]]]=a[i]; return;}//注意点:为了方便下面的递归处理,r数组和sa数组的大小都要是3*nvoid dc3(int *r,int *sa,int n,int m) { //rn数组保存的是递归处理的新字符串,san数组是新字符串的sa int i , j , *rn = r+n , *san = sa+n , ta = 0 ,tb = (n+1)/3 , tbc = 0 , p; r[n] = r[n+1] = 0; for(i=0; i<n; i++) { if(i%3!=0) wa[tbc++]=i; //tbc表示起始位置模3为1或2的后缀个数 } sort(r+2,wa,wb,tbc,m); sort(r+1,wb,wa,tbc,m); sort(r,wa,wb,tbc,m); for(p=1,rn[F(wb[0])]=0,i=1; i<tbc; i++) rn[F(wb[i])]=c0(r,wb[i-1],wb[i])?p-1:p++; if(p<tbc) dc3(rn,san,tbc,p); else { for(i=0; i<tbc; i++) san[rn[i]]=i; }//对所有起始位置模3等于0的后缀排序 for(i=0; i<tbc; i++) { if(san[i]<tb) wb[ta++]=san[i]*3; } if(n%3==1) //n%3==1,要特殊处理suffix(n-1) wb[ta++]=n-1; sort(r,wb,wa,ta,m); for(i=0; i<tbc; i++) wv[wb[i]=G(san[i])]=i;//合并所有后缀的排序结果,保存在sa数组中 for(i=0,j=0,p=0; i<ta&&j<tbc; p++) sa[p]=c12(wb[j]%3,r,wa[i],wb[j])?wa[i++]:wb[j++]; for(; i<ta; p++) sa[p]=wa[i++]; for(; j<tbc; p++) sa[p]=wb[j++]; return;}//height[i]=suffix(sa[i-1])和suffix(sa[i])的最长公共前缀,也就是排名相邻的两个后缀的最长公共前缀void calheight(int *r,int *sa,int n) { int i,j,k=0; for(i=1; i<=n; i++) rank1[sa[i]]=i; for(i=0; i<n; height[rank1[i++]]=k) for(k?k--:0,j=sa[rank1[i]-1]; r[i+k]==r[j+k]; k++);}inline void RD(int &ret) { char c; do { c = getchar(); } while(c < '0' || c > '9') ; ret = c - '0'; while((c=getchar()) >= '0' && c <= '9') ret = ret * 10 + ( c - '0' );}inline void OT(int a) { if(a >= 10)OT(a / 10) ; putchar(a % 10 + '0') ;}char a[N] ;int ans = 0 ;int main() { while(scanf("%s",a) != EOF) { ans = 0 ; int l = strlen(a) ; a[l] = '*' ; scanf("%s", a + l + 1) ; int ll = strlen(a) ; for (int i = 0 ; i < ll ; i ++ )r[i] = (int)a[i] ; r[ll] = 0 ; dc3(r ,sa ,ll + 1,128) ; calheight(r , sa , ll) ; for (int i = 1 ; i < ll ; i ++ ) { if((sa[i] > l && sa[i - 1] < l ) || (sa[i] < l && sa[i - 1] > l) ) { ans = max(ans ,height[i]) ; } } cout << ans << endl; } return 0 ;}
- 字符串 KMP Trie AC自动机 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组(转)
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串:KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
- 字符串模板 KMP AC自动机 Manacher 后缀数组
- Trie、KMP、AC自动机小结
- kmp,trie树,ac自动机
- 关于Trie KMP AC自动机
- KMP&&trie树&&AC自动机
- 字符串专题(trie,KMP,AC自动机,manacher)
- 从KMP算法,trie树再到AC自动机
- 字符串专题 kmp-trie-ac
- MFC 多线程(转)
- hdu 1133
- RCP 启动时提示缺少插件,问题解决方法
- python小技巧 全排列的实现和join函数
- HDFS小文件问题及解决方案
- 字符串 KMP Trie AC自动机 后缀数组
- Binary Tree Level Order Traversal II
- 把长方形的构造函数写的文艺点儿
- PC行业奄奄一息,移动时代威力巨大
- hdu1913-Prime Cuts
- Spring IOC和DI原理讲解并制作LazyCoder版的Spring (二)
- SVN常用命令
- sql语句中Where 1=1的意思
- 帝国CMS和PHPCMS对比随谈