bzoj-2754[SCOI2012]喵星球上的点名——后缀自动机

其他人写的SAM题解我都看不懂,而且广义SAM的构建都是错误的构建方法,只能自己写一篇构建广义SAM正确,但是又比较暴力可以过的题解了。
题解:
第一问很简单,求询问的子串在多少个姓名中出现过,只要每次加进一个字符进去的时候,我们都对该字符的及其祖先进行标记此姓名出现过即可。
第二个问题的话,其实在标记第一问的同时可以用一个vector去标记该节点出现过那些姓名就行了。(当然又更优的解法,这个方法空间开销太大了)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<ctime>
#include<algorithm>
#include<sstream>
#include<bitset>
#define scand(a) scanf("%d",&a)
#define scandd(a,b) scanf("%d%d",&a,&b)
#define scanddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x&-x)
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fLL;
const int maxn=2e5+5;
const ll mod=2012;
map<int,int>ch[maxn];
int fa[maxn],len[maxn],tot=1,last=1;
//int c[maxn],cnt[maxn];
int nxt[maxn];
int n,m;
int sum[maxn];
vector<int>vis[maxn];
void extend(int c)
{
	if(ch[last].find(c)!=ch[last].end())
	{
        int p=last,x=ch[p][c];
        if(len[p]+1==len[x])last=x;
        else{
            int y=++tot;
			len[y]=len[p]+1;
			ch[y]=ch[x];
			nxt[y]=nxt[x];//广义sam记录节点的话这里记得赋值
			sum[y]=sum[x];//
			vis[y]=vis[x];//
            //memcpy(ch[y],ch[x],sizeof(ch[x]));
            while(p&&ch[p][c]==x)ch[p][c]=y,p=fa[p];
            fa[y]=fa[x];
			last=fa[x]=y;
        }
        return;
    }
	int now=++tot,pre=last;
	last=now,len[now]=len[pre]+1;
	while(pre&&ch[pre].find(c)==ch[pre].end())
	{
		ch[pre][c]=now;
		pre=fa[pre];
	}
	if(!pre)fa[now]=1;
	else
	{
		int x=ch[pre][c];
		if(len[x]==len[pre]+1)fa[now]=x;
		else
		{
			int y=++tot;
			ch[y]=ch[x];
			nxt[y]=nxt[x];//同上
			sum[y]=sum[x];
			vis[y]=vis[x];
			//memcpy(ch[y],ch[x],sizeof(ch[x]));
			len[y]=len[pre]+1;
			fa[y]=fa[x];
			fa[x]=fa[now]=y;
			while(pre&&ch[pre][c]==x)
			{
				ch[pre][c]=y;
				pre=fa[pre];
			}
		}
	}
}
//void sort()
//{
//	for(int i=0;i<=tot;i++)cnt[i]=c[i]=0;
//	for(int i=1;i<=tot;i++)cnt[len[i]]++;
//	for(int i=1;i<=tot;i++)cnt[i]+=cnt[i-1];
//	for(int i=1;i<=tot;i++)c[cnt[len[i]]--]=i;
//	for(int i=tot;i>=1;i--)sum[fa[c[i]]]+=sum[c[i]];
//}
//void init()
//{
//	tot=last=1;
//	mst(ch,0);
//	mst(len,0);
//	mst(sum,0);
//}
void inp(int now)
{
	int len;
	scand(len);
	for(int j=0;j<len;j++)
	{
		int t;
		scand(t);
		extend(t);
		int p=last;
		while(p)
		{
			if(nxt[p]!=now)
			{
				sum[p]++;
				nxt[p]=now;
				vis[p].push_back(now);
			}
			p=fa[p];
		}
	}
}
int ans[maxn];
int main()
{
    #ifdef local
    freopen("1.txt","r",stdin);
    freopen("2.txt","w",stdout);
    #endif
    scandd(n,m);
    for(int i=1;i<=n;i++)
    {
		last=1;
		inp(i);
		last=1;
		inp(i);
	}
	for(int i=1;i<=m;i++)
	{
		int len;
		scand(len);
		int p=1;
		for(int j=1;j<=len;j++)
		{
			int t;
			scand(t);
			p=ch[p][t];
		}
		printf("%d\n",sum[p]);
		for(int j=0;j<vis[p].size();j++)//对该节点存在的姓名计数就行
		{
			int v=vis[p][j];
			ans[v]++;
		}
	}
	for(int i=1;i<=n;i++)printf("%d ",ans[i]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值