MLE0 完 Re0,真相竟是厌氧??
查看原帖
MLE0 完 Re0,真相竟是厌氧??
374452
AnteAntibe楼主2024/11/29 18:16

调试的时候红温开始乱点
然后把O2关掉就过了???

想知道为什么会厌氧

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;

struct zy {
	int ch[26];
	int len ,fa;
	zy() {
		len = fa = 0;
		memset(ch ,0 ,sizeof ch);
	}
}sam[N<<1];


struct zqz {
	int to ,nxt;
} es[N];
int hd[N] ,idx;
void link(int u ,int v) {
	es[++idx].to = v;
	es[idx].nxt = hd[u];
	hd[u] = idx;
}


int n ,tot=1 ,las=1;
int p ,np ,q ,nq;
void add(int c) {
	p = las;
	las = ++tot; np = las;
	sam[np].len = sam[p].len + 1;
	
	for (;p && !(sam[p].ch[c]) ;p = sam[p].fa) {
		sam[p].ch[c] = np;
	}
	
	if(p == 0) {
		sam[np].fa = 1;
	}
	else {
		q = sam[p].ch[c];
		if (sam[q].len == sam[p].len + 1) {
			sam[np].fa = q;
		}
		else {
			nq = ++tot;
			sam[nq] = sam[q];
			sam[nq].len = sam[p].len + 1;
			sam[np].fa = nq;
			sam[q].fa = nq;
			for(;p && sam[p].ch[c] == q ;p = sam[p].fa) {
				sam[p].ch[c] = nq;
			}
		}
	}
}

long long f[N<<1] ,inn[N<<1];
int topo() {
	queue<int> q;
	for (int i=1 ;i<=tot ;i++) {
		if(!inn[i]) {
			q.push(i);
		}
	}
	for(;q.size();) {
		int thi = q.front();
		q.pop();
		for (int i=hd[thi] ;i ;i = es[i].nxt) {
			int v = es[i].to;
			f[v] += f[thi] + 1;
			inn[v] --;
			if(inn[v] == 0) {
				q.push(v);
			}
		}
	}
}

char in[N];
int main() {
	scanf("%d %s" ,&n ,in+1);
	memset(sam ,0 ,sizeof sam);
	for (int i=1 ;i<=n ;i++) {
		add(in[i] - 'a');
	}
	for (int i=1 ;i<=tot ;i++) {
		for (int j=0 ;j<=25 ;j++) {
			if (sam[i].ch[j]) {
				inn[i]++;
				link(sam[i].ch[j] ,i);
			}
		}
	}
	topo();
	printf("%lld" ,f[1]);
	return 0;
}
2024/11/29 18:16
加载中...