题意:给定两个字符串,从中各取一个子串使之相同,有多少种取法。允许本质相同。

解:建立广义后缀自动机,对于每个串,分别统计cnt,之后每个点的cnt乘起来。记得开long long

 #include <cstdio>
#include <algorithm>
#include <cstring> typedef long long LL;
const int N = ; struct Edge {
int nex, v;
}edge[N << ]; int top; int tr[N][], len[N], fail[N], cnt[N][], vis[N];
int tot = , last, turn, e[N];
char s[N], str[N]; inline void add(int x, int y) {
top++;
edge[top].v = y;
edge[top].nex = e[x];
e[x] = top;
return;
} inline int split(int p, int f) {
int Q = tr[p][f], nQ = ++tot;
len[nQ] = len[p] + ;
fail[nQ] = fail[Q];
fail[Q] = nQ;
memcpy(tr[nQ], tr[Q], sizeof(tr[Q]));
while(tr[p][f] == Q) {
tr[p][f] = nQ;
p = fail[p];
}
return nQ;
} inline int insert(int p, char c) {
int f = c - 'a';
if(tr[p][f]) {
int Q = tr[p][f];
if(len[Q] == len[p] + ) {
cnt[Q][turn] = ;
return Q;
}
int t = split(p, f);
cnt[t][turn] = ;
return t;
}
int np = ++tot;
len[np] = len[p] + ;
cnt[np][turn] = ;
while(p && !tr[p][f]) {
tr[p][f] = np;
p = fail[p];
}
if(!p) {
fail[np] = ;
}
else {
int Q = tr[p][f];
if(len[Q] == len[p] + ) {
fail[np] = Q;
}
else {
fail[np] = split(p, f);
}
}
return np;
} void DFS(int x) {
for(int i = e[x]; i; i = edge[i].nex) {
int y = edge[i].v;
DFS(y);
cnt[x][] += cnt[y][];
cnt[x][] += cnt[y][];
}
return;
} int main() {
scanf("%s%s", s, str);
int n = strlen(s), last = ;
for(int i = ; i < n; i++) {
last = insert(last, s[i]);
}
n = strlen(str);
last = turn = ;
for(int i = ; i < n; i++) {
last = insert(last, str[i]);
}
for(int i = ; i <= tot; i++) {
add(fail[i], i);
}
DFS();
int p = ;
LL ans = ;
for(int i = ; i <= tot; i++) {
ans += 1ll * cnt[i][] * cnt[i][] * (len[i] - len[fail[i]]);
} printf("%lld", ans);
return ;
}

AC代码

最新文章

  1. 简洁的java代码
  2. SparkContext的初始化(季篇)——测量系统、ContextCleaner及环境更新
  3. spring 缓存(spring自带Cache)(入门)源码解读
  4. Printf()输出格式控制(转)
  5. MongoDB学习笔记四:索引
  6. Redis PHP通用类
  7. [POJ] 3277 .City Horizon(离散+线段树)
  8. nginx配置文件特殊字符说明
  9. Spring MVC中Ajax实现二级联动
  10. 【转】伟大的RAC和MVVM入门(一)
  11. border-radius实例1
  12. Select XML Nodes by Name [C#]
  13. 【samba】samba 用户权限配置(转)
  14. VisualStudio移动开发(C#、VB.NET)Smobiler开发平台——BarcodeView控件的使用方式,.Net移动开发
  15. CSS Grid基于网格的二维布局系统(详细教程)
  16. sql语句表连接删除
  17. oracle DML语句 事务的定义与特点
  18. SpringBoot 学习教程(二):示例
  19. Entity framework 绑定到Datagridview的添加删除修改
  20. redis缓存雪崩、穿透、击穿概念及解决办法

热门文章

  1. 4面向对象(OOP)
  2. Django数据库操作中You are trying to add a non-nullable field &#39;name&#39; to contact without a default错误处理
  3. Appium之编写H5应用测试脚本(切换到Webview)
  4. linux mysql -- ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid)
  5. Lodop背景图无图片时显示放大叉号问题
  6. 灰度图Matlab
  7. Vasya and a Tree CodeForces - 1076E(线段树+dfs)
  8. codeforces570C
  9. 51-node-1649齐头并进(最短路)
  10. gym-10135I