自己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. js之DOM间接操作

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Leetcode 80.删除重复数组的重复项

    删除重复数组的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间 ...

  3. empty array & Array.from

    empty array bug const duplicationArray = (arr = [], times = 2, debug = false) => { let result = [ ...

  4. poj 2823单调队列模板题

    #include<stdio.h>//每次要吧生命值长的加入,吧生命用光的舍弃 #define N  1100000 int getmin[N],getmax[N],num[N],n,k, ...

  5. codeforces gym 100357 I (费用流)

    题目大意 给出一个或与表达式,每个正变量和反变量最多出现一次,询问是否存在一种方案使得每个或式中有且仅有一个变量的值为1. 解题分析 将每个变量拆成三个点x,y,z. y表示对应的正变量,z表示对应的 ...

  6. PatentTips - Hamming distance comparison

    BACKGROUND INFORMATION In a typical data processing environment, data may be transmitted in multiple ...

  7. Ubuntu查看隐藏文件夹的方法

    比如要查看当前用户目录下的隐藏文件夹 进入/home/jim目录,使用快捷键Ctrl+H,即可显示隐藏文件夹,如果要关闭,再次按Ctrl+H即可. GUI操作如下所示: 进入文件夹,左上角->查 ...

  8. zoj 月赛

    Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game call ...

  9. Unity游戏小地图生成

    孙广东  2015.6.25 这个在AssetStore上有几个不错的插件, 除了知道原理,能自己实现还是好的. 非常多插件是不会直接使用的.而是要依据自己项目的需求进行改动或者就是自己写. 那么我们 ...

  10. mysql20170410练习代码+笔记

    今天的几道高级sql查询真的挺难的,感觉好像视频里讲过,当时也没有练,已经淡化了很多,sql还是要多练习啊!确实逻辑性挺强的. SELECT studentResult,studentNO FROM ...