2 条题解

  • 1
    @ 2026-8-15 14:30:04

    SAM模板

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int ch[1000010][27],len[1000010],fa[1000010],id=1,np=1;
    void extend(int c){
    	int p=np;np=++id;
    	len[np]=len[p]+1;
    	for(;p&&!ch[p][c];p=fa[p])ch[p][c]=np;
    	if(!p)fa[np]=1;
    	else{
    		int q=ch[p][c];
    		if(len[q]==len[p]+1)fa[np]=q;
    		else{
    			int nq=++id;
    			len[nq]=len[p]+1;
    			fa[nq]=fa[q];fa[q]=nq;fa[np]=nq;
    			for(;p&&ch[p][c]==q;p=fa[p])ch[p][c]=nq;
    			memcpy(ch[nq],ch[q],sizeof(ch[q]));
    		}
    	}
    }
    int main(){
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	string s;
    	cin>>s;
    	for(char i:s)extend(i-'a');
    	ll ans=0;
    	for(int i=1;i<=id;i++)ans+=len[i]-len[fa[i]];
    	cout<<ans;
    	return 0;
    }
    
    • 0
      @ 2026-8-24 8:54:47

      sam模板链接

      董晓算法F09

      • 1

      信息

      ID
      3274
      时间
      100ms
      内存
      1024MiB
      难度
      10
      标签
      递交数
      6
      已通过
      3
      上传者