[BZOJ5137] [USACO2017 DEC]Standing Out from the Herd

Description

Just like humans, cows often appreciate feeling they are unique in some way. Since Farmer John's cows all come from the same breed and look quite similar, they want to measure uniqueness in their names.Each cow's name has some number of substrings. For example, "amy" has substrings {a, m, y, am, my, amy}, and "tommy" would have the following substrings: {t, o, m, y, to, om, mm, my, tom, omm, mmy, tomm, ommy, tommy}.A cow name has a "uniqueness factor" which is the number of substrings of that name not shared with any other cow. For example, If amy was in a herd by herself, her uniqueness factor would be 6. If tommy was in a herd by himself, his uniqueness factor would be 14. If they were in a herd together, however, amy's uniqueness factor would be 3 and tommy's would be 11.Given a herd of cows, please determine each cow's uniqueness factor.

定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数。如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 "a" "am" "amy" 共 3 个。只属于 "tommy" 的本质不同的子串为 "t" "to" "tom" "tomm" "tommy" "o" "om" "omm" "ommy" "mm" "mmy" 共 11 个。 所以 "amy" 的「独特值」为 3 ,"tommy" 的「独特值」为 11 。给定 N 个字符集为小写英文字母的字符串,所有字符串的长度和小于 10^5 ,求出每个字符串「独特值」

Input

The first line of input will contain NN (1≤N≤10^5).

The following NN lines will each contain the name of a cow in the herd.

Each name will contain only lowercase characters a-z.

The total length of all names will not exceed 10^5

Output

Output N numbers, one per line, describing the uniqueness factor of each cow.

Sample Input

3
amy
tommy
bessie

Sample Output

3
11
19

Solution

对这些串建立广义后缀自动机。

对于每个串在自动机上跑一下,然后把经过的点以及他们的\(parent\)树上的祖先全部标记上当前串的编号,如果一个点被标记了两次,那么这个点所代表的子串必然不是本质相同的,就不能算。

然后统计答案就直接枚举自动机上的每个点,如果这个点只被标记了一次就给答案统计上当前点代表的本质不同的子串个数,即\(maxl[p]-maxl[par[p]]\)

时间复杂度\(O(\sum len_i)\)

#include<bits/stdc++.h>
using namespace std;
 
void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

const int maxn = 3e5+10;

char ss[maxn],s[maxn];
int n,qs=1,cnt=1,lstp=1,tot;
int tr[maxn][27],par[maxn],ml[maxn],vis[maxn],len[maxn],ans[maxn];

void append(int x) {
	int p=lstp,np=++cnt;lstp=np,ml[np]=ml[p]+1;
	for(;p&&tr[p][x]==0;p=par[p]) tr[p][x]=np;
	if(p==0) return par[np]=qs,void();
	int q=tr[p][x];
	if(ml[p]+1<ml[q]) {
		int nq=++cnt;ml[nq]=ml[p]+1;
		memcpy(tr[nq],tr[q],sizeof tr[nq]);
		par[nq]=par[q],par[q]=par[np]=nq;
		for(;p&&tr[p][x]==q;p=par[p]) tr[p][x]=nq;
	} else par[np]=q;
}

int main() {
	read(n);
	for(int i=1;i<=n;i++) {
		scanf("%s",ss+1);len[i]=strlen(ss+1);lstp=qs;
		for(int j=1;j<=len[i];j++) s[++tot]=ss[j],append(ss[j]-'a'+1);
	}
	tot=0;
	for(int p,i=1;i<=n;i++)
		for(int now=qs,j=1;j<=len[i];j++) {
			now=tr[now][s[++tot]-'a'+1];
			for(p=now;p&&vis[p]!=-1&&vis[p]!=i;p=par[p])
				if(vis[p]!=0) vis[p]=-1;else vis[p]=i;
		}
	for(int i=1;i<=cnt;i++) if(vis[i]!=-1) ans[vis[i]]+=ml[i]-ml[par[i]];
	for(int i=1;i<=n;i++) write(ans[i]);
	return 0;
}
posted @ 2019-02-26 17:13  Hyscere  阅读(277)  评论(0编辑  收藏  举报