zoukankan      html  css  js  c++  java
  • UVALive 6869 Repeated Substrings

    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
    ",ret);
    57     }
    58     return 0;
    59 }
    View Code

    后缀自动机

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 201010;
     4 int cnt[maxn],c[maxn],sa[maxn];
     5 struct node{
     6     int son[128],f,len;
     7     void init(){
     8         memset(son,-1,sizeof son);
     9         f = -1;
    10         len = 0;
    11     }
    12 };
    13 struct SAM{
    14     node e[maxn];
    15     int tot,last;
    16     int newnode(int len = 0){
    17         e[tot].init();
    18         e[tot].len = len;
    19         return tot++;
    20     }
    21     void init(){
    22         tot = last = 0;
    23         newnode();
    24     }
    25     void add(int c){
    26         int p = last,np = newnode(e[p].len + 1);
    27         while(p != -1 && e[p].son[c] == -1){
    28             e[p].son[c] = np;
    29             p = e[p].f;
    30         }
    31         if(p == -1) e[np].f = 0;
    32         else{
    33             int q = e[p].son[c];
    34             if(e[p].len + 1 == e[q].len) e[np].f = q;
    35             else{
    36                 int nq = newnode();
    37                 e[nq] = e[q];
    38                 e[nq].len = e[p].len + 1;
    39                 e[q].f = e[np].f = nq;
    40                 while(p != -1 && e[p].son[c] == q){
    41                     e[p].son[c] = nq;
    42                     p = e[p].f;
    43                 }
    44             }
    45         }
    46         last = np;
    47         cnt[np] = 1;
    48     }
    49 }sam;
    50 char str[maxn];
    51 int main(){
    52     int kase;
    53     scanf("%d",&kase);
    54     while(kase--){
    55         scanf("%s",str);
    56         sam.init();
    57         memset(cnt,0,sizeof cnt);
    58         int len = strlen(str);
    59         for(int i = 0; str[i]; ++i)
    60             sam.add(str[i]);
    61         node *e = sam.e;
    62         memset(c,0,sizeof c);
    63         for(int i = 0; i < sam.tot; ++i) c[e[i].len]++;
    64         for(int i = 1; i <= len; ++i) c[i] += c[i-1];
    65         for(int i = sam.tot-1; i >= 0; --i) sa[--c[e[i].len]] = i;
    66         for(int i = sam.tot-1; i > 0; --i){
    67             int v = sa[i];
    68             cnt[e[v].f] += cnt[v];
    69         }
    70         int ret = 0;
    71         for(int i = 1; i < sam.tot; ++i){
    72             if(cnt[i] <= 1) continue;
    73             ret += e[i].len - e[e[i].f].len;
    74         }
    75         printf("%d
    ",ret);
    76     }
    77     return 0;
    78 }
    View Code
  • 相关阅读:
    Java Web学习总结(9)——servlet和Jsp生命周期解读
    Java Web学习总结(9)——servlet和Jsp生命周期解读
    Java Web学习总结(10)——Session详解
    Java Web学习总结(10)——Session详解
    Mysql学习总结(6)——MySql之ALTER命令用法详细解读
    Mysql学习总结(6)——MySql之ALTER命令用法详细解读
    十大热门问题集锦,一眼看懂华为云数据库黑科技
    python上下文管理器细读
    关于token你需要知道的
    使用app测试Modelarts在线服务
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4529894.html
Copyright © 2011-2022 走看看