CSU 1632 Repeated Substrings

原创 2016年08月28日 16:47:43

Description

String analysis often arises in applications from biology and chemistry, such as the study of DNA and protein molecules. One interesting problem is to find how many substrings are repeated (at least twice) in a long string. In this problem, you will write a program to find the total number of repeated substrings in a string of at most 100 000 alphabetic characters. Any unique substring that occurs more than once is counted. As an example, if the string is “aabaab”, there are 5 repeated substrings: “a”, “aa”, “aab”, “ab”, “b”. If the string is “aaaaa”, the repeated substrings are “a”, “aa”, “aaa”, “aaaa”. Note that repeated occurrences of a substring may overlap (e.g. “aaaa” in the second case).

Input

The input consists of at most 10 cases. The first line contains a positive integer, specifying the number of
cases to follow. Each of the following line contains a nonempty string of up to 100 000 alphabetic characters.

Output

For each line of input, output one line containing the number of unique substrings that are repeated. You
may assume that the correct answer fits in a signed 32-bit integer.

Sample Input

3
aabaab
aaaaa
AaAaA

Sample Output

5
4

5

求重复了两次或以上的子串的数量,根据h数组统计即可

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define loop(i,j,k) for (int i = j;i != -1; i = k[i])
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
#define fi first
#define se second
#define mp(i,j) make_pair(i,j)
#define pii pair<string,string>
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 1e8;
const int N = 5e5 + 10;
const int read()
{
	char ch = getchar();
	while (ch<'0' || ch>'9') ch = getchar();
	int x = ch - '0';
	while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
	return x;
}
int T;

struct Sa
{
	char s[N];
	int rk[2][N], sa[N], h[N], w[N], now, n;
	int rmq[N][20], lg[N];

	bool GetS()
	{
		scanf("%s", s + 1);
		return true;
	}

	void getsa(int z, int &m)
	{
		int x = now, y = now ^= 1;
		rep(i, 1, z) rk[y][i] = n - i + 1;
		for (int i = 1, j = z; i <= n; i++)
			if (sa[i] > z) rk[y][++j] = sa[i] - z;

		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[rk[x][rk[y][i]]]++;
		rep(i, 1, m) w[i] += w[i - 1];
		per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];
		for (int i = m = 1; i <= n; i++)
		{
			int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];
			rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;
		}
	}

	void getsa(int m)
	{
		n = strlen(s + 1);
		rk[1][0] = now = sa[0] = s[0] = 0;
		rep(i, 1, m) w[i] = 0;
		rep(i, 1, n) w[s[i]]++;
		rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];
		rep(i, 1, m) w[i] += w[i - 1];
		rep(i, 1, n) rk[0][i] = rk[1][s[i]];
		rep(i, 1, n) sa[w[s[i]]--] = i;

		rk[1][n + 1] = rk[0][n + 1] = 0;	//多组的时候容易出bug
		for (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);
		for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j)
		{
			if (rk[now][i] == 1) continue;
			int k = n - max(sa[rk[now][i] - 1], i);
			while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;
		}
	}

	void getrmq()
	{
		h[n + 1] = h[1] = lg[1] = 0;
		rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;
		for (int i = 1; (1 << i) <= n; i++)
		{
			rep(j, 2, n)
			{
				if (j + (1 << i) > n + 1) break;
				rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);
			}
		}
	}

	int lcp(int x, int y)
	{
		int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);
		return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);
	}

	void work()
	{
		GetS();	getsa(256);
		int ans = 0;
		rep(i, 2, n) ans += max(0, h[i] - h[i - 1]);
		printf("%d\n", ans);
	}
}sa;

int main()
{
	T = read(); while (T--) sa.work();
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

CSU1632: Repeated Substrings(后缀数组)

Description String analysis often arises in applications from biology and chemistry, such as the s...

CSU 1632 最长公共前缀(出现超过两次的不同子串有多少个)

1632: Repeated Substrings Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 83  Solved: 33 [Submi...

UVa #1632 Alibaba (习题9-8)

区间dp,和UVa #1336 Fixing the Great Wall (例题9-21)类似。但数据规模太大,记忆化搜索会TLE,必须递推。 我一开始把状态设计为dp(i,j,k)表示当...

花瓶收集 POJ1632 经典DFS 枚举+暴搜

花瓶收集 POJ1632 经典DFS 枚举+暴搜 做这道题的第一个障碍在于读题。刚开始把题目读了三遍,也没搞清楚要求什么。其实可以把shape和decoration看成点,它们之际的对应关系看成边,这...

UVA 1632 区间DP

题意有n个宝藏,宝藏信息n行,第一个数字表示宝藏的位置,第二个数字表示宝藏消失的时间。求获得所有宝藏,最少需要多久?如果不能获得所有宝藏,则输出’No solution’。题解由于取宝藏不需要时间,所...

uva1632Alibaba

题目描述: Alibaba the famous character of our childhood stories would like to be immortal in order to k...
  • fouzhe
  • fouzhe
  • 2016年04月01日 16:46
  • 279

uva 1632 阿里巴巴

大意:直线上有n个点,其中第i个点的坐标为xi,且它会在di秒后消失。Alibaba可以从直线任意位置出发,求访问玩所有点的最短时间,无解输出No solution. 思路:对所有点按照坐标从小到大...

1632 - Alibaba(DP)

该题过的人不多,但是实际上并不难 。首先,不难用贪心的方法证明,起点一定要选最快消失的点, 所有宝藏地点的坐标从小到大排列,因为起点固定且获取宝藏的时间忽略不计,所以不难发现任意时刻,已经拾取的点一定...

HDU1632+半平面交

模板题 题意:给定两个凸多边形,求出合并后的面积,这个合并后的面积不包括重叠部分。 #include #include #include #include #include using names...
  • xxx0624
  • xxx0624
  • 2013年07月25日 21:43
  • 606
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CSU 1632 Repeated Substrings
举报原因:
原因补充:

(最多只允许输入30个字)