Home avatar

白衣苍狗

字符串查找树(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%