码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 6869 Repeated Substrings

时间:2015-05-26 10:38:08      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

Repeated Substrings

Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

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

HINT

 

Source

解题:后缀数组lcp的应用,如果lcp[i] > lcp[i-1]那么累加lcp[i] - lcp[i-1]

技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 100010;
 4 int rk[maxn],wb[maxn],wv[maxn],wd[maxn],lcp[maxn];
 5 bool cmp(int *r,int i,int j,int k) {
 6     return r[i] == r[j] && r[i+k] == r[j+k];
 7 }
 8 void da(int *r,int *sa,int n,int m) {
 9     int i,k,p,*x = rk,*y = wb;
10     for(i = 0; i < m; ++i) wd[i] = 0;
11     for(i = 0; i < n; ++i) wd[x[i] = r[i]]++;
12     for(i = 1; i < m; ++i) wd[i] += wd[i-1];
13     for(i = n-1; i >= 0; --i) sa[--wd[x[i]]] = i;
14 
15     for(p = k = 1; p < n; k <<= 1,m = p) {
16         for(p = 0,i = n-k; i < n; ++i) y[p++] = i;
17         for(i = 0; i < n; ++i) if(sa[i] >= k) y[p++] = sa[i] - k;
18         for(i = 0; i < n; ++i) wv[i] = x[y[i]];
19 
20         for(i = 0; i < m; ++i) wd[i] = 0;
21         for(i = 0; i < n; ++i) wd[wv[i]]++;
22         for(i = 1; i < m; ++i) wd[i] += wd[i-1];
23         for(i = n-1; i >= 0; --i) sa[--wd[wv[i]]] = y[i];
24 
25         swap(x,y);
26         x[sa[0]] = 0;
27         for(p = i = 1; i < n; ++i)
28             x[sa[i]] = cmp(y,sa[i-1],sa[i],k)?p-1:p++;
29     }
30 }
31 void calcp(int *r,int *sa,int n) {
32     for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
33     int h = 0;
34     for(int i = 0; i < n; ++i) {
35         if(h > 0) h--;
36         for(int j = sa[rk[i]-1]; i+h < n && j+h < n; h++)
37             if(r[i+h] != r[j+h]) break;
38         lcp[rk[i]] = h;
39     }
40 }
41 int r[maxn],sa[maxn];
42 char str[maxn];
43 int main() {
44     int hn,x,y,cs,ret;
45     scanf("%d",&cs);
46     while(cs--) {
47         scanf("%s",str);
48         int len = strlen(str);
49         for(int i = 0; str[i]; ++i)
50             r[i] = str[i];
51         ret = r[len] = 0;
52         da(r,sa,len+1,128);
53         calcp(r,sa,len);
54         for(int i = 2; i <= len; ++i)
55             if(lcp[i] > lcp[i-1]) ret += lcp[i] - lcp[i-1];
56         printf("%d\n",ret);
57     }
58     return 0;
59 }
View Code

 

UVALive 6869 Repeated Substrings

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4529894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!