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. 对spring默认的单列模式的理解

    我想大部分朋友对spring的单例模式都比較了解. 什么叫单例模式呢,顾名思义就是无论有多少个请求,都仅仅生成一个实例. 比方在spring中a,b请求都在调用同样的bean ,他们都是调用的同一个b ...

  2. xml里面product的值在哪里设置的?

    转载请注明出处:http://blog.csdn.net/droyon/article/details/39890469 在进行Android应用程序开发时,善于运用xml中的product配置,能够 ...

  3. SpringBoot结合MongoDB入门

    MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系 ...

  4. BZOJ 3236 莫队+树状数组

    思路: 莫队+树状数组 (据说此题卡常数) yzy写了一天(偷笑) 复杂度有点儿爆炸 O(msqrt(n)logn) //By SiriusRen #include <cmath> #in ...

  5. 通过不修改arc.archives.class.php而保留原有样式调上一篇下

    今天织梦58站长分享一个织梦内容页上下篇的调用方法,估计在大家在仿站的过程中,经常会碰到原站的上下篇很美观,比如如图类似的这种:或,但是用织梦原有调用上下篇的标签来调用,总达不到我们想要的效果,找不到 ...

  6. du---是对文件和目录磁盘使用的空间查看

    du命令也是查看使用空间的,但是与df命令不同的是Linux du命令是对文件和目录磁盘使用的空间的查看,还是和df命令有一些区别的. 语法 du [选项][文件] 选项 -a或-all 显示目录中个 ...

  7. Windows Server 2016 辅助域控制器搭建

    Windows Server 2016 主域控制器搭建完成后,继续进行辅助域控制器搭建.1.更改服务器的IP地址2.修改服务器的名称3.打开服务器管理器,选择添加角色和功能4.选择,下一步5.选择,下 ...

  8. 洛谷 P2913 [USACO08OCT]车轮旋转Wheel Rotation

    P2913 [USACO08OCT]车轮旋转Wheel Rotation 题目描述 Farmer John has an old-time thresher (wheat harvester) tha ...

  9. 解决WIN7远程登录提示无法保存凭据的问题

    事由:今天到公司,想起手上还有点小东西没有改动完,就打算连上server进行小改动.结果发现昨天还能好好的远程连接server的WIN7,今天突然间不能远程登录了~ 无奈~悲催~ 我仅仅能猜是不是有什 ...

  10. IOS开发人员经常使用的10个Xcode插件

    IOS开发人员经常使用的10个Xcode插件 申请达人,去除赞助商链接 一个合适的插件意味着它能够适应不同的开发环境,Sublime Text 和TextMate就是非常好的样例.你知道Xcode也支 ...