Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com
And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Wednesday, October 15, 2014

DISUBSTR - Distinct Substrings

Distinct Substrings

Given below code is for disubstr spoj or distinct substring spoj

Hint :-> suffix array + LCP you can read both from e-maxx .


#include <bits/stdc++.h>
using namespace std;
#define MAX 50009
#define PB push_back
#define MP make_pair
int rank[20][MAX];
struct tuple
{
    int pos;
    int firstHalf,secondHalf;
};
bool compare(const tuple &a , const tuple &b)
{
    return a.firstHalf == b.firstHalf ?a.secondHalf < b.secondHalf :a.firstHalf < b.firstHalf;
}
int * suffix_array(tuple t[] , char s[], int length )
{
    int pos = 0;
    int *arr = (int*)calloc(length+9,sizeof(int));
    for(int i=0;i<length;i++)
        rank[0][i] = s[i] - 'A';
    for(int cnt = 1,stp = 1;(cnt>>1) < length ; cnt<<=1,stp++)
    {
        for(int i = 0;i<length ; i++)
        {
            t[i].firstHalf = rank[stp-1][i];
            t[i].secondHalf = i+cnt < length ? rank[stp-1][i+cnt] : -1;
            t[i].pos = i;
        }
        sort(t,t+length,compare);
        int rnk = 0;
        for(int i = 0 ; i<length ; i++)
        {
            if((i > 0) && (t[i-1].firstHalf == t[i].firstHalf && t[i].secondHalf == t[i-1].secondHalf))
                rnk = rank[stp][t[i-1].pos];
            else
                rnk = i;
            rank[stp][t[i].pos] = rnk;
        }
    }
    pos = ceil(log(length)/log(2));
    for(int i=0;i<length;i++)
        arr[rank[pos][i]] = i;
    return arr;
}
int LCP(int i,int j,int n)
{
    int res = 0;
    if(i==j)
        return n - i;
    for(int stp = ceil(log(n)/log(2)) ; stp>=0 && i < n && j < n ; stp--)
        if(rank[stp][i] == rank[stp][j])
        {
            res += 1<<stp;
            i += 1<<stp;
            j += 1<<stp;
        }
    return res;
}
int LCParray(char s[],int p[],int n)
{
    int sum = 0;
    for(int i = 1 ; i < n ; i++)
        sum += (LCP(p[i-1],p[i],n));
    return sum;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char s[50009];
        scanf("%s",s);
        int n = strlen(s);
        tuple t[n + 9];
        int *p;
        p = suffix_array(t,s,n);
        long long lcp_sum = 0,suffix_sum = 0;
        lcp_sum = LCParray(s,p,n);
        for(int i=0;i<n;i++)
            suffix_sum += p[i];
        printf("%lld\n",(long long)n*n - lcp_sum - suffix_sum);
    }
    return 0;
}