fastle
垆边人似月 皓腕凝霜雪

/**
首先通过SAM求出每个串本质不同的子串
然后发现转移不好处理整体的本质不同

形如串A可能状态有a,b,ab,空,串B可能状态有b,空两种, 那么我们需要处理ab + 空 和 a + b的情况
为了避免这种情况,我们强行假定 假若A的某个状态x存在字符b的trans, 那么他就不能从下一个自动机中开头为b的状态转移
实现这个可以通过预处理下个串每种字符开头的dp值或者采用所有无法匹配的叶子向之后的sam的root连边之后求DaG路径数的方式来统计答案



*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<iostream>
#define ll long long
#define M 2000100
#define mmp make_pair
using namespace std;
const int mod = 1000000007;
void add(int &x, int y) {
	x += y;
	x -= x >= mod ? mod : 0;
}
int read() {
	int nm = 0, f = 1;
	char c = getchar();
	for(; !isdigit(c); c = getchar()) if(c == '-') f = -1;
	for(; isdigit(c); c = getchar()) nm = nm * 10 + c - '0';
	return nm * f;
}
int f[M], du[M], ch[M][26], fa[M], len[M], lst, cnt, rt[M], root;
int n;
char s[M];
void insert(int c) {
	int p = ++cnt, f = lst;
	lst = p;
	len[p] = len[f] + 1;
	while(f && !ch[f][c]) ch[f][c] = p, f = fa[f];
	if(f == 0)
		fa[p] = root;
	else {
		int q = ch[f][c];
		if(len[q] == len[f] + 1)
			fa[p] = q;
		else {
			int nq = ++cnt;
			memcpy(ch[nq], ch[q], sizeof(ch[nq]));
			fa[nq] = fa[q];
			len[nq] = len[f] + 1;
			fa[p] = fa[q] = nq;
			while(f && ch[f][c] == q) ch[f][c] = nq, f = fa[f];
		}
	}
}
int main() {
	n = read();
	for(int i = 1; i <= n; i++) {
		scanf("%s", s + 1);
		int l = strlen(s + 1);
		rt[i] = root = lst = ++cnt;
		for(int j = 1; j <= l; j++) insert(s[j] - 'a');
	}
	for(int i = n - 1; i >= 1; i--) {
		for(int j = rt[i]; j <= rt[i + 1]; j++) {
			for(int c = 0; c < 26; c++) {
				if(!ch[j][c]) ch[j][c] = ch[rt[i + 1]][c];
			}
		}
	}
	for(int i = 1; i <= cnt; i++) {
		for(int c = 0; c < 26; c++) {
			du[ch[i][c]]++;
		}
	}
	queue<int> q;
	int ans = 0;
	f[1] = 1;
	for(int i = 1; i <= cnt; i++) if(!du[i]) q.push(i);
	while(!q.empty()) {
		int now = q.front();
		q.pop();
		add(ans, f[now]);
		for(int c = 0; c < 26; c++) {
			if(ch[now][c]) {
				add(f[ch[now][c]], f[now]);
				du[ch[now][c]]--;
				if(du[ch[now][c]] == 0) q.push(ch[now][c]);
			}
		}
	}
	cout << ans << "\n";
	return 0;
}
posted on 2019-04-11 18:54  fastle  阅读(190)  评论(0)    收藏  举报