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. 泛泰A820L (高通MSM8660 cpu) 3.4内核的CM10.1(Android 4.2.2) 測试版第二版

    欢迎关注泛泰非盈利专业第三方开发团队 VegaDevTeam  (本team 由 syhost suky zhaochengw(z大) xuefy(大星星) tenfar(R大师) loogeo cr ...

  2. Visual Studio Code Setup

    Windows https://code.visualstudio.com/docs/setup/windows Additional Components and Tools https://cod ...

  3. jFinal 关联数据库操作

    1.建数据库(我用的是oracle数据库,其他的相对也差不多) -- Create table create table CLASSES ( classesid NUMBER not null, cl ...

  4. BZOJ 1193 搜索+贪心

    预处理出100*100以内的最优解 贪心走日 判断是0*4还是2*4 搞定 //By SiriusRen #include <queue> #include <cstdio> ...

  5. Android中Gallery和ImageSwitcher同步自动(滚动)播放图片库

    本文主要内容是如何让Gallery和ImageSwitcher控件能够同步自动播放图片集 ,看起来较难,然而,实现的方法非常简单, 请跟我慢慢来.总的来说,本文要实现的效果如下图:(截图效果不怎么好) ...

  6. 重大漏洞:Bitlocker成摆设,多款固态硬盘硬件加密均可被绕过

    荷兰拉德堡德大学的两名研究人员日前发表论文,描述了固态硬盘流行加密软件Bitlocker中的关键漏洞.固态硬盘需要口令来加密和解密其上存储的内容,但该口令可以被绕过. 荷兰拉德堡德大学的两名研究人员日 ...

  7. 二进制部署mysql5.6

    二进制部署不用编译直接配置环境,初始化就可以使用了下面是官网给的方法: MySQL 二进制安装解决依赖yum install libaio shell> yum search libaio # ...

  8. Java Web学习总结(9)——servlet和Jsp生命周期解读

    一.servlet的工作工程 Servlet是运行在Servlet容器(有时候也叫Servlet引擎,是web服务器和应用程序服务器的一部分,用于在发送的请求和响应之上提供网络服务,解码基于MIME的 ...

  9. hdu 5073 Galaxy(2014 鞍山现场赛)

    Galaxy                                                                   Time Limit: 2000/1000 MS (J ...

  10. 什么是string interning(字符串驻留)以及python中字符串的intern机制

    Incomputer science, string interning is a method of storing only onecopy of each distinct string val ...