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. FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity

    自Android3.2之后,TabActibvity被弃用(Deprecated).取而代之的是FragmentActivity.由于Fragment比Activiy更灵活.消耗的资源更小.全然可以满 ...

  2. linux下u盘检測程序

           获得U盘的插入或者拔取得信息的传统方法是在内核级执行hotplug程序.相关參数通过环境变量传递过来,再由hotplug通知其它关注hotplug的应用程序,可是效率比較低.      ...

  3. 剑指offer—java版本实现

    终于完成了全部!所有的心累这时候都觉得很值得啊!爽! https://github.com/xurui1995/Sword-pointing-to-offer

  4. Linux 服务器下多网卡的负载均衡

    Linux 服务器下多网卡负载均衡的实现   一.引言    现今几乎各行各业内部都建立了自己的服务器,由于服务器的特殊地位,它的可靠性.可用性及其 I/O 速度就显得非常的重要, 保持服务器的高可用 ...

  5. 分享vue中 slot用法

    //slot默认用法 //slot带参数name用法

  6. AtCoder Beginner Contest 067 C - Splitting Pi

    C - Splitting Pile Time limit : 2sec / Memory limit : 256MB Score : 300 points Problem Statement Snu ...

  7. Ubunut PPA源概述

    Ubuntu 自带的“软件”应用,可以安装海量软件,既包括发行者支持的软件.社区支持的软件,也包括专有驱动和版权软件.有时,我们需要的软件通过这些渠道仍然无法找到.这时,可以到 PPA 软件源中查找. ...

  8. Cisco路由器交换机配置命令详解

    1. 交换机支持的命令: 交换机基本状态:switch: :ROM状态, 路由器是rommon>hostname> :用户模式hostname# :特权模式hostname(config) ...

  9. 微信小程序官方文档中的加密算法

    用Nodejs来算一下:

  10. git pull 、git fetch、 git clone

    git clone 代表从远程克隆过来包括所有的版本信息 git fetch是从远程获取最新的版本 git pull相当于 git fetch 然后再git merge