bzoj3756: Pty的字符串
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define maxn 800005
#define maxm 1600005
#define maxl 1600005
#define ll long long
using namespace std;
int n,cnt,m,last,root,tot,val[maxl],now[maxn],son[maxl],prep[maxl],ri[maxm],fa[maxm],dist[maxm],tmp[maxm];
ll sum[maxm],ans;
char st[maxn*];
struct Tsegment{
int son[maxm][];
void prepare(){tot=last=root=,memset(ri,,sizeof(ri)),memset(son,,sizeof(son)),memset(dist,,sizeof(dist));}
int newnode(int x){
dist[++tot]=x; return tot;
}
int add(int x,int p){
int q=son[p][x];
if (q==){
int np=newnode(dist[p]+); last=np; ri[np]=;
for (;p&&!son[p][x];p=fa[p]) son[p][x]=np;
if (p==) fa[np]=root;
else{
int q=son[p][x];
if (dist[p]+==dist[q]) fa[np]=q;
else{
int nq=newnode(dist[p]+);
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q],fa[q]=fa[np]=nq;
for (;p&&son[p][x]==q;p=fa[p]) son[p][x]=nq;
}
}
}else{
if (dist[p]+==dist[q]) last=q,ri[q]++;
else{
int nq=newnode(dist[p]+); last=nq; ri[nq]=;
memcpy(son[nq],son[q],sizeof(son[q]));
fa[nq]=fa[q],fa[q]=nq;
for (;p&&son[p][x]==q;p=fa[p]) son[p][x]=nq;
}
}
return last;
}
void Tsort(){
memset(sum,,sizeof(sum));
for (int i=;i<=tot;i++) sum[dist[i]]++;
for (int i=;i<=n;i++) sum[i]+=sum[i-];
for (int i=;i<=tot;i++) tmp[sum[dist[i]]--]=i;
memset(sum,,sizeof(sum));
for (int x,i=tot;i>=;i--){
x=tmp[i];
if (fa[x]) ri[fa[x]]+=ri[x];
}
ri[root]=,dist[root]=dist[]=;
for (int x,i=;i<=tot;i++){
x=tmp[i];
sum[x]=ri[x]*(dist[x]-dist[fa[x]]);
if (fa[x]) sum[x]+=sum[fa[x]];
}
sum[root]=;
}
void work(){
int len=; last=root; ans=;
for (int x,i=;i<=m;i++){
x=st[i]-'a';
if (son[last][x]) len++,last=son[last][x];
else{
for (;last&&!son[last][x];last=fa[last]);
if (last==) last=root,len=;
else{
len=dist[last]+,last=son[last][x];
}
}
if (last!=root&&last){
ans+=(sum[fa[last]]+(len-dist[fa[last]])*ri[last]);
}
}
printf("%lld\n",ans);
}
}SAM;
struct Graph{
void add(int x,int y,int z){
cnt++,prep[cnt]=now[x],now[x]=cnt,son[cnt]=y,val[cnt]=z;
}
void dfs(int x,int goal){
for (int i=now[x],so=son[i];i;i=prep[i],so=son[i]){
int p=SAM.add(val[i],goal);
dfs(so,p);
}
}
}G;
int main(){
scanf("%d",&n);
cnt=ans=,memset(now,,sizeof(now));
for (int i=,u,v;i<=n;i++){
scanf("%d%s",&u,st+);
G.add(u,i,st[]-'a');
}
SAM.prepare();
G.dfs(,);
SAM.Tsort();
scanf("%s",st+),m=strlen(st+);
SAM.work();
return ;
}
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3756
题目大意:

bzoj3756: Pty的字符串的更多相关文章
- 【BZOJ3756】Pty的字符串(广义后缀自动机)
题意: 思路:论文题 建立Trie树的后缀自动机需要换这个长的板子 #include<bits/stdc++.h> using namespace std; typedef long lo ...
- Pty的字符串(string)
题目描述 在神秘的东方有一棵奇葩的树,它有一个固定的根节点(编号为1).树的每条边上都是一个字符,字符为a,b,c中的一个,你可以从树上的任意一个点出发,然后沿着远离根的边往下行走,在任意一个节点停止 ...
- [JZOJ3402] 【GDOI2014模拟】Pty的字符串
题目 给你一棵每条边从父亲指向儿子的树,每条边上面有一个字母. 从树上的任意一点出发,走出的路径就是对应一个子串. (这不是\(Trie\),因为每个父亲可能会连出字母相同的边) 再给你一个字符串\( ...
- [BZOJ 2894]世界线
传送门 \(\color{green}{solution}\) 在开这道题之前建议先看看3756:pty的字符串,然后你会发现这题就很zz了. 当然,作为一名合格的博主,我还是应该写点什么的 首先,我 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- (从终端看linux-1)linux tty pty pts 概念 区别
基本概念: 1> tty(终端设备的统称):tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东 ...
- Linux中tty、pty、pts的概念区别
转自Linux中tty.pty.pts的概念区别 基本概念: 1> tty(终端设备的统称): tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过 ...
- Linux中tty、pty、pts的概念区别 转载
基本概念: > tty(终端设备的统称): tty一词源于Teletypes,或teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东西 ...
- 字符串的朴素模式和KMP模式匹配
先复习一下字符串指针: #include <iostream> #include <string.h> using namespace std; int main() { ch ...
随机推荐
- (一)GATT Profile和GAP 简介(目前所有的BLE应用都基于GATT,所以也要了解是怎么一回事)-转发
个人大总结:(先后顺序) 1.GAP协议定义多个角色(其中就有中心设备[GATT客户端](唯一)叫主设备||和外围设备[GATT服务端端](多个)也叫从设备). 2.先经过GAP协议,再有GATT协议 ...
- list使用例子(转)
例子: 在vs2010中创建一个winform的解决方案,然后定义一个类Person,Person.cs 的代码如下: using System;using System.Collections.Ge ...
- 异常检测算法--Isolation Forest
南大周志华老师在2010年提出一个异常检测算法Isolation Forest,在工业界很实用,算法效果好,时间效率高,能有效处理高维数据和海量数据,这里对这个算法进行简要总结. iTree 提到森林 ...
- IntelliJ IDEA,代码行宽度超出限制时自动换行
转自:http://my.oschina.net/angerbaby/blog/471351 当我们使用IDE写代码时,为了保证代码的可阅读性和优雅性,通常会借助IDE的代码风格设置功能,令IDE智能 ...
- 开源:ASP.NET MVC+EF6+Bootstrap开发框架
前言 我在博客园潜水两三年了,在这里看过很多大神的文章,也学到了很多东西.可以说我是汲取着博客园的营养成长的. 想当年,我也是拿10个G的精神粮食从一个博客园大神那里换来一套开发框架,正式走上开发之路 ...
- PHP+mysql数据库开发搜索功能:中英文分词+全文检索(MySQL全文检索+中文分词(SCWS))
PHP+mysql数据库开发类似百度的搜索功能:中英文分词+全文检索 中文分词: a) robbe PHP中文分词扩展: http://www.boyunjian.com/v/softd/robb ...
- md5加密篇(一)
/// <summary> /// 获取文件的md5摘要 /// </summary> /// <param name="sFile">文件流& ...
- P和NP问题
1. 通俗详细地讲解什么是P和NP问题 http://blog.sciencenet.cn/blog-327757-531546.html NP----非定常多项式(英语:non-determin ...
- BroadcastReceiver之应用卸载和安装监听
首先创建一个类继承BroadcastReceiver,然后配置Manifest.xml <receiver android:name=".PackageAddRemove"& ...
- WEB 文件上传
关键:<input name="file" type="file"/> 然后,在外面<form>层中必须写上:enctype=" ...