自己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();
}

【BZOJ4566_洛谷3181】[HAOI2016]找相同字符(SAM)的更多相关文章

  1. [洛谷P3181][HAOI2016]找相同字符

    题目大意:给你两个字符串,求从两个字符串中各选择一个字串使得这两个字串相同的方案数. 题解:建广义$SAM$,对每个点求出在第一个串中出现次数和第二个串中出现次数,乘起来就行了 卡点:无 C++ Co ...

  2. BZOJ4566:[HAOI2016]找相同字符(SAM)

    Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同. Input 两行,两个字符串s1,s2,长度分别 ...

  3. BZOJ 4566: [Haoi2016]找相同字符 [后缀自动机]

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 275  Solved: 155[Submit][Statu ...

  4. bzoj4566 / P3181 [HAOI2016]找相同字符

    P3181 [HAOI2016]找相同字符 后缀自动机 (正解应是广义后缀自动机) 并不会广义后缀自动机. 然鹅可以用普通的后缀自动机.   我们先引入一个问题:算出从一个串内取任意两个不重合子串完全 ...

  5. 洛谷 1938 [USACO09NOV]找工就业Job Hunt

    洛谷 1938  [USACO09NOV]找工就业Job Hunt 题目描述 Bessie is running out of money and is searching for jobs. Far ...

  6. 【BZOJ4566】[HAOI2016]找相同字符

    [BZOJ4566][HAOI2016]找相同字符 题面 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. 其中\(1\le ...

  7. [BZOJ4566][Haoi2016]找相同字符 后缀自动机+dp

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1212  Solved: 694[Submit][Stat ...

  8. 【BZOJ4566】[Haoi2016]找相同字符 后缀数组+单调栈

    [BZOJ4566][Haoi2016]找相同字符 Description 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两 个子串中有一个位置不同 ...

  9. bzoj 4566 [Haoi2016]找相同字符SA

    4566: [Haoi2016]找相同字符 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 128  Solved: 75[Submit][Status ...

随机推荐

  1. ubuntu tomcat自启动

    1 编写启动文件/etc/init.d/tomcat7 文件内容如下:

  2. 编写函数,第一个参数指定今天是星期几(1 ~ 7),第二个参数指定天数n,返回n天后是星期几

    def week(today, n): s = n % 7 + today return "n天后是星期:{}".format(s) print(week(1, 3))

  3. Java基础学习总结(87)——坚持写Java等技术类博客的好处

    1.加深对技术点的理解 每天写博客,可以加深对技术点的理解,假如工作中,对某个技术点运用的不熟,当你通过博客的形式写出来,这个过程中,遇到不懂的知识点,你就会查阅相关的资料,弄明白他. 2.自己日后用 ...

  4. noip模拟赛 算

    [问题背景]zhx 帮他妹子做数学题.[问题描述]求: 如 N=3, M=3, 这个值为 1^1+1^2+1^3+2^1+2^2+2^3+3^1+3^2+3^3=56. [输入格式]仅一行, 包含两个 ...

  5. mappingLocations、mappingDirectoryLocations与mappingJarLocations 区别 (转)

    mappingLocations.mappingDirectoryLocations与mappingJarLocations 区别 由于spring对hibernate配置文件hibernate.cf ...

  6. 项目中应用到的框架和技术之二——ol3-ext

    ol3-ext有很多很丰富的效果,可以不用重复造轮子,ol3-ext示例大全:http://viglino.github.io/ol3-ext/ 在本次项目中使用到了ol3-ext的两个功能:图层管理 ...

  7. 1.7-BGP④

    注意:默认路由ip route 0.0.0.0 0.0.0.0 12.1.1.1是不可以作为BGP邻居TCP始发连接的(但回包可以) 要配静态路由:ip route 13.1.1.3 255.255. ...

  8. Bitmap工具类BitmapHelper

    BitmapHelper 提供一些获取本地缩略图,获取网络图片.dp与px的相互转换等方法. import java.io.ByteArrayInputStream; import java.io.B ...

  9. C#文件运行类的VB.NET版本号

    主要差别在于事件处理要採用AddHandler和RemoveHandler,以及AddressOf三个keyword,其他基本一样. VB的操作稍微繁琐.但仍然能够实现.

  10. kettle_删除“共享输出表”引发的错误

    原创作品.出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...