自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下。

题目:

洛谷3181

分析:

SAM(可能是)模板题(不会SAM的同学戳我:【知识总结】后缀自动机的构建)。

对\(s1\)建出SAM,用\(s2\)在上面跑。用\(size[i]\)表示结点\(i\)的\(Right\)集合大小(直接拓扑排序后DP就行)。既然要求\(s2\)中有多少子串在\(s1\)中出现了,那么用\(f[i]\)表示结点\(i\)对应的所有子串在\(s2\)中一共出现了多少次(位置不同算多次)。答案就是\(\sum f[i]\cdot size[i]\)

如何计算\(f[i]\)呢?如果当前匹配过程中走到了\(p\)点,匹配长度为\(len\)。很明显,当前匹配到的是从\(p\)点沿着\(fa\)链到根的路径上对应的长度不大于\(len\)的字符串。它对\(f[p]\)有\(len-min[p]+1=len-max[fa[p]]\)的贡献(SAM上\(min[p]-1=max[fa[p]]\)),且对这条路径上任意一点\(q\)的\(f[q]\)都有\(max[q]-min[q]+1=max[q]-max[fa[q]]\)的贡献。可以发现此时只有对\(f[p]\)的贡献与\(len\)有关,对其他结点\(q\)的贡献只取决于\(q\)在\(fa\)树上的子树的访问次数之和(乘上\(max[q]-max[fa[q]]\))。所以这里只处理对\(f[p]\)的贡献,并记录一下\(p\)被访问了多少次,处理完后一起推上去就行了(类似树上差分)。时间复杂度\(O(n)\)。这里可能讲得不清楚,详见代码qwq。

代码:

还是比较好写的。别问我Auto_Suffix_Chicken是什么东西

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <string>
#undef i
#undef j
#undef k
#undef true
#undef false
#undef min
#undef max
#undef sort
#undef swap
#undef if
#undef for
#undef while
#undef printf
#undef scanf
#undef putchar
#undef getchar
#define _ 0
using namespace std; namespace zyt
{
const int N = 2e5 + 10;
template<typename T>
inline bool read(T &x)
{
bool f = false;
char c;
x = 0;
do
c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF)
return false;
if (c == '-')
f = true, c = getchar();
do
x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f)
x = -x;
return true;
}
inline bool read(string &s)
{
static char buf[N];
if (scanf("%s", buf) == -1)
return false;
s = buf;
return true;
}
template<typename T>
inline void write(T x)
{
static char buf[20];
char *pos = buf;
if (x < 0)
putchar('-'), x = -x;
do
*pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf)
putchar(*--pos);
}
const int CH = 26;
typedef long long ll;
string s1, s2;
namespace Auto_Suffix_Chicken
{
struct node
{
int size, max, fa, s[CH];
}tree[N << 1];
int len, cnt, last;
inline void init()
{
cnt = last = 1;
}
inline int ctoi(const char c)
{
return c - 'a';
}
void insert(const char c)
{
int x = ctoi(c), np = ++cnt, p = last;
tree[np].max = tree[p].max + 1;
tree[np].size = 1;
while (p && !tree[p].s[x])
tree[p].s[x] = np, p = tree[p].fa;
if (!p)
tree[np].fa = 1;
else
{
int q = tree[p].s[x];
if (tree[p].max + 1 == tree[q].max)
tree[np].fa = q;
else
{
int nq = ++cnt;
memcpy(tree[nq].s, tree[q].s, sizeof(char[CH]));
tree[nq].max = tree[p].max + 1;
tree[nq].fa = tree[q].fa;
tree[np].fa = tree[q].fa = nq;
while (p && tree[p].s[x] == q)
tree[p].s[x] = nq, p = tree[p].fa;
}
}
last = np;
}
int buf[N << 1];
void radix_sort()
{
static int count[N];
memset(count, 0, sizeof(int[len + 1]));
for (int i = 1; i <= cnt; i++)
++count[tree[i].max];
for (int i = 1; i <= len; i++)
count[i] += count[i - 1];
for (int i = cnt; i > 0; i--)
buf[count[tree[i].max]--] = i;
}
void build(const string &s)
{
init();
len = s.size();
for (int i = 0; i < s.size(); i++)
insert(s[i]);
radix_sort();
for (int i = cnt; i > 0; i--)
tree[tree[buf[i]].fa].size += tree[buf[i]].size;
}
inline ll query(const string &s)
{
static ll f[N << 1];
static int num[N << 1];
memset(f, 0, sizeof(ll[cnt + 1]));
memset(num, 0, sizeof(int[cnt + 1]));
ll ans = 0;
int now = 1, len = 0;
for (int i = 0; i < s.size(); i++)
{
int x = ctoi(s[i]);
while (now && !tree[now].s[x])
now = tree[now].fa, len = tree[now].max;
if (now)
now = tree[now].s[x], ++len;
else
now = 1, len = 0;
f[now] += (len - tree[tree[now].fa].max), ++num[now];
}
radix_sort();
for (int i = cnt; i > 0; i--)
{
int tmp = tree[buf[i]].fa;
f[tmp] += (ll)num[buf[i]] * (tree[tmp].max - tree[tree[tmp].fa].max);
num[tmp] += num[buf[i]];
}
for (int i = 2; i <= cnt; i++)
ans += f[i] * tree[i].size;
return ans;
}
}
int work()
{
using namespace Auto_Suffix_Chicken;
read(s1), read(s2);
build(s1);
write(query(s2));
return (0^_^0);
}
}
int main()
{
return zyt::work();
}

最新文章

  1. 文档分享-Activiti 5.16 用户手册
  2. HTML5系列:HTML5绘图
  3. ILGenerator.Emit动态 MSIL编程(三)之动态代理
  4. CentOS 6下Apache的https虚拟主机实践
  5. (Python)list的内建函数 filter(), map(), 和 reduce()
  6. C# 枚举绑定到ComboBox
  7. 高并发的常见策略--大型web项目
  8. sql语句中like的使用
  9. 【转】android 4.3 BLE onCharacteristicWrite没有回调
  10. iOS会议和组织
  11. nc命令 (NetCat)
  12. 使用libcurl进行文件上传
  13. C++ 观察者模式样例
  14. CSharp Oracle 登陆
  15. 大学二三事&mdash;&mdash;那些事(1)
  16. AR_Demon(使用vuforia平台提供的钥匙跟后台,实现相机拍图片读取模型以及视频的功能)
  17. jQuery的动态绑定事件的应用
  18. 老李分享: Oracle Performance Tuning Overview 翻译
  19. Spring MVC的配置与DispatcherServlet的分析
  20. 云计算之路-阿里云上:部分服务器未及时续费造成docker swarm集群故障

热门文章

  1. docke容器使用
  2. sql杂记:一些坑和数据库恢复
  3. window7 上创建定时任务来运行自动化脚本
  4. 洛谷 1937 [USACO10MAR]仓配置Barn Allocation
  5. No buffer space available错误解决方案
  6. Python学习笔记 (2)变量、常量和数据类型
  7. vim配置说明20170819
  8. 强连通图 HDU - 1269
  9. 【CV论文阅读】Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
  10. 如何将PSD批量装换为JPG如何对PSD批量减小体积