[USACO17DEC]Standing Out from the Herd
[USACO17DEC]Standing Out from the Herd
题目描述
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 (N \leq 10^5N≤105) 个字符集为小写英文字母的字符串,所有字符串的长度和小于 10^5105,求出每个字符串「独特值」。
sol
考虑广义sam
对于每一个点记录有多少串经过它。nsqrtn
然后再保利跳统计答案
#include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> #include<string> #define maxn 20005 using namespace std; int n,rt,la,cnt,f[maxn]; struct node{ int par,Max,nex[26],num,now; }s[maxn]; string ch[maxn]; void ins(int c){ int p=la,np=++cnt;la=np;s[np].Max=s[p].Max+1; for(;p&&!s[p].nex[c];p=s[p].par)s[p].nex[c]=np; if(!p)s[np].par=rt; else { int q=s[p].nex[c],nq; if(s[q].Max==s[p].Max+1)s[np].par=q; else { nq=++cnt;s[nq].Max=s[p].Max+1; for(int j=0;j<26;j++)s[nq].nex[j]=s[q].nex[j]; s[nq].par=s[q].par;s[q].par=s[np].par=nq; for(;p&&s[p].nex[c]==q;p=s[p].par)s[p].nex[c]=nq; } } } int main(){ cin>>n; rt=la=cnt=1; for(int i=1;i<=n;i++){ cin>>ch[i];int l=ch[i].length(); la=rt; for(int j=0;j<l;j++){ ins(ch[i][j]-'a'); } } for(int i=1;i<=n;i++){ int l=ch[i].length(); int k=rt; for(int j=0;j<l;j++){ k=s[k].nex[ch[i][j]-'a']; int p=k; for(;p&&s[p].now!=i;p=s[p].par)s[p].now=i,s[p].num++; } } for(int i=1;i<=cnt;i++)s[i].now=0; for(int i=1;i<=n;i++){ int l=ch[i].length(); int k=rt;long long ans=0; for(int j=0;j<l;j++){ k=s[k].nex[ch[i][j]-'a']; int p=k; for(;p&&s[p].now!=i&&s[p].num==1;p=s[p].par)s[p].now=i,ans=ans+s[p].Max-s[s[p].par].Max; } printf("%lld\n",ans); } return 0; }