白衣苍狗

后缀数组

后缀数组转述

概念

要了解后缀数组,首先要知道字符串的后缀。

后缀

​ 对于字符串str,从str的某个位置pos到str的末尾的所有字符依次排列组成的新字符串。

即:

str.suffix(pos) = str.substr(pos,str.length-pos)

/其中substr是C++的库函数(头文件),用来求字符串的子串。用法为:string::substr(起始位置,子串长度)/

我们可以看出,对于一个特定的字符串,它的所有后缀都可以用起始位置pos唯一确定,这在下面会用到。

AC自动机

AC自动机

概念

AC自动机,不是自动AC机,而是Aho-Corasick automation。

采用与kmp类似的方法在trie上构造失败指针来实现快速的字符串匹配。

例题:hdu2222

代码太丑试了几次才勉强卡过。

代码

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
hdu2222
AC automation, Aho-Corasick automation
MODEL Problem
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<queue>
using namespace std;
void write(int A);
int n,t;
struct node
{
	char c;
	int isend;
	int p[27];
	int fail;
	bool used;
	int cnt;
	inline void clear()
	{
		if(used)
		{
			c=isend=used=fail=cnt=0;
			memset(p,0,sizeof(p));
		}
	}
	inline void init(char cc)
	{
		c=cc;
		used=true;
	}
	inline void findFail(const node&);
}Trie[510053];
node& root=Trie[0];
int size=0;
inline void add(char* s)
{
	int len=strlen(s);
	int nownode=0;
	#define now Trie[nownode]
	for(int pos=0;pos+1<=len;++pos)
	{
		
		if(!now.p[s[pos]-96])
		{
			//Trie[++size].init(s[pos]);
			Trie[++size].clear();
			Trie[size].init(s[pos]);
			now.p[s[pos]-96]=size;
			++now.cnt;
		}
		if(pos==len-1)
		{
			++Trie[now.p[s[pos]-96]].isend;
			//return;
		}
		else nownode=now.p[s[pos]-96];
	}
}
inline void node::findFail(const node& ff)
{
	if(Trie[ff.fail].p[c-96])
	{
		fail=Trie[ff.fail].p[c-96];
		return;
	}
	int f=ff.fail;
	#define fa Trie[f]
	while(f!=0&&!Trie[fa.fail].p[c-96])
	{
		f=fa.fail;
	}
	if(f!=0)
	{
		fail=Trie[fa.fail].p[c-96];
	}
	else fail=0;
}
int q[510053];
int b,e;
void initFail()
{
	b=1,e=1;
	root.fail=0;
	q[e++]=0;
	int count=0;
	for(int i=1;i<=26&&count<=Trie[q[b]].cnt;++i)
	{
			if(Trie[q[b]].p[i])
			{
				int son=Trie[q[b]].p[i];
				q[e++]=son;
				++count;
			}
	}
	++b;
	while(e>b)
	{
		count=0;
		for(int i=1;i<=26&&count<=Trie[q[b]].cnt;++i)
		{
			if(Trie[q[b]].p[i])
			{
				int son=Trie[q[b]].p[i];
				Trie[son].findFail(Trie[q[b]]);
				q[e++]=son;
				++count;
			}
		}
		++b;
	}
}
int ACQuery(char* s)
{
	int len=strlen(s);
	int nownode=0,temp=0,ans=0;
	#define now Trie[nownode]
	#define tmp Trie[temp]
	for(int pos=0;pos<=len-1;++pos)
	{
		while(nownode!=0&&!now.p[s[pos]-96])
		{
			nownode=now.fail;
		}
		if(now.p[s[pos]-96])
		{
			nownode=now.p[s[pos]-96];
			temp=nownode;
			while(temp!=0)
			{
				ans+=tmp.isend;
				tmp.isend=0;
				temp=tmp.fail;
			}
		}
	}
	return ans;	
}
char str[53],article[1000003];
int main()
{
	freopen("hdu2222.in","r",stdin);
	//freopen("hdu2222.out","w",stdout);
	scanf("%d",&t);
	for(int s=1;s<=t;++s)
	{
		//memset(Trie,0,sizeof(Trie));
		root.clear();
		root.used=true;
		size=0;
		scanf("%d\n",&n);
		for(int i=1;i<=n;++i)
		{
			gets(str);
			add(str);
		}
		initFail();
		gets(article);
		write(ACQuery(article));
	}
	return 0;
}
void write(int A)
{
	if(!A)putchar('0');
	char buf[15];
	int k=0;
	while(A)
	{
		buf[++k]=A%10;
		A/=10;
	}
	for(int i=k;i>=1;--i)putchar(buf[i]+48);
	putchar('\n');
}

附赠数据生成器

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cstdio>
using namespace std;
int n=10,m,length;
int main()
{
	freopen("hdu2222.in","w",stdout);
	srand(time(NULL));
	cout<<n<<endl;
	for(int s=1;s<=n;++s)
	{
		m=rand()%10001+1;
		cout<<m<<endl;
		for(int i=1;i<=m;++i)
		{
			for(int j=1;j<=5;++j)
			{
				cout<<char(rand()%26+97);
			}
			cout<<endl;
		}
		for(int i=1;i<=1000000;++i)
		{
				cout<<char(rand()%26+97);
		}
		cout<<endl;
	}
	return 0;
}

参考资料

字符串查找树(Trie)

字符串查找树(Trie)

例题:luogu2580

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/*
luogu2580
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int n,m;
char name[55];
struct node
{
	char c;
	bool isend;
	bool called;
	int p[27];
	inline node(){c=0,memset(p,0,sizeof(p)),isend=called=false;}
	inline node(const char cc){c=cc,memset(p,0,sizeof(p)),isend=called=false;}
	inline void init(const char cc){c=cc;}
	inline int Map(char c){return c-97+1;}
	inline bool have(char c){return p[Map(c)]!=0;}
	inline node& nxt(char c);
	inline void add(char* s,int len,int pos);
	inline int query(char* s,int len,int pos);
}Trie[500053],ERROR('#');
node& root=Trie[0];
int size=0;
inline node& node::nxt(char c)
{
	if(have(c))
	{
		return Trie[p[Map(c)]];
	}
	else return ERROR;
}
inline void node::add(char* s,int len,int pos)
{
	if(pos+1>len)return;
	if(pos+1==len)
	{
		isend=true;
		return;
	}
	if(!have(s[pos+1]))
	{
		Trie[++size].init(s[pos+1]);
		p[Map(s[pos+1])]=size;
	}
	nxt(s[pos+1]).add(s,len,pos+1);
}
inline int node::query(char* s,int len,int pos)//1 means OK, 0 means WRONG, -1 means REPEAT
{
	if(pos+1>len)return 0;
	if(pos+1==len)
	{
		if(isend)
		{
			if(called)
			{
				return -1;
			}
			else
			{
				called=true;
				return 1;
			}
		}
		else return 0;
	}
	if(have(s[pos+1]))
		return nxt(s[pos+1]).query(s,len,pos+1);
	else return 0;
}
int main()
{
	freopen("luogu2580.in","r",stdin);
	//freopen("luogu2580.out","w",stdout);
	
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		cin>>name;
		root.add(name,strlen(name),-1);
	}
	
	scanf("%d",&m);
	for(int i=1;i<=m;++i)
	{
		cin>>name;
		int ans=root.query(name,strlen(name),-1);
		if(ans==1)
			cout<<"OK\n";
		else if(ans==0)
			cout<<"WRONG\n";
		else if(ans==-1)
			cout<<"REPEAT\n";
		else 
			cout<<"ERROR"<<endl;
	}
	return 0;
}

Manacher算法

Manacher算法

用于寻找给定字符串中的最长回文子串。

模板(待修改)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Manacher
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//using namespace std;
#define maxn 2*11000003
char s[maxn];
int lens=0,spread[maxn];
int maxl,maxid;

void Manacher()
{
	int id=0,rbound=0;
	maxid=0;maxl=0;
	int i=0;
	for(;i<=lens;++i)
	{
		if(i<rbound)
		{
			int j=2*id-i;
			if(spread[j]<rbound-i)
			{
				spread[i]=spread[j];
			}
			else spread[i]=rbound-i;
		}//else spread[i]=0
		
		while
		( i-spread[i]-1>=0
		&&i+spread[i]+1<=lens
		&&s[i+spread[i]+1]==s[i-spread[i]-1])
		{
			++spread[i];
		}
		if(i+spread[i]>rbound)
			rbound=i+spread[i],id=i;
		if(spread[i]>maxl)
			maxl=spread[i],maxid=i;
	}
}
int main()
{
	s[0]=1;
	while(scanf("%c",&s[++lens])!=EOF)
	{
		if(s[lens]!='\n')s[++lens]=1;
		else 
		{
			s[lens]='\0';
			break;
		}
	}
	--lens;
	Manacher();
	printf("%d",(maxl));
	return 0;
}

例题:luogu1210USACO6.2.1(原1.3.3))

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
Manacher
luogu1210
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
char ss[42003],s[42003],real[4203],ans[42003];
int Map[42003];
int lenss=0,lens=0,lenreal=0,spread[42003];
int maxl,maxid;
void change()
{
	for(int i=0;i<=lenreal-1;++i)
	{
		if(real[i]>=97&&real[i]<=122)
			ss[lenss++]=real[i],Map[lenss-1]=i;
		else if(real[i]>=65&&real[i]<=90)
			ss[lenss++]=real[i]+32,Map[lenss-1]=i;	
	}
	ss[lenss]='\0';
}
void addChar()
{
	for(int i=0;i<=lenss-1;++i)
	{
		s[2*i+1]=ss[i];
		s[2*i]='#';
	}
	s[2*lenss+1]='\0';
	s[2*lenss]='#';
	lens=2*lenss+1;
}
inline int mapss(int pos)
{
	return (pos-1)>>1;
}
void Manacher()
{
	int id=0,rbound=0;
	maxid=0;maxl=0;
	for(int i=0;i<=lens-1;++i)
	{
		if(i<rbound)
		{
			int j=2*id-i;
			if(spread[j]<rbound-i)
			{
				spread[i]=spread[j];
			}
			else spread[i]=rbound-i;
		}//else spread[i]=0
		
		while
		( i-spread[i]-1>=0
		&&i+spread[i]+1<=lens-1
		&&s[i+spread[i]+1]==s[i-spread[i]-1])
		{
			++spread[i];
		}
		if(i+spread[i]>rbound)
			rbound=i+spread[i],id=i;
		if(spread[i]>maxl)
			maxl=spread[i],maxid=i;
	}
}
//#undef DEBUG
int main()
{
	freopen("1210.in","r",stdin);
	//freopen("1210.out","w",stdout);
	while(scanf("%c",&real[lenreal])!=EOF)
		++lenreal;
	lenreal='\0';
	lenreal=strlen(real);
	change();
	addChar();
	Manacher();
	printf("%d\n",mapss(maxid+maxl-1)-mapss(maxid-maxl+1)+1);
	int l=Map[mapss(maxid-maxl+1)],
		r=Map[mapss(maxid+maxl-1)];
	for(int i=l;i<=r;++i)
	{
		ans[i-l]=real[i];
	}
	ans[r-l+1]='\0';
	printf("%s\n",ans);
	return 0;
}

推荐

kmp算法

kmp算法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
kmp
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
using namespace std;
int nxt[1003],len1,len2;
char s1[1000003],s2[1003];
void initNxt()
{
	nxt[0]=-1;
	int j=-1;
	for(int i=1;i<=len2-1;++i)
	{
		while(j!=-1&&s2[i]!=s2[j+1])
			j=nxt[j];
		if(s2[i]==s2[j+1])++j;
			nxt[i]=j;
	}
}
void findApr()
{
	int i,j;
	for(i=0,j=-1;i<=len1-1;++i)
	{
		while(j!=-1&&s1[i]!=s2[j+1])
			j=nxt[j];
		if(s1[i]==s2[j+1])++j;//find the same
		if(j==len2-1)
		{
			printf("%d\n",i-j+1);
		}
	}
}
int main()
{
	freopen("kmp.in","r",stdin);
	//freopen("kmp.out","w",stdout);
	scanf("%s %s",s1,s2);
	len1=strlen(s1),len2=strlen(s2);
	initNxt();
	findApr();
	for(int i=0;i<=len2-1;++i)
	{
		printf("%d ",nxt[i]+1);
	}
	printf("\n");
	return 0;
}

字符串哈希

字符串哈希

例题:luogu3370

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include<cstring>
#include<cstdios>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10005;
typedef unsigned long long ULL;
char s[maxn];
ULL Base=62;
struct Hash
{
    inline Hash(ULL M,ULL d):Mod(M),low(d){}
    ULL Mod;
    ULL low;
    ULL pre[maxn],//pre[i] store hash of s.substr(1,i)
        Pow[maxn];//Pow[i] store 26^i
    inline ULL code()
    {
        return (pre[strlen(s)-1]-pre[0]*Pow[strlen(s)-1]%Mod+Mod)%Mod;
    }
    void init()
    {
        memset(pre,0,sizeof(pre));
        memset(Pow,0,sizeof(Pow));
        Pow[0]=1;
        pre[0]=0;
        for(int i=1; i<strlen(s); ++i)
        {
            Pow[i]=Pow[i-1]*Base;
        }
        for(int i=1; i<strlen(s); ++i)
        {
            pre[i]=(pre[i-1]*Base%Mod+s[i]-low)%Mod;
        }
    }
}h1(1e9+7,40),h2(1e9+9,47),h3(1e9+829,45);
struct str
{
    ULL c1;
    str()
    {
        c1=0; 
    } 
    str(ULL e1):c1(e1){}
}q[10050];
inline bool operator==(str a,str b)
{
    return a.c1==b.c1;
}
inline bool cmp(str f1,str f2)
{
    /*
    if(f1.c1!=f2.c1)
    {
        return f1.c1<f2.c1;
    }
    else 
    {
        if(f1.c2!=f2.c2)
        {
            return f1.c2<f2.c2;
        }
        else
            return f1.c3<f2.c3;
    }
    */
    return f1.c1<f2.c1;
}
int n;
int cnt=0;
int main()
{
    //freopen("x.txt","r",stdin);
    scanf("%d",&n);

    for(int i=1; i<=n; ++i)
    {
        memset(s,0,sizeof(s));
        s[0]='#';
        scanf("%s",s+1);
        //cout<<'\t'<<s<<endl;
        h3.init();
        q[i]=str(h3.code());
        ++cnt;
    }
    sort(q+1,q+n+1,cmp);
    for(int i=1;i<n;++i)
    {
        if(q[i]==q[i+1])--cnt;
    }
    printf("%d\n",cnt);
    return 0;
}
0%