[USACO17DEC]Standing Out from the Herd(广义后缀自动机)
题意
定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数。如 "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≤10^5 ) 个字符集为小写英文字母的字符串,所有字符串的长度和小于 10^5 ,求出每个字符串「独特值」。
题解
这题和喵星人的点名那题有点像啊……会了那题做这题挺简单的
先把所有串一起建一个广义SAM,然后把所有串放上去跑一遍,记录一下每一个子串属于多少个原串。然后再把所有串放上去跑一边,如果这个子串只属于一个原串,那么就把它的答案加上$l[i]-l[fa[i]]$(都是后缀自动机上的节点)
// luogu-judger-enable-o2
//minamoto
#include<cstring>
#include<cstdio>
using namespace std;
char sr[<<],z[];int C=-,Z;
inline void Ot(){fwrite(sr,,C+,stdout),C=-;}
inline void print(int x){
if(C><<)Ot();if(x<)sr[++C]=,x=-x;
while(z[++Z]=x%+,x/=);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+;
char c[N];int len[N],tot,ans[N],s[N];
int fa[N<<],ch[N<<][],l[N<<],sz[N<<],las[N<<];
int n,cnt=,last=;
void ins(int c){
int p=last,np=++cnt;last=np,l[np]=l[p]+;
for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
if(!p) fa[np]=;
else{
int q=ch[p][c];
if(l[p]+==l[q]) fa[np]=q;
else{
int nq=++cnt;l[nq]=l[p]+;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
fa[nq]=fa[q];fa[q]=fa[np]=nq;
for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
}
}
}
void update(int x,int y){
for(;x&&las[x]!=y;x=fa[x])
++sz[x],las[x]=y;
}
void query(int x,int y){
for(;x&&las[x]!=y;x=fa[x]){
if(sz[x]==) ans[y]+=l[x]-l[fa[x]];
las[x]=y;
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;++i){
scanf("%s",c+);
len[i]=strlen(c+);
last=;
for(int j=;j<=len[i];++j) s[++tot]=c[j]-'a',ins(s[tot]);
}
tot=;
for(int i=;i<=n;++i)
for(int j=,x=;j<=len[i];++j)
update(x=ch[x][s[++tot]],i);
for(int i=;i<=cnt;++i) las[i]=;
tot=;
for(int i=;i<=n;++i)
for(int j=,x=;j<=len[i];++j)
query(x=ch[x][s[++tot]],i);
for(int i=;i<=n;++i) print(ans[i]);
Ot();
return ;
}
[USACO17DEC]Standing Out from the Herd(广义后缀自动机)的更多相关文章
- BZOJ.5137.Standing Out from the Herd(广义后缀自动机)
题目链接 \(Description\) 对于每个串,求在\(n\)个串中只在该串中出现过的子串的数量. \(Solution\) 建广义SAM.对每个串插入时新建的np标记其属于哪个串. 然后在pa ...
- BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)
Description Just like humans, cows often appreciate feeling they are unique in some way. Since Farme ...
- 【BZOJ5137】Standing Out from the Herd(后缀自动机)
[BZOJ5137]Standing Out from the Herd(后缀自动机) 题面 BZOJ 洛谷 题解 构建广义后缀自动机 然后对于每个节点处理一下它的集合就好了 不知道为什么,我如果按照 ...
- 后缀自动机再复习 + [USACO17DEC] Standing Out from the Herd
here:https://oi-wiki.org/string/sam/ 下面转自 KesdiaelKen的雷蒻论坛 来个广义后缀自动机模板题 [USACO17DEC]Standing Out fro ...
- P4081 [USACO17DEC]Standing Out from the Herd P
知识点: 广义 SAM 原题面 Luogu 「扯」 随便「口胡」一下居然「过」了. 比较考验「代码能力」,第一次感觉「大模拟」没有白写((( 还有这个「符号」实在是太「上头」了. 前置知识 在线构造广 ...
- bzoj3926: [Zjoi2015]诸神眷顾的幻想乡 对[广义后缀自动机]的一些理解
先说一下对后缀自动机的理解,主要是对构造过程的理解. 构造中,我们已经得到了前L个字符的后缀自动机,现在我们要得到L+1个字符的后缀自动机,什么需要改变呢? 首先,子串$[0,L+1)$对应的状态不存 ...
- BZOJ 3926 && ZJOI 2015 诸神眷顾的幻想乡 (广义后缀自动机)
3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec Memory Limit: 512 MB Description 幽香是全幻想乡里最受人欢迎的萌妹子,这天,是幽 ...
- BZOJ 3277 串 (广义后缀自动机)
3277: 串 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 309 Solved: 118 [Submit][Status][Discuss] De ...
- BZOJ 3473: 字符串 [广义后缀自动机]
3473: 字符串 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 354 Solved: 160[Submit][Status][Discuss] ...
- BZOJ.2780.[SPOJ8093]Sevenk Love Oimaster(广义后缀自动机)
题目链接 \(Description\) 给定n个模式串,多次询问一个串在多少个模式串中出现过.(字符集为26个小写字母) \(Solution\) 对每个询问串进行匹配最终会达到一个节点,我们需要得 ...
随机推荐
- 索引(index)
#创建索引 create index index_name_pass on student(name,pass); create index index_name_id on student(name ...
- centos7 安装wxPython
*** exact error that occured. This usually means GTK+ is incorrectly installed. configure: error: ...
- 用python给图片添加文字(水印)
题目来源于:Python 练习册,每天一个小程序 第0000题 代码如下: #-*- coding:utf-8 -*- import PIL from PIL import Image from PI ...
- python解析pcap文件中的http数据包
使用scapy.scapy_http就可以方便的对pcap包中的http数据包进行解析 scapy_http可以在https://github.com/invernizzi/scapy-http下载, ...
- 使用 XML-RPC 为 C++ 应用程序启用 Web 服务
http://www.ibm.com/developerworks/cn/webservices/ws-xml-rpc/ 引言 Internet 现在的受欢迎程度越来越高,由于这个原因及其固有的优势, ...
- DOM0级与DOM2级
定义: 0级DOM 分为2个:一是在标签内写onclick事件 二是在JS写onlicke=function(){}函数 1) <input id="myButton" t ...
- orcle clob字段查询
select utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(column,2000,1)) from t
- [C++ Mind Map] class and memory
class and memory
- [C++] Variable storage space
Variable storage space
- [SoapUI] 比较两个不同环境下XML格式的Response, 结果不同时设置Test Step的执行状态为失败
import org.custommonkey.xmlunit.* def responseTP=context.expand( '${Intraday Table_TP#Response}' ) d ...