【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)
自己yy的方法yyyyyyyy着就A了,写篇博客庆祝一下。
题目:
分析:
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();
}
【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)的更多相关文章
- [洛谷P3181][HAOI2016]找相同字符
题目大意:给你两个字符串,求从两个字符串中各选择一个字串使得这两个字串相同的方案数. 题解:建广义$SAM$,对每个点求出在第一个串中出现次数和第二个串中出现次数,乘起来就行了 卡点:无 C++ Co ...
- BZOJ4566:[HAOI2016]找相同字符(SAM)
Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...
- BZOJ 4566: [Haoi2016]找相同字符 [后缀自动机]
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 275 Solved: 155[Submit][Statu ...
- bzoj4566 / P3181 [HAOI2016]找相同字符
P3181 [HAOI2016]找相同字符 后缀自动机 (正解应是广义后缀自动机) 并不会广义后缀自动机. 然鹅可以用普通的后缀自动机. 我们先引入一个问题:算出从一个串内取任意两个不重合子串完全 ...
- 洛谷 1938 [USACO09NOV]找工就业Job Hunt
洛谷 1938 [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...
- 【BZOJ4566】[HAOI2016]找相同字符
[BZOJ4566][HAOI2016]找相同字符 题面 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. 其中\(1\le ...
- [BZOJ4566][Haoi2016]找相同字符 后缀自动机+dp
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1212 Solved: 694[Submit][Stat ...
- 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈
[BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...
- bzoj 4566 [Haoi2016]找相同字符SA
4566: [Haoi2016]找相同字符 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 128 Solved: 75[Submit][Status ...
随机推荐
- Linux下常用的操作
Linux下常用的操作 文件定位 locate filename 有些版本的linux会出现 -bash: locate: command not found错误,不要慌,安装一下mlocate包就好 ...
- 嵌入式LinuxC语言程序设计基础教程
第1章 嵌入式LinxuC语言开发工具 第2章 数据 第3章 数据的输入输出 第4章 运算符和表达式 第5章 程序结构和控制语句 第6章 数组 第7章 指针 第8章 函数 第9章 用户自定义数据类型 ...
- 升级 HTTPS,价值何在?
HTTPS 实质上是一种面向安全信息通信的协议.从最终的数据解析的角度上看,HTTPS 与 HTTP 没有本质上的区别.对于接收端而言,SSL/TSL 将接收的数据包解密,将数据传给 HTTP 协议层 ...
- 【Codeforces 923A】Primal Sport
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 考虑怎么得到数字x2=N,假设是质数p的倍数 那么x1肯定在x2-p+1~x2这个范围内才行 因为p的倍数要刚好大于等于x1, 所以x1肯定是 ...
- Intellij IDEA神器居然还有这些小技巧---超级好用的
Intellij IDEA神器居然还有这些小技巧----https://my.oschina.net/samgege/blog/1808622?p=8
- mac os随笔
1.定义对象的成员变量的时候以便使用下划线开头:例如NSImage *_image; NSURL的一个函数: getResourceValue:forKey:error: Returns t ...
- Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)
在这篇文章中,我们将使用Spring4 MVC编写一个CRUD RESTful Web服务,写一个REST客户端RestTemplate来使用这些服务.我们也将利用外部客户端测试的服务. 下面将展示核 ...
- WINDOW 专家
http://www.cnblogs.com/shanyou/category/725986.html
- linux 创建 bootable iso 文件
windows制作iso文件通过ultraiso可以实现. linux下用mkisofs这个命令就可以 最简单的用法 mkisofs -o target.iso source 要制作可以启动的iso文 ...
- ObjectiveC开发教程--字符串的连接
NSString *type = @"hello"; NSString *subtype = @"good"; NSString *typesub = [NSS ...