zoukankan      html  css  js  c++  java
  • [csu/coj 1632]LCP

    题意:求一个串的出现次数超过1次的字串的个数

    思路:对于一个后缀,出现在它后面的所有后缀与它的LCP的最大值就是应该增加的答案,当然这里没有考虑去重,但是却转化了问题,使得我们可以用最长公共前缀来统计答案。假设我们将每一个后缀按字典序排好,那么对于每一个后缀,与其它后缀的LCP的最大值其实就是与它相邻的两个的lcp的较大值,这不就是height数组了么?考虑去重的问题,如果height[i]>height[i-1],那么对于rank为i和i-1的最长公共前缀,它的前height[i-1]个前缀已经统计过了,答案只需要加上height[i]-height[i-1],如果height[i]<=height[i-1]则不需处理。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2  
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21  
     22 using namespace std;
     23  
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define mem_1(a) memset(a, -1, sizeof(a))
     26 #define lson l, m, rt << 1
     27 #define rson m + 1, r, rt << 1 | 1
     28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     32 #define all(a) (a).begin(), (a).end()
     33 #define lowbit(x) ((x) & (-(x)))
     34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     37 #define pchr(a) putchar(a)
     38 #define pstr(a) printf("%s", a)
     39 #define sstr(a) scanf("%s", a)
     40 #define sint(a) scanf("%d", &a)
     41 #define sint2(a, b) scanf("%d%d", &a, &b)
     42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     43 #define pint(a) printf("%d
    ", a)
     44 #define test_print1(a) cout << "var1 = " << a << endl
     45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     47 #define mp(a, b) make_pair(a, b)
     48 #define pb(a) push_back(a)
     49  
     50 typedef unsigned int uint;
     51 typedef long long LL;
     52 typedef pair<int, int> pii;
     53 typedef vector<int> vi;
     54  
     55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     57 const int maxn = 120000;
     58 const int md = 1000000007;
     59 const int inf = 1e9 + 7;
     60 const LL inf_L = 1e18 + 7;
     61 const double pi = acos(-1.0);
     62 const double eps = 1e-6;
     63  
     64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     67 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     69 int make_id(int x, int y, int n) { return x * n + y; }
     70  
     71 /// 构造后缀数组的之前,需要在原串末尾加个空字符(比其它字符都小即可),
     72 ///把这个空字符看成原串的一部分(这样在比较的时候到这个位置一定可以分个大小),
     73 ///所以n应该为原序列长度+1,后缀n-1是"空串",sa[0]总是n-1。
     74 struct SuffixArray {
     75     int n;
     76     int arr[6][maxn];
     77      int *sa, *x, *y, *c, *rnk, *height;
     78     SuffixArray() { sa = arr[0]; x = arr[1]; y = arr[2]; c = arr[3]; rnk = arr[4]; height = arr[5]; }
     79     void resize(int nn) {  n = nn; mem0(arr[0]); }
     80     void build_sa(int s[], int m) { // m is biger than the max value of char
     81         rep_up0(i, m) c[i] = 0;
     82         rep_up0(i, n) c[x[i] = s[i]]++;
     83         rep_up1(i, m - 1) c[i] += c[i - 1];
     84         rep_down0(i, n) sa[--c[x[i]]] = i;
     85         for (int k = 1; k <= n; k <<= 1) {
     86             int p = 0;
     87             for (int i = n - k; i < n; i++) y[p++] = i;
     88             rep_up0(i, n) if (sa[i] >= k) y[p++] = sa[i] - k;
     89             rep_up0(i, m) c[i] = 0;
     90             rep_up0(i, n) c[x[y[i]]]++;
     91             rep_up0(i, m) c[i] += c[i - 1];
     92             rep_down0(i, n) sa[--c[x[y[i]]]] = y[i];
     93             swap(x, y);
     94             p = 1; x[sa[0]] = 0;
     95             for (int i = 1; i < n; i++) {
     96                 x[sa[i]] = y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]? p - 1 : p++;
     97             }
     98             if (p >= n) break;
     99             m = p;
    100         }
    101     }
    102     void build_height(int s[]) {
    103         mem0(height);
    104         int k = 0;
    105         rep_up0(i, n) rnk[sa[i]] = i;
    106         rep_up0(i, n) {
    107             if (k) k--;
    108             int j = sa[rnk[i] - 1];
    109             while (s[i + k] == s[j + k]) k++;
    110             height[rnk[i]] = k;
    111         }
    112     }
    113     int solve(int n) {
    114         int ans = 0;
    115         for (int i = 2; i <= n; i ++) {
    116             if (height[i] > height[i - 1]) ans += height[i] - height[i - 1];
    117         }
    118         return ans;
    119     }
    120 };
    121 char s[maxn];
    122 int ss[maxn];
    123 SuffixArray sa;
    124 int main() {
    125     //freopen("in.txt", "r", stdin);
    126     int T;
    127     cin >> T;
    128     while (T --) {
    129         scanf("%s", s);
    130         int len = strlen(s) + 1;
    131         rep_up0(i, len) ss[i] = s[i];
    132         sa.resize(len);
    133         sa.build_sa(ss, 128);
    134         sa.build_height(ss);
    135         cout << sa.solve(len - 1) << endl;
    136     }
    137     return 0;
    138 }
    View Code

    ps: 这个代码能够正常工作,但是里面的ST表与“max”有关的部分有问题,慎用。

  • 相关阅读:
    Redis 基础、高级特性与性能调优
    CentOS 7安装新版RabbitMQ解决Erlang 19.3版本依赖
    Centos7中docker开启远程访问
    基于Docker+Jenkins+Gitlab搭建持续集成环境
    Gitlab利用Webhook实现Push代码后的jenkins自动构建
    在jenkins中调用maven的变量
    docker中gitlab-runner配置
    搭建docker私有仓库
    ku8eye 安装概览
    配置docker官方源并用yum安装docker
  • 原文地址:https://www.cnblogs.com/jklongint/p/4526506.html
Copyright © 2011-2022 走看看