题意:

思路:论文题

建立Trie树的后缀自动机需要换这个长的板子

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef pair<ll,int>P;
#define N 800010
#define M 8100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const int MOD=1e9+,inv2=(MOD+)/;
double eps=1e-;
int INF=1e9;
ll inf=5e13;
int dx[]={-,,,};
int dy[]={,,-,}; char s[M];
int p,np,q,nq,k,cas,n;
int pos[N]; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} struct sam
{
int cnt;
int fa[N<<],ch[N<<][];
int st[N<<],b[N<<],bl[N<<],to[N<<],size[N<<];
ll f[N<<];
sam()
{
cnt=;
} int add(int p,int x)
{
if(ch[p][x])
{
q=ch[p][x];
if(st[q]==st[p]+)
{
size[q]++;
return q;
}
else
{
st[nq=++cnt]=st[p]+; size[nq]=;
memcpy(ch[nq],ch[q],sizeof ch[q]);
//t[nq]=t[q];
fa[nq]=fa[q];
fa[q]=nq;
while(ch[p][x]==q)
{
ch[p][x]=nq;
p=fa[p];
}
return nq;
}
}
else
{
st[np=++cnt]=st[p]+; size[np]=;
while(p&&!ch[p][x])
{
ch[p][x]=np;
p=fa[p];
}
if(!p) fa[np]=;
else
{
int q=ch[p][x];
if(st[q]==st[p]+) fa[np]=q;
else
{
nq=++cnt; st[nq]=st[p]+;
memcpy(ch[nq],ch[q],sizeof ch[q]);
fa[nq]=fa[q];
fa[q]=fa[np]=nq;
while(ch[p][x]==q)
{
ch[p][x]=nq;
p=fa[p];
}
}
}
}
return np;
} void solve()
{
//printf("cnt=%d\n",cnt);
rep(i,,cnt) b[st[i]]++;
rep(i,,cnt) b[i]+=b[i-];
rep(i,,cnt) bl[b[st[i]]--]=i;
scanf("%s",s+);
int n=strlen(s+);
per(i,cnt,) size[fa[bl[i]]]+=size[bl[i]];
//rep(i,1,cnt) printf("%d\n",size[i]);
size[]=;
rep(i,,cnt)
{
int p=bl[i];
f[p]=f[fa[p]]+1ll*(st[p]-st[fa[p]])*size[p];
}
ll ans=;
p=;
int L=;
rep(i,,n)
{
int x=s[i]-'a';
if(ch[p][x]) L++,p=ch[p][x];
else
{
while(p&&!ch[p][x]) p=fa[p];
if(p) L=st[p]+,p=ch[p][x];
else L=,p=;
}
if(p!=) ans+=f[fa[p]]+1ll*(L-st[fa[p]])*size[p];
}
printf("%lld\n",ans);
} }sam; int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
n=read();
pos[]=;
rep(i,,n)
{
int x=read();
scanf("%s",s+);
pos[i]=sam.add(pos[x],s[]-'a');
}
sam.solve();
return ;
}

最新文章

  1. Util应用程序框架公共操作类(九):Lambda表达式扩展
  2. 23-React Render Element
  3. hdu 1159, LCS, dynamic programming, recursive backtrack vs iterative backtrack vs incremental, C++ 分类: hdoj 2015-07-10 04:14 112人阅读 评论(0) 收藏
  4. Oracle数据库下sde用户系统表开放权限sql语句
  5. NYOJ之茵茵的第一课
  6. yii授权
  7. js中Math.random()生成指定范围数值的随机数
  8. IP地址查询接口
  9. linux安装IPython四种方法
  10. MFC无边框窗体不响应任务栏点击问题
  11. ubuntu 解压rar
  12. SQL Server中各个系统表的作用
  13. ruby简单的基本 6
  14. Mysql存储过程历史表备份
  15. [PHP] php + phpstudy + phpstrom + xdebug + postman开启调试
  16. Java集合性能分析-疯狂Java讲义
  17. SharePonit online 列表表单定制
  18. kafka consumer重复消费问题
  19. Ubuntu 下解压tar.xz方法
  20. 浅谈vue之动态路由匹配

热门文章

  1. Git学习及使用
  2. 第1 章 mysql数据库之简单的DDL和DML sql语句
  3. Python List append()方法
  4. Mac011--DbWrench Database安装
  5. 排序算法二:归并排序(Merge sort)
  6. 高级软件工程第二次作业:随机生成N个不重复的已解答完毕的数独棋盘
  7. 标准标签库JSTL(JSP Standard Tag Library)
  8. II play with GG
  9. C#面试 笔试题 六
  10. ASE Alpha Sprint - backend scrum 6