POJ 3415

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 200000 + 10;

char s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
//s:(0 ~ m-1)
void build_sa(int m)
{
    int *x = t, *y = t2;

    for(int i = 0; i < m; i++) c[i] = 0;
    for(int i = 0; i < n; i++) c[x[i] = s[i]]++;
    for(int i = 1; i < m; i++) c[i] += c[i-1];
    for(int i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;

    for(int k = 1; k <= n; k <<= 1){
        int p = 0;

        for(int i = n - k; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;

        for(int i = 0; i < m; i++) c[i] = 0;
        for(int i = 0; i < n; i++) c[x[y[i]]]++;
        for(int i = 0; i < m; i++) c[i] += c[i-1];
        for(int i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];

        swap(x, y);
        p = 1; x[sa[0]] = 0;
        for(int i = 1; i < n; i++){
            x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
        }
        if(p >= n) break;
        m = p;
    }
}

int m; //length of MSC
int cmp_suffix(char* pattern, int p){
    return strncmp(pattern, s + sa[p], m);
}

int Rank[maxn], height[maxn];
void getHeight()
{
    int k = 0;
    for(int i = 0; i < n; i++) Rank[sa[i]] = i;
    for(int i = 0; i < n; i++){
        if(k) k--;
        int j = sa[Rank[i]-1];
        while(s[i+k] == s[j+k]) k++;
        height[Rank[i]] = k;
    }
}

int main()
{
    int k;
    while(scanf("%d", &k) != EOF && k){
        char a[maxn], b[maxn];
        n = 0;
        memset(height, 0, sizeof(height));
        scanf("%s%s", a, b);
        int lena = strlen(a), lenb = strlen(b);
        for(int i = 0; i < lena; i++) s[n++] = a[i]; s[n++] = '#';
        for(int i = 0; i < lenb; i++) s[n++] = b[i]; s[n++] = '$';
        build_sa(128);
        getHeight();

        long long tot = 0;
        long long sum = 0;
        int top = 0;
        int rec[maxn][2];
        for(int i = 1; i <= n; i++){
            if(height[i] < k) tot = top = 0;
            else{
                int cnt = 0;
                if(sa[i-1] < lena){
                    cnt++;
                    tot += height[i] - k + 1;
                }
                while(top > 0 && height[i] <= rec[top-1][0]){
                    top--;
                    tot -= (rec[top][0] - height[i])*rec[top][1];
                    cnt += rec[top][1];
                }
                rec[top][0] = height[i];
                rec[top++][1] = cnt;
                if(sa[i] > lena) sum += tot;
            }
        }
        tot = top = 0;
        for(int i = 1; i <= n; i++){
            if(height[i] < k) tot = top = 0;
            else{
                int cnt = 0;
                if(sa[i - 1] > lena){
                    cnt++;
                    tot += height[i] - k + 1;
                }
                while(top > 0 && height[i] <= rec[top - 1][0]){
                    top--;
                    tot -= (rec[top][0] - height[i])*rec[top][1];
                    cnt += rec[top][1];
                }
                rec[top][0] = height[i];
                rec[top++][1] = cnt;
                if(sa[i] < lena) sum += tot;
            }
        }
        printf("%I64d\n", sum);
    }
    return 0;
}
posted @ 2017-07-21 22:31  />.<\  阅读(136)  评论(0编辑  收藏  举报