Problem Description
035 now faced a tough problem,his english teacher gives him a string,which consists with n lower case letter,he must figure out how many substrings appear at least twice,moreover,such apearances can not overlap each other.
Take aaaa as an example.”a” apears four times,”aa” apears two times without overlaping.however,aaa can’t apear more than one time without overlaping.since we can get “aaa” from [0-2](The position of string begins with 0) and [1-3]. But the interval [0-2] and [1-3] overlaps each other.So “aaa” can not take into account.Therefore,the answer is 2(“a”,and “aa”).
Input
The input data consist with several test cases.The input ends with a line “#”.each test case contain a string consists with lower letter,the length n won’t exceed 1000(n <= 1000).
Output
For each test case output an integer ans,which represent the answer for the test case.you’d better use int64 to avoid unnecessary trouble.
Sample Input
aaaa
ababcabb
aaaaaa
#
Sample Output
2
3
3
Source
2010 ACM-ICPC Multi-University Training Contest(9)——Host by HNU
Recommend
求出字符串的后缀数组,枚举长度,然后对后缀分组,由于每一组的后缀它们的排名连续,因此它们拥有相同的一段前缀,这样就不会产生重复计数问题,注意记录每一组后缀里,出现的最靠前的和最靠后的后缀位置,差不多1000*500的复杂度
/*************************************************************************
> File Name: HDU3518.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年04月09日 星期四 13时35分42秒
************************************************************************/
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>
using namespace std;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;
class SuffixArray
{
public:
static const int N = 1200;
int init[N];
int X[N];
int Y[N];
int Rank[N];
int sa[N];
int height[N];
int buc[N];
int size;
void clear()
{
size = 0;
}
void insert(int n)
{
init[size++] = n;
}
bool cmp(int *r, int a, int b, int l)
{
return (r[a] == r[b] && r[a + l] == r[b + l]);
}
void getsa(int m = 256)
{
init[size] = 0;
int l, p, *x = X, *y = Y, n = size + 1;
for (int i = 0; i < m; ++i)
{
buc[i] = 0;
}
for (int i = 0; i < n; ++i)
{
++buc[x[i] = init[i]];
}
for (int i = 1; i < m; ++i)
{
buc[i] += buc[i - 1];
}
for (int i = n - 1; i >= 0; --i)
{
sa[--buc[x[i]]] = i;
}
for (l = 1, p = 1; l <= n && p < n; m = p, l *= 2)
{
p = 0;
for (int i = n - l; i < n; ++i)
{
y[p++] = i;
}
for (int i = 0;i < n; ++i)
{
if (sa[i] >= l)
{
y[p++] = sa[i] - l;
}
}
for (int i = 0; i < m; ++i)
{
buc[i] = 0;
}
for (int i = 0; i < n; ++i)
{
++buc[x[y[i]]];
}
for (int i = 1; i < m; ++i)
{
buc[i] += buc[i - 1];
}
for (int i = n - 1; i >= 0; --i)
{
sa[--buc[x[y[i]]]] = y[i];
}
int i;
for (swap(x, y), x[sa[0]] = 0, p = 1, i = 1; i < n; ++i)
{
x[sa[i]] = cmp(y, sa[i - 1], sa[i], l) ? p - 1 : p++;
}
}
}
void getheight()
{
int h = 0, n = size;
for (int i = 0; i <= n; ++i)
{
Rank[sa[i]] = i;
}
height[0] = 0;
for (int i = 0; i < n; ++i)
{
if (h > 0)
{
--h;
}
int j =sa[Rank[i] - 1];
for (; i + h < n && j + h < n && init[i + h] == init[j + h]; ++h);
height[Rank[i] - 1] = h;
}
}
void solve()
{
int ans = 0;
for (int i = 1; i <= size / 2; ++i)
{
int il = sa[1], ir = sa[1];
for (int j = 1; j < size; ++j)
{
if (height[j] >= i)
{
il = min(il, sa[j + 1]);
ir = max(ir, sa[j + 1]);
}
else
{
if (ir - il >= i)
{
++ans;
}
ir = il = sa[j + 1];
}
}
if (ir - il >= i)
{
++ans;
}
}
printf("%d\n", ans);
}
}SA;
char str[1200];
int main()
{
while (~scanf("%s", str))
{
if (str[0] == ‘#‘)
{
break;
}
SA.clear();
int len = strlen(str);
for (int i = 0; i < len; ++i)
{
SA.insert(str[i] - ‘a‘ + 1);
}
SA.getsa(30);
SA.getheight();
SA.solve();
}
return 0;
}