spoj694--Distinct Substrings

个人第一道后缀数组题目。
对于每一个后缀suffix(i),都有len-sa[i]个前缀(也即有len-sa[i]个不同的字符串),其中与排名前一位的后缀有height[i]个共同的前缀,最后所得到的新的字符串个数为len-sa[i]-height[i].因此这题只要求出sa以及height即可求得答案。

#include<stdio.h>
#include<string.h>
 #define maxn 100010
 int wa[maxn];
 int wb[maxn];
 int wv[maxn];
 int wsa[maxn];
 int ranks[maxn],height[maxn];

 void calheight(int *r,int *sa,int n)
 {
      int i,j,k=0;
      for(i=1;i<=n;i++) ranks[sa[i]]=i;
      for(i=0;i<n ;height[ranks[i++]]=k)
      for(k?k--:0,j=sa[ranks[i]-1];r[i+k]==r[j+k];k++);
      return;
 }

 int cmp(int *r,int a,int b,int l)
 {
     return r[a]==r[b]&&r[a+l]==r[b+l];
 };

 void da(int *r,int *sa,int n,int m)
 {
      int i,j,p,*x=wa,*y=wb,*t;
      for(i=0;i<m;i++) wsa[i]=0;
      for(i=0;i<n;i++) wsa[x[i]=r[i]]++;
      for(i=1;i<m;i++) wsa[i]+=wsa[i-1];
      for(i=n-1;i>=0;i--) sa[--wsa[x[i]]]=i;
      for(j=1,p=1;p<n;j*=2,m=p)
      {
       for(p=0,i=n-j;i<n;i++) y[p++]=i;
       for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
       for(i=0;i<n;i++) wv[i]=x[y[i]];
       for(i=0;i<m;i++) wsa[i]=0;
       for(i=0;i<n;i++) wsa[wv[i]]++;
       for(i=1;i<m;i++) wsa[i]+=wsa[i-1];
       for(i=n-1;i>=0;i--) sa[--wsa[wv[i]]]=y[i];
       for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
       x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
       }
       return;
 }

 int r[maxn];
 int sa[maxn];
 int main()
 {
     char str[maxn];
     int t,i;
     scanf("%d",&t);
     while(t--)
     {
         scanf("%s",str);
         memset(r, 0 ,sizeof(r));
         memset(sa, 0, sizeof(sa));
         int len = strlen(str);
         for (i = 0 ; i < len; ++i)
         {
             r[i] = (int)str[i];
         }
         da(r, sa, len + 1 , 150);
         calheight(r, sa, len);
         int sum = 0;
         for(i=0;i<len;i++)
             sum += len-sa[i+1]-height[i+1];
         printf("%d\n",sum);
     }
     return 0;
 }

spoj694--Distinct Substrings

时间: 2024-11-12 01:59:17

spoj694--Distinct Substrings的相关文章

SPOJ694&amp;&amp;SPOJ705:Distinct Substrings(后缀数组)

Description Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the numb

SPOJ694--- DISUBSTR - Distinct Substrings(后缀数组)

Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the number of distin

SPOJ 题目694 Distinct Substrings(后缀数组,求不同的子串个数)

DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output

SPOJ 705 New Distinct Substrings

New Distinct Substrings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: SUBST164-bit integer IO format: %lld      Java class name: Main Given a string, we need to find the total number of its distinct subst

SPOJ - DISUBSTR Distinct Substrings (不相同的子串的个数)

Distinct Substrings  Time Limit: 159MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20;Each test case consists of

SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)

DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output

SPOJ 694. Distinct Substrings,705. New Distinct Substrings(后缀数组)

题目大意:给定长度为N的字符串,求出其中不相同子串的个数. 解题思路:每一个字串一定是某个后缀的前缀,那么原问题就可以等价于求所有后缀之间的不相同的前缀的个数.如果所有的后缀按照suffix(sa[1]),suffix(sa[2])--suffix(sa[n])的顺序计算,我们会发现对于每个新加进来的后缀suffix(sa[k]),它将产生n-sa[k]+1个新的前缀.但是其中有leight[k]个是和前面的字符串的前缀是相同的.所以suffix(sa[k])加进来增加的不同的子串的个数为n-s

SPOJ 题目705 New Distinct Substrings(后缀数组,求不同的子串个数)

SUBST1 - New Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 50000 Output For each test case out

SPOJ 694、705 Distinct Substrings 、 New Distinct Substrings (后缀数组)

题目大意: 求串中不同的子串的个数. 思路分析: 子串一定是某一个后缀的前缀. 所以我们把每一个后缀拿出来,分析它有多少个前缀,然后除去它与sa数组中前面那个后缀相同的前缀. 最后也就是 ans = segma (n-sa[i] + height[i]).... #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #define maxn 1000005

SPOJ705 Distinct Substrings (后缀自动机&amp;后缀数组)

Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20;Each test case consists of one string, whose length is <= 1000 Output For each test case output one number saying the number of distinc