Advertisement
Guest User

Karkkeinen

a guest
Mar 4th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.54 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<stdbool.h>
  5. #include<assert.h>
  6. #include<time.h>
  7.  
  8. #define N (int)1e6+77
  9. int s[N];
  10. int n;
  11.  
  12. void input() {
  13.     char *sp = calloc(N,sizeof(char));
  14.     int nw = 0;
  15.     int i;
  16.     n = 1e5+2; // rozmiar slowa
  17.     for(i=0;i<n;i++) // losowanie slowa
  18.         sp[i] = rand()%26+'a';
  19.     n = strlen(sp);
  20.     int jest[150];
  21.     for(i=0;i<150;i++) {
  22.         jest[i] = 0;
  23.     }
  24.     for(i=0;i<n;i++) {
  25.         jest[sp[i]] = 1;
  26.     }
  27.  
  28.     int kolor[150];
  29.     int ile = 0;
  30.     for(i=0;i<150;i++) {
  31.         if(jest[i])
  32.             ile++;
  33.         kolor[i] = ile;
  34.     }
  35.     for(i=0;i<n;i++) {
  36.         s[i] = kolor[sp[i]];
  37.     }
  38.     free(sp);
  39. }
  40.    
  41. struct Vector {
  42.      void* *arr;
  43.      int size;
  44.      int capacity;
  45. };
  46.  
  47. typedef struct Vector Vector;
  48.  
  49. void VectorInit(Vector *x) {
  50.     assert(x!=NULL);
  51.     x->arr = NULL;
  52.     x->size = 0;
  53.     x->capacity = 0;
  54. }
  55.  
  56. void VectorPushBack(Vector *x, void *el) {
  57.     assert(x!=NULL);
  58.     if(x->size + 1 > x->capacity) {
  59.         if(x->capacity > 0) {
  60.             x->arr = realloc(x->arr, x->capacity*2*sizeof(void*));
  61.             x->capacity*=2;
  62.         }
  63.         else {
  64.             x->arr = calloc(1,sizeof(void*));
  65.             x->capacity=1;
  66.         }
  67.     }
  68.     x->arr[x->size] = el;
  69.     x->size++;
  70. }
  71.  
  72. void VectorClear(Vector *x) {
  73.     assert(x!=NULL);
  74.     if(x->capacity > 0)
  75.         free(x->arr);
  76.     x->size = 0;
  77.     x->capacity = 0;
  78. }
  79.  
  80. struct Triple {
  81.     int val[3];
  82.     int who;
  83. };
  84.  
  85. typedef struct Triple Triple;
  86.  
  87. Triple new_Triple(int a, int b, int c, int d) {
  88.     Triple k;
  89.     k.val[0] = a;
  90.     k.val[1] = b;
  91.     k.val[2] = c;
  92.     k.who = d;
  93.     return k;
  94. }
  95.  
  96. inline bool rozne(Triple *x, Triple *y) {
  97.     int i=0;
  98.     for(i=0;i<3;i++) {
  99.         if(x->val[i] != y->val[i])
  100.             return true;
  101.     }
  102.     return false;
  103. }
  104.  
  105. Triple* sortTriples(Triple *box, int size, int range) { // w boxie sa wspolrzedne >= 0 i <= size
  106.     Vector vec[range];
  107.     int i;
  108.     for(i=0;i<range;i++)
  109.         VectorInit(&vec[i]);
  110.     void *wsk[size];
  111.     for(i=0;i<size;i++)
  112.         wsk[i] = &box[i];
  113.     void *wskc[size+4];
  114.     for(i=2;i>=0;i--) {
  115.         int j;
  116.         for(j=0;j<size;j++) {
  117.             VectorPushBack(&vec[((Triple*)wsk[j])->val[i]],wsk[j]);
  118.         }
  119.         int wskcsize = 0;
  120.         int k;
  121.         for(j=0;j<range;j++) {
  122.             for(k=0;k<vec[j].size;k++) {
  123.                 wskc[wskcsize++] = vec[j].arr[k];
  124.             }
  125.         }
  126.         for(j=0;j<wskcsize;j++) {
  127.             wsk[j] = wskc[j];
  128.         }
  129.         for(j=0;j<range;j++) {
  130.             VectorClear(&vec[j]);
  131.         }
  132.     }
  133.     Triple *res = calloc(size,sizeof(Triple));
  134.     for(i=0;i<size;i++) {
  135.         res[i] = *((Triple*)wsk[i]);
  136.     }
  137.     free(box);
  138.     return res;
  139. }
  140.  
  141. int compress(int *l, int **nowaw, int roz, int *ilep, bool* dorzucono) {
  142.     int *nowa;
  143.     int *gdzie = calloc(roz,sizeof(int));
  144.     Triple *worek = calloc(roz,sizeof(Triple));
  145.     int size = 0;
  146.     int i;
  147.     *ilep = 0;
  148.     *dorzucono = false;
  149.     // mod 3 = 1
  150.     for(i=1;i+2<roz;i+=3) {
  151.         worek[size++] = new_Triple(l[i],l[i+1],l[i+2],i);
  152.         (*ilep)++;
  153.     }
  154.     if(i==roz-2) {
  155.         worek[size++] = new_Triple(l[roz-2],l[roz-1],0,roz-2);
  156.         (*ilep)++;
  157.     } else if(i == roz-1) {
  158.         worek[size++] = new_Triple(l[roz-1],0,0,roz-1);
  159.         (*ilep)++;
  160.     } else {
  161.         *dorzucono = true;
  162.     }
  163.     // mod 3 = 2
  164.     for(i=2;i+2<roz;i+=3) {
  165.         worek[size++] = new_Triple(l[i],l[i+1],l[i+2],i);
  166.     }
  167.     if(i==roz-2) {
  168.         worek[size++] = new_Triple(l[roz-2],l[roz-1],0,roz-2);
  169.     } else if(i == roz-1) {
  170.         worek[size++] = new_Triple(l[roz-1],0,0,roz-1);
  171.     }
  172.    
  173.     Triple *temp = sortTriples(worek,size,roz+1);
  174.     worek = temp;
  175.    
  176.     int nic = 1;
  177.     for(i=0;i<size;i++) {
  178.         if(i>0 && rozne(&worek[i],&worek[i-1])) {
  179.             nic++;
  180.         }
  181.  
  182.         gdzie[worek[i].who] = nic;
  183.     }
  184.  
  185.     int nowasize = 0;
  186.     nowa = calloc(size+1,sizeof(int));
  187.     *nowaw = nowa;
  188.     // mod 3 = 1
  189.     for(i=1;i+2<roz;i+=3) {
  190.         nowa[nowasize++] = gdzie[i];
  191.     }
  192.     if(i==roz-2) {
  193.         nowa[nowasize++] = gdzie[roz-2];
  194.     } else if(i == roz-1) {
  195.         nowa[nowasize++] = gdzie[roz-1];
  196.     }
  197.  
  198.     // dodawanie $
  199.     if(*dorzucono)
  200.         nowa[nowasize++] = 0;
  201.  
  202.     // mod 3 = 2
  203.     for(i=2;i+2<roz;i+=3) {
  204.         nowa[nowasize++] = gdzie[i];
  205.     }
  206.     if(i==roz-2) {
  207.         nowa[nowasize++] = gdzie[roz-2];
  208.     } else if(i == roz-1) {
  209.         nowa[nowasize++] = gdzie[roz-1];
  210.     }
  211.     free(gdzie);
  212.     free(worek);
  213.     return nowasize;
  214. }
  215.  
  216. struct Pair {
  217.     int a[2];
  218. };
  219.  
  220. typedef struct Pair Pair;
  221.  
  222. Pair newPair(int d, int e) {
  223.     Pair x;
  224.     x.a[0] = d;
  225.     x.a[1] = e;
  226.     return x;
  227. }
  228.  
  229.  
  230. int* count_sa(int *l, int roz) {
  231.     int i;
  232.     if(roz==1) {
  233.         int *x = calloc(1,sizeof(int));
  234.         x[0] = 0;
  235.         return x;
  236.     }
  237.     int *nowa;
  238.     int ilep;
  239.     bool dorzucono;
  240.     int dorz;
  241.     int nowasize = compress(l,&nowa,roz,&ilep,&dorzucono);
  242.     if(dorzucono)
  243.         dorz = 1;
  244.     else
  245.         dorz = 0;
  246.     int *sa_old = count_sa(nowa,nowasize); // tablica Sufiksowa dla 1 i 2 mod 3
  247.     int *rank = calloc(roz+1,sizeof(int)); // tablica RANK
  248.     int lol = 1;
  249.     for(i=0;i<nowasize;i++) {
  250.         if(sa_old[i] < ilep)
  251.             rank[sa_old[i]*3+1] = lol++;
  252.         else  if(sa_old[i] > ilep || (sa_old[i] == ilep && !dorzucono)) {
  253.             rank[(sa_old[i]-ilep-dorz)*3+2] = lol++;
  254.         }
  255.     }
  256.    
  257.     free(nowa);
  258.     int wsk = 1;
  259.  
  260.     Pair pary[roz/3+6]; // pary gosci podzielnych przez 3
  261.     for(i=0;i<roz;i+=3) {
  262.         if(i+1<roz)
  263.             pary[i/3] = newPair(l[i],rank[i+1]);
  264.         else
  265.             pary[i/3] = newPair(l[i],0);
  266.     }
  267.    
  268.     int j;
  269.     Vector wor;
  270.     VectorInit(&wor);
  271.     for(i=0;i<roz;i+=3) {
  272.         VectorPushBack(&wor,(void*)&pary[i/3]);
  273.     }
  274.     Vector buckets[roz+5];
  275.     for(j=roz+4;j>=0;j--)
  276.         VectorInit(&buckets[j]);
  277.     int t;
  278.     for(j=1;j>=0;j--) {
  279.         for(t=0;t<wor.size;t++) {
  280.             VectorPushBack(&buckets[((Pair*)wor.arr[t])->a[j]],wor.arr[t]);
  281.         }
  282.         void* wskaznik[wor.size+1];
  283.         int wskazniksize = 0;
  284.         for(t=0;t<=roz+4;t++) {
  285.             for(i=0;i<buckets[t].size;i++) {
  286.                 wskaznik[wskazniksize++] = buckets[t].arr[i];
  287.             }
  288.         }
  289.         for(t=0;t<wskazniksize;t++) {
  290.             wor.arr[t] = wskaznik[t];
  291.         }
  292.         for(t=0;t<roz+5;t++)
  293.             VectorClear(&buckets[t]);
  294.     }
  295.     int* zerowe  = calloc(roz/3+4,sizeof(int));
  296.     int zerowesize = 0;
  297.     for(i=0;i<wor.size;i++) {
  298.         zerowe[zerowesize++] = (((Pair*)wor.arr[i])-pary)*3;
  299.     }
  300.     VectorClear(&wor);
  301.     // zmerguj
  302.     int* saRes = calloc(roz,sizeof(int));
  303.     int saRessize = 0;
  304.     int* niezerowe = calloc(roz/3*2+4,sizeof(int));
  305.     int  niezerowesize = 0;
  306.     wsk = 0;
  307.     for(i=0;i<nowasize;i++) {
  308.         if(!(dorzucono && sa_old[i] == ilep)) {
  309.             if(sa_old[i] < ilep) {
  310.                 niezerowe[niezerowesize++] = sa_old[i]*3+1;
  311.             } else {
  312.                 niezerowe[niezerowesize++] = (sa_old[i]-ilep-dorz)*3+2;
  313.             }
  314.         }
  315.         wsk++; 
  316.     }
  317.     free(sa_old);
  318.     i = 0, j = 0;
  319.     while( i < zerowesize || j < niezerowesize) {
  320.         if( j == niezerowesize) {
  321.             saRes[saRessize++] = zerowe[i++];
  322.         } else if( i == zerowesize) {
  323.             saRes[saRessize++] = niezerowe[j++];
  324.         } else {
  325.             if(l[niezerowe[j]] == l[zerowe[i]]) {
  326.                 if(niezerowe[j]%3==1) {
  327.                     if(rank[niezerowe[j]+1] < rank[zerowe[i]+1])
  328.                         saRes[saRessize++] = niezerowe[j++];
  329.                     else
  330.                         saRes[saRessize++] = zerowe[i++];
  331.                 } else {
  332.                     if(niezerowe[j]+1 >= roz) {
  333.                         saRes[saRessize++] = niezerowe[j++];
  334.                     } else if(zerowe[i]+1 >= roz) {
  335.                         saRes[saRessize++] = zerowe[i++];
  336.                     } else {
  337.                         if(l[niezerowe[j]+1] == l[zerowe[i]+1]) {
  338.                             if(rank[niezerowe[j]+2] < rank[zerowe[i]+2])
  339.                                 saRes[saRessize++] = niezerowe[j++];
  340.                             else
  341.                                 saRes[saRessize++] = zerowe[i++];
  342.                         }  else {
  343.                             if(l[niezerowe[j]+1] < l[zerowe[i]+1])
  344.                                 saRes[saRessize++] = niezerowe[j++];
  345.                             else
  346.                                 saRes[saRessize++] = zerowe[i++];
  347.                         }
  348.                     }
  349.                 }
  350.             }
  351.             else {
  352.                 if(l[niezerowe[j]] < l[zerowe[i]]) {
  353.                     saRes[saRessize++] = niezerowe[j++];   
  354.                 } else {
  355.                     saRes[saRessize++] = zerowe[i++];
  356.                 }
  357.             }
  358.         }
  359.     }
  360.     free(zerowe);
  361.     free(niezerowe);
  362.     free(rank);
  363.  
  364.     return saRes;
  365. }
  366.  
  367.  
  368. int main () {
  369.     input();
  370.     int *res = count_sa(s,n);
  371.    
  372.     //printf("SA : \n");
  373.     int i;
  374.     for(i=0;i<n;i++) {
  375.         printf("%d ", res[i]);
  376.     }
  377.     free(res);
  378.     return 0;
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement