51nod1469淋漓尽致子串题解(SAM)

版权声明:转载请注明原出处啦QAQ(虽然应该也没人转载): https://blog.csdn.net/hzk_cpp/article/details/88699232

题目:51nod1469.
题目:
1.令原串为 S S .
2.设子串的长度为 l e n len ,在原串 S S 中出现的次数为 k k ,令其出现的位置为 p i p_i .
3.若 k = 1 k=1 ,则该子串不是淋漓尽致子串.
4.若存在 p i , p j ( i = ̸ j ) pi,pj(i=\not{}j) ,使得 S [ p i 1 ] = S [ p j 1 ] S[p_i-1]=S[p_j-1] ,则该子串不是淋漓尽致子串.
5.若存在 p i , p j ( i = ̸ j ) pi,pj(i=\not{}j) ,使得 S [ p i + l e n ] = S [ p j + l e n ] S[p_i+len]=S[p_j+len] ,则该子串不是淋漓尽致子串.
否则,该子串为淋漓尽致子串,求本质不同淋漓尽致子串的数量.
数据范围:
S 1 0 5 |S|\leq 10^5 .

一个个分析每一个条件在SAM上等价于什么条件.

首先条件3是Right集合大小大于 1 1 .

条件4等价于这个串是一个状态中长度最大的串,并且没有儿子的Right集合大小大于 1 1 .

条件4等价于这个串是一个状态中长度最大的串,并且它可以通过字符转移边转移到的状态Right集合大小均不超过 1 1 .

那么我们直接建立后缀自动机搞就行了,时间复杂度 O ( S Σ ) O(|S|\Sigma) .

代码如下:

#include<bits/stdc++.h> 
  using namespace std;
 
#define Abigail inline void
typedef long long LL;
 
const int N=100000,C=26;

struct automaton{
  int s[C],len,par;
}tr[N*2+9];
int cn,last,rght[N*2+9];

void Build_sam(){cn=last=1;} 

void extend(int x){
  int np=++cn,p=last;
  tr[np].len=tr[p].len+1;rght[np]=1;
  last=np;
  while (p&&!tr[p].s[x]) tr[p].s[x]=np,p=tr[p].par;
  if (!p) tr[np].par=1;
  else{
  	int q=tr[p].s[x];
  	if (tr[p].len+1==tr[q].len) tr[np].par=q;
  	else{
	  tr[++cn]=tr[q];tr[cn].len=tr[p].len+1;
	  tr[np].par=tr[q].par=cn;
	  while (p&&tr[p].s[x]==q) tr[p].s[x]=cn,p=tr[p].par;
	}
  }
}

char s[N+9];
int n,v[N+9],q[N*2+9],b[N*2+9],ans;

Abigail into(){
  scanf("%s",s+1);
  n=strlen(s+1);
}

Abigail work(){
  Build_sam();
  for (int i=1;i<=n;++i)
    extend(s[i]-'a');
  for (int i=1;i<=cn;++i) ++v[tr[i].len];
  for (int i=1;i<=n;++i) v[i]+=v[i-1];
  for (int i=cn;i>=1;--i) q[v[tr[i].len]--]=i;
  for (int i=cn;i>=2;--i) rght[tr[q[i]].par]+=rght[q[i]];
  for (int i=1;i<=cn;++i){
  	if (rght[i]<=1) b[i]=1;
  	if (rght[i]>1) b[tr[i].par]=1;
  	for (int j=0;j<C;++j)
  	  if (rght[tr[i].s[j]]>1) b[i]=1;
  }
  for (int i=1;i<=cn;++i) ans+=!b[i];
}

Abigail outo(){
  printf("%d\n",ans);
}

int main(){
  into();
  work();
  outo();
  return 0;
}

猜你喜欢

转载自blog.csdn.net/hzk_cpp/article/details/88699232