Boring counting HDU - 3518 后缀自动机
题意:
对于给出的字符串S, 长度不超过1000,
求其中本质不同的子串的数量, 这些子串满足在字符串S中出现了至少不重合的2次
题解:
将串放入后缀自动机中然后求出每一个节点对应的子串为后缀的子串出现的最早和最晚的位置
然后根据
- //return len[last] - len[fail[last]];//多添加一个子串所产生不同子串的个数
- 然后根据这个改一下
- if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
- #include <set>
- #include <map>
- #include <stack>
- #include <queue>
- #include <cmath>
- #include <ctime>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <cstring>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- #define pi acos(-1.0)
- #define eps 1e-9
- #define fi first
- #define se second
- #define rtl rt<<1
- #define rtr rt<<1|1
- #define bug printf("******\n")
- #define mem(a, b) memset(a,b,sizeof(a))
- #define name2str(x) #x
- #define fuck(x) cout<<#x" = "<<x<<endl
- #define sfi(a) scanf("%d", &a)
- #define sffi(a, b) scanf("%d %d", &a, &b)
- #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c)
- #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d)
- #define sfL(a) scanf("%lld", &a)
- #define sffL(a, b) scanf("%lld %lld", &a, &b)
- #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
- #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d)
- #define sfs(a) scanf("%s", a)
- #define sffs(a, b) scanf("%s %s", a, b)
- #define sfffs(a, b, c) scanf("%s %s %s", a, b, c)
- #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d)
- #define FIN freopen("../in.txt","r",stdin)
- #define gcd(a, b) __gcd(a,b)
- #define lowbit(x) x&-x
- #define IO iOS::sync_with_stdio(false)
- using namespace std;
- typedef long long LL;
- typedef unsigned long long ULL;
- const ULL seed = ;
- const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
- const int maxn = 1e4 + ;
- const int maxm = 8e6 + ;
- const int INF = 0x3f3f3f3f;
- const int mod = 1e9 + ;
- struct Suffix_Automaton {
- int last, tot, nxt[maxn << ][], fail[maxn << ];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号
- int len[maxn << ];// 最长子串的长度 (该节点子串数量 = len[x] - len[fa[x]])
- LL num[maxn << ];// 该状态子串的数量
- LL maxx[maxn << ];// 长度为x的子串出现次数最多的子串的数目
- LL sum[maxn << ];// 该节点后面所形成的自字符串的总数
- LL subnum, sublen;// subnum表示不同字符串数目,sublen表示不同字符串总长度
- int X[maxn << ], Y[maxn << ]; // Y表示排名为x的节点,X表示该长度前面还有多少个
- int L[maxn << ], R[maxn << ];//L表示对应节点代表的数组的最早出现位置,R表示最晚出现位置
- void init() {
- tot = last = ;
- fail[] = len[] = ;
- for (int i = ; i <= ; i++) nxt[][i] = ;
- }
- void extend(int c) {
- int u = ++tot, v = last;
- for (int i = ; i <= ; i++) nxt[u][i] = ;
- fail[u] = ;
- L[u] = R[u] = len[u] = len[v] + ;
- num[u] = ;
- for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u;
- if (!v) fail[u] = ;
- else if (len[nxt[v][c]] == len[v] + ) fail[u] = nxt[v][c];
- else {
- int now = ++tot, cur = nxt[v][c];
- len[now] = len[v] + ;
- memcpy(nxt[now], nxt[cur], sizeof(nxt[cur]));
- fail[now] = fail[cur];
- fail[cur] = fail[u] = now;
- L[now] = L[cur], R[now] = R[cur];
- for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now;
- }
- last = u;
- //return len[last] - len[fail[last]];
- }
- void get_sa() { // Y表示排名为x的节点,X表示该长度前面还有多少个
- for (int i = ; i <= tot; i++) X[i] = ;
- for (int i = ; i <= tot; i++) X[len[i]]++;
- for (int i = ; i <= tot; i++) X[i] += X[i - ];
- for (int i = ; i <= tot; i++) Y[X[len[i]]--] = i;
- }
- int get_L_R() {
- int ans = ;
- for (int i = tot; i; i--) {
- L[fail[Y[i]]] = min(L[fail[Y[i]]], L[Y[i]]);
- R[fail[Y[i]]] = max(R[fail[Y[i]]], R[Y[i]]);
- }
- for (int i = ; i <= tot; i++)//不相交且出现次数至少2的子串个数
- if (R[i] - L[i] > len[fail[i]]) ans += min(len[i], R[i] - L[i]) - len[fail[i]];
- return ans;
- }
- } sam;
- char s[maxn];
- int main() {
- #ifndef ONLINE_JUDGE
- FIN;
- #endif
- while (~sfs(s)) {
- if (s[] == '#') break;
- sam.init();
- int len = strlen(s);
- for (int i = ; i < len; ++i) sam.extend((s[i] - 'a'));
- sam.get_sa();
- printf("%d\n", sam.get_L_R());
- }
- #ifndef ONLINE_JUDGE
- cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl;
- #endif
- return ;
- }
Boring counting HDU - 3518 后缀自动机的更多相关文章
- Boring counting HDU - 3518 (后缀自动机)
Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...
- Boring counting HDU - 3518 (后缀数组)
Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...
- POJ 3518 (后缀自动机)
POJ 3518 Boring Problem : 给一个串S,询问串S有多个子串出现至少两次且位置不重叠. Solution : 对S串建立后缀自动机,再建立后缀树,dfs一遍统计处每个结点的子树中 ...
- HDU 5442 后缀自动机(从环字符串选定一个位置 , 时针或顺时针走一遍,希望得到字典序最大)
http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样 ...
- HDU 4436 (后缀自动机)
HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...
- HDU 4622 (后缀自动机)
HDU 4622 Reincarnation Problem : 给一个串S(n <= 2000), 有Q个询问(q <= 10000),每次询问一个区间内本质不同的串的个数. Solut ...
- HDU 4416 (后缀自动机)
HDU 4416 Good Article Good sentence Problem : 给一个串S,和一些串T,询问S中有多少个子串没有在T中出现. Solution :首先对所有的T串建立后缀自 ...
- HDU 5442 后缀自动机+kmp
题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样大,希望找到起始位置最小的,如果还相同,就默认顺时针 比赛一直因为处理最小位置出错,一结束就想 ...
- hdu 6208(后缀自动机、或者AC自动机
题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...
随机推荐
- 数据库(一)—— MySQL介绍
目录 MySQL介绍 一.MySQL版本 1.mysql主流版本 2.版本选择 二.MySQL连接与实例 1.MySQL的C/S结构 2.MySQL实例 三.mysql三层结构 1.连接层(连接上数据 ...
- 推荐几个顶级的IT技术公众号,坐稳了!
提升自我的路很多,学习是其中最为捷径的一条.丰富的知识提升的不仅仅是你的阅历,更能彰显你的气质,正如古人云:"文质彬彬是君子." 今天为大家整理了10个公众号,分别为多领域,多角度 ...
- leetcode.矩阵.240搜索二维矩阵II-Java
1. 具体题目 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵具有以下特性:每行的元素从左到右升序排列:每列的元素从上到下升序排列. 示例: 现有矩阵 ...
- 如何在vue框架中兼容IE
IE目前已经放弃了自己的独特化,正一步步迎入互联网的主流怀抱.但迫于有用户存在,还是要兼容到IE8,9, 以上. 下面聊一下如何在vue框架中兼容IE 1.首先在index.html <meta ...
- vue - blog开发学习3
1.添加less 和less-loader支持 npm install less less-loader --save-dev 2.新建main.less,将这个样式添加到home.vue中的cont ...
- 粗糙的区别prepareStatement:(为Statement的子类)与Statement
区别: prepareStatement:(为Statement的子类) conn = DBFactory.getInstance().getImpl().getConnection(); //方式一 ...
- 【牛客网-剑指offer】跳台阶
题目: 一只青蛙一次可以跳上1级台阶,也可以跳上2级.求该青蛙跳上一个n级的台阶总共有多少种跳法(先后次序不同算不同的结果). 考点: 递归和循环 思路: 1)利用二叉树,左孩子为跳一级,右孩子为跳两 ...
- Vue--入门篇
一.v-model和单选按钮(radio) <div id="app"> <input name="sex" value="男&qu ...
- LeetCode Array Easy 119. Pascal's Triangle II
Description Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's tria ...
- Shell内置命令expr