Description

Just like humans, cows often appreciate feeling they are unique in some way. Since Farmer John's cow
s all come from the same breed and look quite similar, they want to measure uniqueness in their name
s.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 na
me not shared with any other cow. For example, If amy was in a herd by herself, her uniqueness facto
r 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 NN numbers, one per line, describing the uniqueness factor of each cow.

Sample Input

3
amy
tommy
bessie

Sample Output

3
11
19

解题思路:

这次略有不同,建完Parent树之后,要在一个节点中统计子串种类。

但是并不需要想Dfs序那样,只需要统计是否大于1就好了。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long lnt;
struct sant{
int tranc[];
int len;
int pre;
}s[],sts;
struct pnt{
int hd;
int wgt;
int blg;
}p[];
struct ent{
int twd;
int lst;
}e[];
int fin;
int siz;
int n,T;
int cnt;
lnt ans[];
char tmp[];
void ade(int f,int t)
{
cnt++;
e[cnt].twd=t;
e[cnt].lst=p[f].hd;
p[f].hd=cnt;
return ;
}
void res(void)
{
fin=siz=;
s[]=s[]=sts;
return ;
}
void Insert(int c,int i)
{
int nwp,lsp,nwq,lsq;
nwp=++siz;
p[nwp].blg=i;
p[nwp].wgt=;
s[nwp].len=s[fin].len+;
for(lsp=fin;lsp&&!s[lsp].tranc[c];lsp=s[lsp].pre)
s[lsp].tranc[c]=nwp;
if(!lsp)
s[nwp].pre=;
else{
lsq=s[lsp].tranc[c];
if(s[lsq].len==s[lsp].len+)
s[nwp].pre=lsq;
else{
nwq=++siz;
s[nwq]=s[lsq];
s[nwq].len=s[lsp].len+;
s[nwp].pre=s[lsq].pre=nwq;
while(s[lsp].tranc[c]==lsq)
{
s[lsp].tranc[c]=nwq;
lsp=s[lsp].pre;
}
}
}
fin=nwp;
return ;
}
void Dfs(int x)
{
int t=p[x].blg;
for(int i=p[x].hd;i;i=e[i].lst)
{
int to=e[i].twd;
Dfs(to);
if(!t)
t=p[to].blg;
else if(t!=p[to].blg)
t=-;
p[x].wgt+=p[to].wgt;
}
p[x].blg=t;
if(t<=)
return ;
ans[t]+=(lnt)s[x].len-s[s[x].pre].len;
return ;
}
int main()
{
res();
scanf("%d",&T);
for(int t=;t<=T;t++)
{
fin=;
lnt ans=;
scanf("%s",tmp+);
n=strlen(tmp+);
for(int i=;i<=n;i++)
Insert(tmp[i]-'a',t);
}
for(int i=;i<=siz;i++)
ade(s[i].pre,i);
Dfs();
for(int i=;i<=T;i++)
printf("%lld\n",ans[i]);
return ;
}

BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)的更多相关文章

  1. BZOJ5137[Usaco2017 Dec]Standing Out from the Herd

    看了半天题 不知道怎么用SAM维护 于是借(chao)鉴(xi)的一发神犇的 只要判断这个子串之前被标记的记号(也就是他属于第几个串)和这次转移到的是否相同 如果不同就说明该子串属于多个串 直接标记- ...

  2. BZOJ.5137.Standing Out from the Herd(广义后缀自动机)

    题目链接 \(Description\) 对于每个串,求在\(n\)个串中只在该串中出现过的子串的数量. \(Solution\) 建广义SAM.对每个串插入时新建的np标记其属于哪个串. 然后在pa ...

  3. 【BZOJ5137】Standing Out from the Herd(后缀自动机)

    [BZOJ5137]Standing Out from the Herd(后缀自动机) 题面 BZOJ 洛谷 题解 构建广义后缀自动机 然后对于每个节点处理一下它的集合就好了 不知道为什么,我如果按照 ...

  4. BZOJ 5137: [Usaco2017 Dec]Standing Out from the Herd(后缀自动机)

    传送门 解题思路 这个似乎和以前做过的一道题很像,只不过这个是求本质不同子串个数.肯定是先把广义\(SAM\)造出来,然后\(dfs\)时把子节点的信息合并到父节点上,看哪个只被一个串覆盖,\(ans ...

  5. BZOJ5137&&lg4081(广义后缀自动机,set启发式合并)

    BZOJ5137&&lg4081(广义后缀自动机,set启发式合并) 题面 自己找去 HINT 给定多个文本串,让你查询每个文本串中有多少个本质不同的子串且这个子串只出现在当前这个文本 ...

  6. BZOJ5142: [Usaco2017 Dec]Haybale Feast(双指针&set)(可线段树优化)

    5142: [Usaco2017 Dec]Haybale Feast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 182  Solved: 131[ ...

  7. [USACO17DEC]Standing Out from the Herd(广义后缀自动机)

    题意 定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数.如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 ...

  8. Luogu4081 USACO17DEC Standing Out from the Herd(广义后缀自动机)

    建出广义SAM,通过parent树对每个节点求出其是否仅被一个子串包含及被哪个包含. 写了无数个sam板子题一点意思都没啊 #include<bits/stdc++.h> using na ...

  9. 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd

    here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...

随机推荐

  1. 安卓自己定义对话框及The specified child already has a child问题

    问题:在android开发过程中,有时会在不同情况下遇到同种问题:The specified child already has a parent.You must call removeView() ...

  2. thinkphp使后台的字体图标显示异常

    thinkphp使后台的字体图标显示异常 相似问题 1.thinkPHP的这些图标都不显示了-CSDN论坛https://bbs.csdn.net/topics/391823415 解答: 发现在别的 ...

  3. Gallery滑动一页(一个Item)效果

    本文主要介绍如何使用Gallery只滑动一页以及其实现原理. Demo APK 可以方便的查看效果,在各大应用商店搜索 trinea android 下载即可,如:Google Play. 可运行代码 ...

  4. IBM将收购Linux发行商红帽公司,继续发力云计算市场

    10月29日凌晨消息,IBM和Red Hat当地时间星期日联合宣布,IBM将以340亿美元收购红帽公司(Red Hat).根据两家公司发表的一份联合声明,IBM将以每股190美元的价格,以现金方式收购 ...

  5. Redis 数据持久化的方案的实现

    原文:Redis 数据持久化的方案的实现 版权声明:m_nanle_xiaobudiu https://blog.csdn.net/m_nanle_xiaobudiu/article/details/ ...

  6. &lt;LeetCode OJ&gt; 20. Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. PostgreSQL递归查询实现树状结构查询

    在Postgresql的使用过程中发现了一个非常有意思的功能,就是对于须要相似于树状结构的结果能够使用递归查询实现.比方说我们经常使用的公司部门这样的数据结构.一般我们设计表结构的时候都是相似以下的S ...

  8. Yahoo!团队:网站性能优化的35条黄金守则(转)

    Excetional Performance 团队总结出了一系列可以提高网站速度的方法.可以分为 7大类 35条.包括内容 .服务器 . CSS . JavaScript .Cookie .图片 .移 ...

  9. 闲的无聊写了个很(wu)有(liao)意(dao)思(bao)的程序

    下午机房断网了 闲的无聊,写了个小游戏 忘了sleep在哪个库里了.. 自带变色效果哦 #include<iostream> #include<cstdio> #include ...

  10. mvc4 视图中的form如何获取

    public ActionResult Index(FormCollection form)         {             var Name = form["字段名" ...