Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)
求不重复的子串个数
用所有的减去height就好了 推出来的。。。
- #include <iostream>
- #include <cstdio>
- #include <sstream>
- #include <cstring>
- #include <map>
- #include <cctype>
- #include <set>
- #include <vector>
- #include <stack>
- #include <queue>
- #include <algorithm>
- #include <cmath>
- #define rap(i, a, n) for(int i=a; i<=n; i++)
- #define rep(i, a, n) for(int i=a; i<n; i++)
- #define lap(i, a, n) for(int i=n; i>=a; i--)
- #define lep(i, a, n) for(int i=n; i>a; i--)
- #define rd(a) scanf("%d", &a)
- #define rlld(a) scanf("%lld", &a)
- #define rc(a) scanf("%c", &a)
- #define rs(a) scanf("%s", a)
- #define MOD 2018
- #define LL long long
- #define ULL unsigned long long
- #define Pair pair<int, int>
- #define mem(a, b) memset(a, b, sizeof(a))
- #define _ ios_base::sync_with_stdio(0),cin.tie(0)
- //freopen("1.txt", "r", stdin);
- using namespace std;
- const int maxn = , INF = 0x7fffffff;
- char a[maxn];
- int s[maxn];
- int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
- int ran[maxn], height[maxn];
- void get_sa(int m)
- {
- int i, *x = t, *y = t2;
- for(i = ; i < m; i++) c[i] = ;
- for(i = ; i < n; i++) c[x[i] = s[i]]++;
- for(i = ; i < m; i++) c[i] += c[i-];
- for(i = n-; i >= ; i--) sa[--c[x[i]]] = i;
- for(int k = ; k <= n; k <<= )
- {
- int p = ;
- for(i = n-k; i < n; i++) y[p++] = i;
- for(i = ; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
- for(i = ; i < m; i++) c[i] = ;
- for(i = ; i < n; i++) c[x[y[i]]]++;
- for(i = ; i< m; i++) c[i] += c[i-];
- for(i = n-; i >= ; i--) sa[--c[x[y[i]]]] = y[i];
- swap(x, y);
- p = ; x[sa[]] = ;
- for(i = ; i < n; i++)
- x[sa[i]] = y[sa[i-]] == y[sa[i]] && y[sa[i-]+k] == y[sa[i]+k] ? p- : p++;
- if(p >= n) break;
- m = p;
- }
- int k = ;
- for(i = ; i < n; i++) ran[sa[i]] = i;
- for(i = ; i < n; i++)
- {
- if(k) k--;
- int j = sa[ran[i]-];
- while(s[i+k] == s[j+k]) k++;
- height[ran[i]] = k;
- }
- }
- int main()
- {
- int T;
- rd(T);
- while(T--)
- {
- rs(a);
- n = strlen(a);
- rep(i, , n)
- s[i] = a[i];
- int sum = n*(n+)/;
- s[n++] = ;
- get_sa();
- rep(i, , n)
- sum -= height[i];
- cout<< sum <<endl;
- }
- return ;
- }
Distinct Substrings SPOJ - DISUBSTR(后缀数组水题)的更多相关文章
- Distinct Substrings SPOJ - DISUBSTR 后缀数组
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- Spoj-DISUBSTR - Distinct Substrings~New Distinct Substrings SPOJ - SUBST1~(后缀数组求解子串个数)
Spoj-DISUBSTR - Distinct Substrings New Distinct Substrings SPOJ - SUBST1 我是根据kuangbin的后缀数组专题来的 这两题题 ...
- 705. New Distinct Substrings spoj(后缀数组求所有不同子串)
705. New Distinct Substrings Problem code: SUBST1 Given a string, we need to find the total number o ...
- SPOJ DISUBSTR ——后缀数组
[题目分析] 后缀数组模板题. 由于height数组存在RMQ的性质. 那么对于一个后缀,与前面相同的串总共有h[i]+sa[i]个.然后求和即可. [代码](模板来自Claris,这个板子太漂亮了) ...
- SPOJ DISUBSTR 后缀数组
题目链接:http://www.spoj.com/problems/DISUBSTR/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字 ...
- [spoj DISUBSTR]后缀数组统计不同子串个数
题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...
- poj2774(后缀数组水题)
http://poj.org/problem?id=2774 题意:给你两串字符,要你找出在这两串字符中都出现过的最长子串......... 思路:先用个分隔符将两个字符串连接起来,再用后缀数组求出h ...
- BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题
BZOJ 1031 [JSOI2007]字符加密Cipher | 后缀数组模板题 将字符串复制一遍接在原串后面,然后后缀排序即可. #include <cmath> #include &l ...
- POJ 2774 Long Long Message 后缀数组模板题
题意 给定字符串A.B,求其最长公共子串 后缀数组模板题,求出height数组,判断sa[i]与sa[i-1]是否分属字符串A.B,统计答案即可. #include <cstdio> #i ...
随机推荐
- hdu1754 I Hate It(线段树单点更新,区间查询)
传送门 有更新单个学生成绩和查询某个区间内学生成绩最大值两种操作 线段树代码 #include<bits/stdc++.h> using namespace std; +; using n ...
- Android studio Error occurred during initialization of VM 问题解决
最近开发导入其他Android项目遇见的问题,如下图: 解决办法: 将org.gradle.jvmargs=的值该为521(堆内存分配过高导致) 备忘,希望能帮助到大家
- 高速下载百度网盘的方法,只需要一个软件【win/mac/linux】
论坛里差不多全部都是用百度云的,但是官方百度云的限速,大家都懂的,下面教大家使用一个软件满速下载,不用油猴 1,软件支持mac,win64/32,linux.Linux只有自己编译使用,我也不会哈哈哈 ...
- [linux] lsyncd同步工具
环境说明: 192.168.56.101 同步源 192.168.56.102 同步目标 操作系统centos 7 lsyncd项目地址:https://github.com/axkibe/lsync ...
- url的param与dict转换
urllib.parse.urlencode urlencode from urllib import parse from urllib.request import urlopen from ur ...
- 看oracle的sid
ps -ef|grep pmon 可以从进程名字里看到 也可以通过 sqlplus / as sysdbashow parameter instance_name
- 第十二次作业psp
psp 进度条 代码累积折线图 博文累积折线图 psp饼状图
- 20172319 2018.03.27-04.05 《Java程序设计》第4周学习总结
20172319 2018.03.27-04.05 <Java程序设计>第4周学习总结 教材学习内容总结 第四章 编写类 类与对象的回顾:对象是有状态的,状态由对象的属性值确定.属性由类中 ...
- 利用JAVA制作简单登录窗口
import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...
- 福大软工1816 · 评分结果 · Alpha冲刺
作业地址:alpha冲刺1.alpha冲刺2.alpha冲刺3.alpha冲刺4.alpha冲刺5.alpha冲刺6.alpha冲刺7.alpha冲刺8.alpha冲刺9.alpha冲刺10 作业提交 ...