3277: 串
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 309 Solved: 118
[Submit][Status][Discuss]
Description
字符串是oi界常考的问题。现在给定你n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串(注意包括本身)。
Input
第一行两个整数n,k。
接下来n行每行一个字符串。 Output
输出一行n个整数,第i个整数表示第i个字符串的答案。 Sample Input
3 1
abc
a
ab Sample Output
6 1 3 HINT
对于100%的数据,n,k,l<=100000

算法讨论:

首先对这些串建立出广义后缀自动机,同时在建立的时候要保存当前结点都是哪些串的子串,然后建立出Parent树,

对树进行一遍DFS,把一个点的所以后代结点的颜色信息全部合并到自己身上,并用一个数组来维护当前结点有多少颜色,也就是多少个串的子串。

因为我们知道,一个点在Parent树上的父亲结点是其的最长后缀,所以如果一个点有颜色Q,那么其所有祖先结点全部有颜色Q。

然后对于每个串跑自动机,如果一个当前结点的颜色数目小于K,就沿其fail指针向上跳,跳到一个大于等于K的地方,

此时答案应该加上min(这个点的len, 当前ln + 1的最小值)。 至于这个ln是做什么的,容我再想想。

代码:

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <set>
#include <string> using namespace std;
const int N = 100000 + 5;
const int C = 26;
typedef long long ll; int n, cnt, k;
int head[N << 1], color[N << 1];
string s[N];
set <int> occ[N << 1];
set <int> :: iterator it; struct State {
int pre, len, next[C];
}st[N << 1]; struct SuffixAutomaton {
int sz, last; void Init() {
sz = last = 1;
st[sz].pre = -1; st[sz].len = 0;
sz ++;
} void add(int c, int ccc) {
int cur = sz ++, p;
st[cur].len = st[last].len + 1;
for(p = last; p != -1 && !st[p].next[c]; p = st[p].pre)
st[p].next[c] = cur;
if(p == -1) st[cur].pre = 1;
else {
int q = st[p].next[c];
if(st[q].len == st[p].len + 1) st[cur].pre = q;
else {
int cle = sz ++;
st[cle].pre = st[q].pre;
st[cle].len = st[p].len + 1;
for(int i = 0; i < C; ++ i) st[cle].next[i] = st[q].next[i];
for(; p != -1 && st[p].next[c] == q; p = st[p].pre)
st[p].next[c] = cle;
st[q].pre = st[cur].pre = cle;
}
}
last = cur;
occ[cur].insert(ccc);
}
}sam; struct Edge {
int from, to, next;
}edges[N << 1]; void insert(int from, int to) {
++ cnt;
edges[cnt].from = from; edges[cnt].to = to;
edges[cnt].next = head[from]; head[from] = cnt;
} void dfs(int u) {
for(int i = head[u]; i; i = edges[i].next) {
int v = edges[i].to;
dfs(v);
if(occ[u].size() < occ[v].size())
swap(occ[u], occ[v]);
for(it = occ[v].begin(); it != occ[v].end(); ++ it)
occ[u].insert(*it);
}
color[u] = occ[u].size();
} int main() {
//freopen("stringa.in", "r", stdin);
//freopen("stringa.out", "w", stdout); int __size__ = 50 << 20;
char *__p__ = (char*)malloc (__size__) + __size__;
__asm__("movl %0, %%esp" :: "r"(__p__)); //ios :: sync_with_stdio(false);
cin >> n >> k;
sam.Init();
for(int i = 1; i <= n; ++ i) {
cin >> s[i];
int len = s[i].length();
for(int j = 0; j < len; ++ j) sam.add(s[i][j] - 'a', i);
sam.last = 1;
}
for(int i = 1; i < sam.sz; ++ i)
if(st[i].pre != -1) insert(st[i].pre, i);
dfs(1);
for(int i = 1; i <= n; ++ i) {
if(k > n) { cout << 0 << " "; continue; }
ll ans = 0;
int p = 1, ln = 0, len;
len = s[i].length();
for(int j = 0; j < len; ++ j) {
p = st[p].next[(int) s[i][j] - 'a'];
while(color[p] < k) p = st[p].pre;
ln = min(ln + 1, st[p].len);
ans += ln;
}
cout << ans << " ";
} //fclose(stdin); fclose(stdout);
return 0;
}

BZOJ 3277 串 (广义后缀自动机)的更多相关文章

  1. BZOJ 3277/3473 广义后缀自动机

    说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...

  2. BZOJ 3473: 字符串 [广义后缀自动机]

    3473: 字符串 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 354  Solved: 160[Submit][Status][Discuss] ...

  3. BZOJ3277: 串(广义后缀自动机)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1196  Solved: 478[Submit][Status][Discuss] Descripti ...

  4. BZOJ 2894: 世界线 广义后缀自动机

    Code: #include<bits/stdc++.h> #define maxn 300000 #define ll long long using namespace std; ve ...

  5. BZOJ 3277 串 & BZOJ 3473 字符串 (广义后缀自动机、时间复杂度分析、启发式合并、线段树合并、主席树)

    标签那么长是因为做法太多了... 题目链接: (bzoj 3277) https://www.lydsy.com/JudgeOnline/problem.php?id=3277 (bzoj 3473) ...

  6. bzoj 3926 转换+广义后缀自动机

    思路:重点在于叶子节点只有20个,我们把叶子节点提到根,把20个trie图插入后缀自动机,然后就是算有多少个本质不同的字串. #include<bits/stdc++.h> #define ...

  7. BZOJ 3473 字符串 ——广义后缀自动机

    这题就比较有趣了. 首先匹配一遍,然后统计子树叶子节点中包含大于等于k的节点个数(HH的项链) 然后就可以搞了. 关于合法的情况数,显然是l[i]-l[fa[i]],然后向下下传即可(YY一下). # ...

  8. bzoj 3277 串 && bzoj 3473 字符串 && bzoj 2780 [Spoj]8093 Sevenk Love Oimaster——广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

  9. bzoj 3277 & bzoj 3473,bzoj 2780 —— 广义后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3277 https://www.lydsy.com/JudgeOnline/problem.p ...

随机推荐

  1. void (*f(int, void (*)(int)))(int) 函数解析

    函数指针 今天与几个同学看到了一个函数指针定义: void (*f(int, void (*)(int)))(int) 以前在C trap pit fails里面见过,但是文章里面介绍的很详细,但是往 ...

  2. (原)python使用ctypes调用C/C++接口

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6135514.html 参考网址: https://docs.python.org/2/library/ ...

  3. Guava 15新特性介绍

    原文:http://www.javacodegeeks.com/2013/10/guava-15-new-features.html Guava 是众所周知的google出品的开源工具包,十分好用,本 ...

  4. em与px的区别 [ 转 ]

    其实,还是不大理解,为什么1em=16px:而且,还一般要在body里面,就写清楚Fone-size=62.5%,然后再让1em=10px进行使用:那么,为什么不直接在当时定义em的时候,就直接让它等 ...

  5. Android再学习-20141023-Intent-Thread

    20141023-Android再学习 Intent对象的基本概念 Intent是Android应用程序组件之一 Intent对象在Android系统中表示一种意图 Intent当中最重要的内容是ac ...

  6. 使用jquery 操作checkbox

    checkbox 的全选与全不选以及获取选择的值. 效果: <!DOCTYPE html> <html lang="en"> <head> &l ...

  7. pywin32 安装错误 ImportError: DLL load failed: 不是有效的 Win32 应用程序

    pywin32 安装错误 ImportError: DLL load failed:  不是有效的 Win32 应用程序. 发现是因为没有制定Pywin32的dll所致,我们在用Pywin32开发时, ...

  8. OR扩展

    <pre name="code" class="sql">SQL> select substr(xx.acct_no,1,5) agent_o ...

  9. HDU 4276 The Ghost Blows Light

    K - The Ghost Blows Light Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & ...

  10. xm学习笔记

    1关于静态网页的制作 html主要负责页面的结构+css页面的美观+js与用户的交互. 2html 有标签体的标签: <p></p>  <span></spa ...