zoukankan      html  css  js  c++  java
  • HDU#3518. Boring counting

    找出一个字符串中至少重复出现两次的字串的个数(重复出现时不能重叠)。

    题目描述

    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”).
     

    输入格式

    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).
     

    输出格式

    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.
     

    样例输入输出

    输入

    aaaa ababcabb aaaaaa #
     

    输出

    2 3 3
    利用height数组分组乱搞。
     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <cstdio>
     5 const int N = 1000 + 11, inf = 1 << 30;
     6 using namespace std;
     7 char ss[N];
     8 int R[N],Rank[N],wv[N],wb[N],wss[N],Sa[N],height[N],f[N];
     9 
    10 int cmp(int *r,int a,int b,int l)
    11 {
    12     return (r[a] == r[b] && r[a+l] == r[b+l]);
    13 }
    14 
    15 void da(int *r,int *sa,int n,int m)
    16 {
    17     int i,j,p, *x = Rank, *y = wb, *t;
    18     for(i = 0; i < m; ++i) wss[i] = 0;
    19     for(i = 0; i < n; ++i) ++wss[x[i] = r[i]];
    20     for(i = 1; i < m ; ++i) wss[i] += wss[i-1];
    21     for(i = n - 1; i >= 0; --i) sa[--wss[x[i]]] = i;
    22     for(p = 1, j = 1; p < n; j *= 2, m = p)
    23     {
    24         for(p = 0, i = n - j; i < n; ++i) y[p++] = i;
    25         for(i = 0; i < n ; ++i) if(sa[i] >= j) y[p++] = sa[i] - j;
    26         for(i = 0; i < n; ++i) wv[i] = x[y[i]]; // x[y[i]]
    27         for(i = 0; i < m; ++i) wss[i] = 0;
    28         for(i = 0; i < n; ++i) ++wss[wv[i]];
    29         for(i = 1; i < m; ++i) wss[i] += wss[i-1];
    30         for(i = n - 1; i >= 0; --i ) sa[--wss[wv[i]]] = y[i];
    31         for(t = x, x = y, y = t,p = 1,x[sa[0]] = 0, i = 1; i < n; ++i)
    32         x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++; // x[sa[i]]
    33     }
    34 //    for(i = 1; i <= n; ++i) cout<<sa[i]<<endl;
    35 }
    36 
    37 void cal_height(int *r,int *sa,int n)
    38 {
    39     int i,j,k = 0;
    40     for(i = 1; i <= n; ++i) f[sa[i]] = i;
    41     for(i = 0; i < n; height[f[i++]] = k)
    42     for(k?k--:0, j = sa[f[i]-1]; r[j+k] == r[i+k]; ++k);
    43 }
    44 
    45 void Solve(int n)
    46 {
    47     int ans = 0,minx = 1001,maxx = -1;
    48     for(int i = 1; i <= (n + 1) / 2; ++i)
    49     {
    50         minx = 1001, maxx = -1;
    51         for(int j = 1; j <= n; ++j)
    52         {
    53             if(height[j] >= i)
    54             {
    55                 minx = min(minx,min(Sa[j],Sa[j-1]));
    56                 maxx = max(maxx,max(Sa[j],Sa[j-1]));
    57             
    58             }
    59             else 
    60             {
    61                 if(minx+i <= maxx) ++ans;
    62                 minx = 1001, maxx = -1; 
    63             }
    64         }
    65         if(minx+i <= maxx) ++ans;
    66     }
    67     printf("%d
    ",ans);
    68 }
    69 
    70 int main()
    71 {    
    72     while(~scanf("%s",ss))
    73     {
    74         if(ss[0] == '#') break;
    75         int l = strlen(ss);    memset(R,0,sizeof(R));
    76         for(int i = 0; i < l; ++i) R[i] = ss[i]; R[l] = 0; 
    77         da(R,Sa,l + 1, 128);
    78         cal_height(R,Sa,l);
    79         Solve(l);
    80     }
    81     return 0;
    82 }
  • 相关阅读:
    HDU 6464 /// 权值线段树
    HDU 6468 /// DFS
    HDU 6469 /// 二分
    HDU 6470 /// 矩阵快速幂
    USACO 2014 US Open Odometer /// 数位DP
    hdu5988(费用流,对数相乘做加法)
    对AC自动机+DP题的一些汇总与一丝总结 (2)
    对AC自动机+DP题的一些汇总与一丝总结 (1)
    POJ 1625 Censored ( Trie图 && DP && 高精度 )
    HDU 2243 ( Trie图 矩阵构造幂和 )
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6491713.html
Copyright © 2011-2022 走看看