zoukankan      html  css  js  c++  java
  • BZOJ3473: 字符串【后缀数组+思维】

    Description

    给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串?

    Input

    第一行两个整数n,k。

    接下来n行每行一个字符串。

    Output

    一行n个整数,第i个整数表示第i个字符串的答案。

    Sample Input

    3 1
    abc
    a
    ab

    Sample Output

    6 1 3

    HINT

    对于 100% 的数据,1<=n,k<=105,所有字符串总长不超过105,字符串只包含小写字母。


    思路

    首先发现这东西是真的不好做。。。那就找找性质

    先把所有子串的贡献拆分成每个后缀的前缀的贡献

    然后考虑怎么算每个后缀的贡献

    又因为对于后缀i,假设前j个前缀有贡献,那么对于后缀i+1,它的前j-1个前缀一定是有贡献的

    是不是就想到了height数组的处理,很容易证明这个的复杂度是线性的

    那么怎么考虑可不可以扩展呢?

    因为我们要算这个串的出现次数

    并且知道连接所有串的分隔符一定是不会被匹配的

    所以我们可以直接二分出包含这个串的排名区间

    那么就可以预处理出排名第i的字符串至少要到第j个排名的字符串才能包含k个不同的字符串

    这个东西可以双指针做


    一定要把预处理的指针数组初值设成INF


     #include<bits/stdc++.h>
     
    using namespace std;
     
    typedef pair<int, int> pi;
    typedef long long ll;
    const int N = 2e5 + 10;
    const int LOG = 20;
     
    struct Suffix_Array {
      int s[N], n, m;
      int c[N], x[N], y[N];
      int height[N], sa[N], rank[N];
      int st[N][LOG], Log[N];
      ll sum[N]; 
       
      void init(int len, char *c) {
        n = len, m = 0;
        for (int i = 1; i <= len; i++) {
          s[i] = c[i];
          m = max(m, s[i]);
        }
      }
       
      void radix_sort() {
        for (int i = 1; i <= m; i++) c[i] = 0;
        for (int i = 1; i <= n; i++) c[x[y[i]]]++;
        for (int i = 1; i <= m; i++) c[i] += c[i - 1];
        for (int i = n; i >= 1; i--) sa[c[x[y[i]]]--] = y[i];
      }
       
      void buildsa() {
        for (int i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
        radix_sort();
        int now;
        for (int k = 1; k <= n; k <<= 1) {
          now = 0;
          for (int i = n - k + 1; i <= n; i++) y[++now] = i;
          for (int i = 1; i <= n; i++) if (sa[i] > k) y[++now] = sa[i] - k;
          radix_sort();
          y[sa[1]] = now = 1;
          for (int i = 2; i <= n; i++) y[sa[i]] = (x[sa[i]] == x[sa[i - 1]] && x[sa[i] + k] == x[sa[i - 1] + k]) ? now : ++now;
          swap(x, y);
          if (now == n) break;
          m = now;
        }
      }
       
      void buildrank() {
        for (int i = 1; i <= n; i++) rank[sa[i]] = i;
      }
       
      void buildsum() {
        for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + n - sa[i] + 1 - height[i];
      }
     
      void buildheight() {
        for (int i = 1; i <= n; i++) if (rank[i] != 1) {
          int k = max(height[rank[i - 1]] - 1, 0); 
          for (; s[i + k] == s[sa[rank[i] - 1] + k]; k++);
          height[rank[i]] = k;
        }
      }
       
      void buildst() {
        Log[1] = 0;
        for (int i = 2; i < N; i++) Log[i] = Log[i >> 1] + 1;
        for (int i = 1; i <= n; i++) st[i][0] = height[i];
        for (int j = 1; j < LOG; j++) {
          for (int i = 1; i + (1 << (j - 1)) <= n; i++) {
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
          }
        }
      }
       
      int queryst(int l, int r) {
        if (l == r) return n - sa[l] + 1;
        if (l > r) swap(l, r);
        ++l; //***
        int k = Log[r - l + 1];
        return min(st[l][k], st[r - (1 << k) + 1][k]);
      }
       
      int querylcp(int la, int lb) {
        return queryst(rank[la], rank[lb]);
      }
       
      int querylcp(int la, int ra, int lb, int rb) {
        return min(min(ra - la + 1, rb - lb + 1), queryst(rank[la], rank[lb]));
      }
       
      void build(int len, char *c) {
        init(len, c);
        buildsa();
        buildrank();
        buildheight();
        buildsum();
        buildst();
      } 
    } Sa;
     
    char s[N], c[N];
    int len[N], bg[N], ed[N], tot = 0;
    int n, k, rpos[N], num[N], bel[N];
     
    bool check(int pos, int len) {
      int x = Sa.rank[pos], l, r;
      int l_line = x, r_line = x;
      l = 1, r = x - 1;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (Sa.queryst(x, mid) > len) l_line = mid, r = mid - 1;
        else l = mid + 1;
      } 
      l = x + 1, r = tot;
      while (l <= r) {
        int mid = (l + r) >> 1;
        if (Sa.queryst(x, mid) > len) r_line = mid, l = mid + 1;
        else r = mid - 1;
      }
      return rpos[l_line] <= r_line;
    }
     
    int main() {
    #ifdef dream_maker
      freopen("input.txt", "r", stdin);
    #endif
      scanf("%d %d", &n, &k);
      for (int i = 1; i <= n; i++) {
        scanf("%s", c + 1);
        len[i] = strlen(c + 1);
        bg[i] = tot + 1;
        for (int j = 1; j <= len[i]; j++) {
          s[++tot] = c[j];
          bel[tot] = i;
        }
        ed[i] = tot;
        s[++tot] = '#';
      }
      Sa.build(tot, s);
      memset(rpos, 0x3f, sizeof(rpos)); //*****
      int l = 1, r = 0, cnt = 0;
      for (; r <= tot; r++) {
        if (!bel[Sa.sa[r]]) continue; 
        ++num[bel[Sa.sa[r]]];
        if (num[bel[Sa.sa[r]]] == 1) ++cnt;
        if (cnt >= k) {
          for (; l <= r; l++) {
            if (!bel[Sa.sa[l]]) continue;
            if (cnt >= k) rpos[l] = r;
            else break;
            if (num[bel[Sa.sa[l]]] == 1) --cnt;
            --num[bel[Sa.sa[l]]];
          }
        } 
      }
      for (int i = 1; i <= n; i++) {
        ll ans = 0; int cur = 0;
        for (int j = bg[i]; j <= ed[i]; j++) {
          cur = max(cur - 1, 0);
          while (j + cur <= ed[i] && check(j, cur)) ++cur;
          ans += cur;
        }
        printf("%lld ", ans);
      }
      return 0;
    }
    
  • 相关阅读:
    [WPF系列]基础学习(一) WPF是什么?
    [WPF系列]从基础起步学习系列计划
    [WPF系列]Adorner应用-自定义控件ImageHotSpot
    Windows Phone 8 开发必备资源
    WPF:数据和行为
    WPF触发器(Trigger)
    【WPF】Silverlight中的Action与Trigger
    【WPF】 Behavior
    Struts2 拦截器配置及使用
    在新建FileInputStream时使用当前相对路径或者绝对路径作为参数的问题
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/10073765.html
Copyright © 2011-2022 走看看