Advertisement
jaskaran_1

substring_search

Dec 27th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int MINI(char *p,int hash1[],int hash2[],int count)
  4. {int count1=0,count2=0;//count2 will keep a count of the unique characters encountered in scan matching with the count
  5. while(*p!='\0')
  6. {if(hash2[*p-' ']==1)
  7. {if(hash1[*p-' ']==0)
  8.  {hash1[*p-' ']=1;
  9.   count2++;//count of unique characters increases
  10.   }}
  11.  count1++;//the total characters read which were present in str2
  12.  if(count2==count)//all characters  
  13.   return count1;
  14.  p++;
  15.  }
  16.  }
  17. int main()
  18. {int hash1[95],hash2[95];
  19. char str1[100],str2[100];
  20. //gets(str1);
  21. scanf("%[^\n]s",str1);
  22. getchar();
  23. scanf("%[^\n]s",str2);
  24. //gets(str2);
  25. int I;
  26. for(I=0;I<95;I++)
  27. {hash1[I]=0;
  28. hash2[I]=0;}
  29. char *p=str2;
  30. int count=0;//count contains number of unique characters in str2
  31. while(*p!='\0')
  32. {if(hash2[*p-' ']==0)
  33.  {hash2[*p-' ']=1;
  34.   count++;}
  35.   p++;}//loop for hashing the characters of str2
  36. int min=strlen(str1);
  37. p=str1;
  38. int k;
  39. while(*p!='\0')
  40. {k=MINI(p,hash1,hash2,count);
  41.  if(min>k)
  42.  min=k;
  43. for(I=0;I<95;I++)
  44. hash1[I]=0;//hash table reset
  45. p++;
  46.  }
  47.  printf("%d",min);
  48.  return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement