UVALive 6869 Repeated Substrings
2015-05-26 10:14
363 查看
[b]Repeated Substrings[/b]
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu
cases to follow. Each of the following line contains a nonempty string of up to 100 000 alphabetic characters.
may assume that the correct answer fits in a signed 32-bit integer.
View Code
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 ofcases 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. Youmay 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]#include <bits/stdc++.h> using namespace std; const int maxn = 201010; int cnt[maxn],c[maxn],sa[maxn]; struct node{ int son[128],f,len; void init(){ memset(son,-1,sizeof son); f = -1; len = 0; } }; struct SAM{ node e[maxn]; int tot,last; int newnode(int len = 0){ e[tot].init(); e[tot].len = len; return tot++; } void init(){ tot = last = 0; newnode(); } void add(int c){ int p = last,np = newnode(e[p].len + 1); while(p != -1 && e[p].son[c] == -1){ e[p].son[c] = np; p = e[p].f; } if(p == -1) e[np].f = 0; else{ int q = e[p].son[c]; if(e[p].len + 1 == e[q].len) e[np].f = q; else{ int nq = newnode(); e[nq] = e[q]; e[nq].len = e[p].len + 1; e[q].f = e[np].f = nq; while(p != -1 && e[p].son[c] == q){ e[p].son[c] = nq; p = e[p].f; } } } last = np; cnt[np] = 1; } }sam; char str[maxn]; int main(){ int kase; scanf("%d",&kase); while(kase--){ scanf("%s",str); sam.init(); memset(cnt,0,sizeof cnt); int len = strlen(str); for(int i = 0; str[i]; ++i) sam.add(str[i]); node *e = sam.e; memset(c,0,sizeof c); for(int i = 0; i < sam.tot; ++i) c[e[i].len]++; for(int i = 1; i <= len; ++i) c[i] += c[i-1]; for(int i = sam.tot-1; i >= 0; --i) sa[--c[e[i].len]] = i; for(int i = sam.tot-1; i > 0; --i){ int v = sa[i]; cnt[e[v].f] += cnt[v]; } int ret = 0; for(int i = 1; i < sam.tot; ++i){ if(cnt[i] <= 1) continue; ret += e[i].len - e[e[i].f].len; } printf("%d\n",ret); } return 0; }
View Code
相关文章推荐
- Repeated Substrings(UVAlive 6869)
- UVALive - 6869 Repeated Substrings 后缀数组
- UVALive 6869 Repeated Substrings(后缀数组)
- UVALive 6869(后缀数组)
- UVALive 7410 Kingdom of Black and White(脑洞 + stl)
- UVALive 7147 World Cup 2014 上海区域赛j题 思维题
- UVAlive 7487 Association of Cats and Magical Lights(线段树)
- UVALive 5880 Vigenère Cipher Encryption (模拟)
- POJ1011 HDU1455 UVA307 UVALive5522 Sticks题解
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- UVALIVE 4329(树状数组)
- ECfinal UVALive 7512 November 11th (公式)
- UVALive 4877 记忆搜索
- UVALive 3956 Key Task (bfs+状态压缩)
- HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation【MAP+排序+水题】
- UVALive 3211 Now or later(2-sat)
- UVALive 2957 Bring Them There (最大流+拆点)
- UVaLive 6697 (DP)
- uvalive 4671 - K-neighbor substrings 快速傅利叶变换
- UVaLive 6698 Sightseeing Bus Drivers (水题,贪心)