题意:求一个串的出现次数超过1次的字串的个数

思路:对于一个后缀,出现在它后面的所有后缀与它的LCP的最大值就是应该增加的答案,当然这里没有考虑去重,但是却转化了问题,使得我们可以用最长公共前缀来统计答案。假设我们将每一个后缀按字典序排好,那么对于每一个后缀,与其它后缀的LCP的最大值其实就是与它相邻的两个的lcp的较大值,这不就是height数组了么?考虑去重的问题,如果height[i]>height[i-1],那么对于rank为i和i-1的最长公共前缀,它的前height[i-1]个前缀已经统计过了,答案只需要加上height[i]-height[i-1],如果height[i]<=height[i-1]则不需处理。

 #pragma comment(linker, "/STACK:10240000,10240000")

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = ;
const int md = ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } /// 构造后缀数组的之前,需要在原串末尾加个空字符(比其它字符都小即可),
///把这个空字符看成原串的一部分(这样在比较的时候到这个位置一定可以分个大小),
///所以n应该为原序列长度+1,后缀n-1是"空串",sa[0]总是n-1。
struct SuffixArray {
int n;
int arr[][maxn];
int *sa, *x, *y, *c, *rnk, *height;
SuffixArray() { sa = arr[]; x = arr[]; y = arr[]; c = arr[]; rnk = arr[]; height = arr[]; }
void resize(int nn) { n = nn; mem0(arr[]); }
void build_sa(int s[], int m) { // m is biger than the max value of char
rep_up0(i, m) c[i] = ;
rep_up0(i, n) c[x[i] = s[i]]++;
rep_up1(i, m - ) c[i] += c[i - ];
rep_down0(i, n) sa[--c[x[i]]] = i;
for (int k = ; k <= n; k <<= ) {
int p = ;
for (int i = n - k; i < n; i++) y[p++] = i;
rep_up0(i, n) if (sa[i] >= k) y[p++] = sa[i] - k;
rep_up0(i, m) c[i] = ;
rep_up0(i, n) c[x[y[i]]]++;
rep_up0(i, m) c[i] += c[i - ];
rep_down0(i, n) sa[--c[x[y[i]]]] = y[i];
swap(x, y);
p = ; x[sa[]] = ;
for (int 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;
}
}
void build_height(int s[]) {
mem0(height);
int k = ;
rep_up0(i, n) rnk[sa[i]] = i;
rep_up0(i, n) {
if (k) k--;
int j = sa[rnk[i] - ];
while (s[i + k] == s[j + k]) k++;
height[rnk[i]] = k;
}
}
int solve(int n) {
int ans = ;
for (int i = ; i <= n; i ++) {
if (height[i] > height[i - ]) ans += height[i] - height[i - ];
}
return ans;
}
};
char s[maxn];
int ss[maxn];
SuffixArray sa;
int main() {
//freopen("in.txt", "r", stdin);
int T;
cin >> T;
while (T --) {
scanf("%s", s);
int len = strlen(s) + ;
rep_up0(i, len) ss[i] = s[i];
sa.resize(len);
sa.build_sa(ss, );
sa.build_height(ss);
cout << sa.solve(len - ) << endl;
}
return ;
}

ps: 这个代码能够正常工作,但是里面的ST表与“max”有关的部分有问题,慎用。

[csu/coj 1632]LCP的更多相关文章

  1. [csu/coj 1619] 递归

    题意:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1619 思路:由于式子具有递归的性质,考虑递归解,中间结果会超64位int,需用大数.另外自己 ...

  2. [csu/coj 1083]贪心

    题意:给定n个线段,问能不能把x,y,z个长度为1,2,3的线段不重合地放进去. 思路:首先如果n个线段长度比要放的长度之和小,则无解,否则先考虑放2和3,如果2和3放下了1肯定可以放下(这是显然的) ...

  3. [csu/coj 1078]多个序列的最长公共子序列

    题意:给n个序列,同一个序列里面元素互不相同,求它们的最长公共子序列. 思路:任取一个序列,对于这个序列里面的两个数ai,aj(i<j),如果对于其它每一个序列,都出现过ai,aj,且ai在aj ...

  4. [csu/coj 1079]树上路径查询 LCA

    题意:询问树上从u到v的路径是否经过k 思路:把树dfs转化为有根树后,对于u,v的路径而言,设p为u,v的最近公共祖先,u到v的路径必定是可以看成两条路径的组合,u->p,v->p,这样 ...

  5. [csu/coj 1080]划分树求区间前k大数和

    题意:从某个区间内最多选择k个数,使得和最大 思路:首先题目给定的数有负数,如果区间前k大出现负数,那么负数不选和更大,于是对于所有最优选择,负数不会出现,所以用0取代负数,问题便转化为区间的前k大数 ...

  6. csu 1812: 三角形和矩形 凸包

    传送门:csu 1812: 三角形和矩形 思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点.然后求出所有的交点.这些点构成一个凸包,求凸包面积就OK了. /************** ...

  7. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  8. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...

  9. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

随机推荐

  1. [YII2] 修改默认控制器Controller以及默认方法Action

    试了好多方法都没成功,下面方法绝对能成功设置 在框架里面有源码,在/vendor/yiisoft/yii2/web/Application.php的第34行找到了: class Application ...

  2. redis:安装及基础知识(一)

    Redis官网:https://redis.io/ Redis中文网:http://www.redis.cn/ Redis 是一个开源的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. ...

  3. C#集合类型——Hashtable、Dictionary之浅谈

    Hashtable表 数组.数组集合.List集合都是通过索引来访问里面成员.哈希表则是通过键来访问成员值.键不可为空,值可为空. 比如: Hashtable  hash=new  Hashtable ...

  4. Java IO 流--FileUtils 工具类封装

    IO流的操作写多了,会发现都已一样的套路,为了使用方便我们可以模拟commosIo 封装一下自己的FileUtils 工具类: 1.封装文件拷贝: 文件拷贝需要输入输出流对接,通过输入流读取数据,然后 ...

  5. MySQL主从数据库配置与原理

    1.为什么要搭建主从数据库 (1)通过增加从库实现读写分离,提高系统负载能力 (2)将从库作为数据库备份库,实现数据热备份,为数据恢复提供机会 (3)根据业务将不同服务部署在不同机器同时又共享相同的数 ...

  6. php静态变量的销毁

    什么都不说,先上代码: public function _childrenids($data,$cate_id,$clear=false) { static $arr = array(); if ($ ...

  7. [Hands-on-Machine-Learning-master] 02 Housing

    用到的函数 numpy.random.permutation随机排列一个序列,返回一个排列的序列. >>> np.random.permutation(10) array([1, 7 ...

  8. BIOS和CMOS区别

    在网上看到一篇关于CMOS的文章,分享一下. 原文地址:http://jingyan.baidu.com/article/c843ea0b51155d77921e4a7a.html BIOS是什么? ...

  9. hdu_2124 Flying to the Mars & hdu_1800 Repair the Wall 贪心水题

    hdu_1800 简单排一下序,从大开始把比他小的都访问一遍,ans++: #include <iostream> #include <stdio.h> #include &l ...

  10. dotnetcore配置框架简介

    一.前言 配置的本质就是字符串的键值对,微软的一系列接口其实就是对这些键值对字符串的抽象. 二.基本类型 2.1.Nuget包 Microsoft.Extensions.Configuration.A ...