Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the meaning of 'S[x[i]]' in the following algorithm program, i understand how '&','<<' and all other code of program, but not sure about 'S[x[i]]', because in it x[i] clearly does not refer to a index position for array S, because it would not make sense as in my example, x[0] = 97/'a' and my S array has a size limit of max 32.
C++
int preSo(char *x, int m, unsigned int *S) { 
    unsigned int j, lim; 
    int i; 
    for (i = 0; i < ASIZE; ++i) 
      S[i] = ~0; 
    for (lim = i = 0, j = 1; i < m; ++i, j <<= 1) { 
      S[x[i]] &= ~j; 
      lim |= j; 
    } 
    lim = ~(lim>>1); 
    return(lim); 
  } 

void SO(char *x, int m, char *y, int n) { 
    unsigned int lim, state; 
    unsigned int S[ASIZE]; 
    int j; 
   /* if (m > WORD) 
      error("SO: Use pattern size <= word size"); */

    /* Preprocessing */ 
    lim = preSo(x, m, S); 

    /* Searching */ 
    for (state = ~0, j = 0; j < n; ++j) { 
      state = (state<<1) | S[y[j]]; 
      if (state < lim) 
        OUTPUT(j - m + 1); 
    } 
  } 

void OUTPUT(int n)
{
	printf("%d",n);
}

void main()
{
	char * pattern="abg";
	char * text ="abg and ade ac are not same as ad";
	SO(pattern,3,text,28);
	getch();

}


i am now adding my test result for value of S[x[0]], x[0] = 97 ,S[x[0]] = 3435973836, value of S[97] = 3341248 where is this value coming from.
Posted
Updated 1-Jun-15 20:29pm
v3
Comments
barneyman 2-Jun-15 2:03am    
you're correct - it means the S[97] (where 97 is gleaned from x[0])
RajneeshSaysHello 2-Jun-15 2:27am    
please see test results
barneyman 2-Jun-15 2:38am    
there's nothing special about the code S[x[0]] - it's original value is 0xCCCCCCCC (which is stack 'undefined' value in debug) - i don't know where you're getting 0x32FBC0 from

S is initialised to -1 in preSo, and then there's some XORing going on ...

Debug it ...
RajneeshSaysHello 2-Jun-15 2:57am    
i did and showed different value's of S[..] with xor and without xor.
Sergey Alexandrovich Kryukov 2-Jun-15 2:06am    
There is nothing worse than trying to explain how something work in a program that you did not write and I would never try to write. This is just a waste of time. Start writing your own code; then we will gladly help you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900