您的位置:首页 > 其它

hdu 3518 后缀数组

2016-08-21 09:58 483 查看

Boring counting

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2906 Accepted Submission(s): 1201


[align=left]Problem Description[/align]
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”).

[align=left]Input[/align]
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).

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

aaaa
ababcabb
aaaaaa
#

[align=left]Sample Output[/align]

2
3
3

/*
hdu 3518 后缀数组

problem:
给你一个字符串,问它有多少种出现两次且不重叠的子串

solve:
因为总长度为1000,所以考虑通过枚举子串的长度来计算每种长度有多少种子串。
但是感觉不知道怎么区分相同长度的不同串 T_T
后来发现最开始思路有问题,对height分组讨论的时候。每一组height在同一长度下只可能有一种子串
所以通过 枚举+判断 就能得出结果

hhh-2016-08-12 10:29:23
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
using namespace std;
#define lson  (i<<1)
#define rson  ((i<<1)|1)
typedef long long ll;
typedef unsigned int ul;
const int INF = 0x3f3f3f3f;
const int  maxn = 200000+10;
const int mod = 1e9+7;

int t1[maxn],t2[maxn],c[maxn];
bool cmp(int *r,int a,int b,int l)
{
return r[a]==r[b] &&r[l+a] == r[l+b];
}

void get_sa(int str[],int sa[],int Rank[],int height[],int n,int m)
{
n++;
int p,*x=t1,*y=t2;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[i] = str[i]]++;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i>=0; i--) sa[--c[x[i]]] = i;
for(int j = 1; j <= n; j <<= 1)
{
p = 0;
for(int i = n-j; i < n; i++) y[p++] = i;
for(int i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i]-j;
for(int i = 0; i < m; i++) c[i] = 0;
for(int i = 0; i < n; i++) c[x[y[i]]]++ ;
for(int i = 1; i < m; i++) c[i] += c[i-1];
for(int i = n-1; i >= 0; i--)  sa[--c[x[y[i]]]] = y[i];

swap(x,y);
p = 1;
x[sa[0]] = 0;
for(int i = 1; i < n; i++)
x[sa[i]] = cmp(y,sa[i-1],sa[i],j)? p-1:p++;
if(p >= n) break;
m = p;
}
int k = 0;
n--;
for(int i = 0; i <= n; i++)
Rank[sa[i]] = i;
for(int i = 0; i < n; i++)
{
if(k) k--;
int j = sa[Rank[i]-1];
while(str[i+k] == str[j+k]) k++;
height[Rank[i]] = k;
}

}

int Rank[maxn],height[maxn];
int sa[maxn];
char str[maxn];
int r[maxn];

int cal(int len,int n)
{
int cnt = 0;
int minx = 100000,maxx = 0;
for(int i = 2;i <= n;i++)
{
if(height[i] >= len)
{
minx = min(minx,sa[i]),minx = min(minx,sa[i-1]);
maxx = max(maxx,sa[i]),maxx = max(maxx,sa[i-1]);
//            cout << "max:" <<maxx << " " <<"min:" << minx <<endl;
}
else
{
if(maxx - minx >= len)
cnt ++;
minx = 100000;maxx = 0;
}
}
if(maxx - minx >= len)
cnt ++;
// cout << len << " " <<cnt <<endl;
return cnt;
}

int main()
{
while(scanf("%s",str) != EOF && str[0] != '#')
{
int len = strlen(str);
for(int i = 0;i < len;i++)
r[i] = str[i] - 'a'+1;
r[len] = 0;
get_sa(r,sa,Rank,height,len,128);

int ans = 0;
for(int i = 1;i <= len/2+1;i++)
ans += cal(i,len);
printf("%d\n",ans);
}
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: