Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. void search(unsigned char *text,unsigned char *sample,int symbol, int *stopsymboltable,int i,int m,int *k,FILE *fout,int str)
  5. {
  6.     int j,k1;
  7.     do
  8.     {
  9.         j=str;
  10.         k1=i;
  11.         while ( (sample[j] == text[k1-1]) && (j >= 0) )
  12.         {
  13.             fprintf(fout,"%d ",k1+m);
  14.             k1--;
  15.             j--;
  16.         }
  17.         if ( (sample[j] != text[k1-1]) && (k1+1 < symbol) && (j >= 0) && (text[k1-1]>=0) )
  18.         fprintf(fout,"%d ",k1+m);
  19.         i=i+stopsymboltable[text[i-1]];
  20.     } while ( i < symbol);
  21.     *k=k1;
  22. }
  23. int clear(unsigned char *text,int k)
  24. {
  25.     int i=0;
  26.     while(k<=2046)
  27.     {
  28.         text[i]=text[k];
  29.         k++;
  30.         i++;
  31.     }
  32.     return i;
  33. }
  34.  
  35. void open(unsigned char *sample, unsigned char *text,int *stopsymboltable,int i,FILE *fout,FILE *fin,int str)
  36. {
  37.     int t=0,m=0,k;
  38.     while(!feof(fin))
  39.     {
  40.         text[t]=fgetc(fin);
  41.         if (t==2046)
  42.         {
  43.             search(text,sample,t,stopsymboltable,i,m,&k,fout, str);
  44.             m=m+k;
  45.             t=clear(text,k)-1;
  46.         }
  47.         t++;
  48.     }
  49.     search(text,sample,t ,stopsymboltable,i,m,&k,fout,str);
  50. }
  51. void stopsymbol(unsigned char *sample,int *stopsymboltable,int *i)
  52. {
  53.     int i1,s;
  54.     s=strlen((char*)sample);
  55.     for(i1=0;i1<256;i1++)
  56.     stopsymboltable[i1]=s;
  57.     for(i1=0;i1<s-1;i1++)
  58.     stopsymboltable[sample[i1]]=s-i1-1;
  59.     *i=s;
  60. }
  61.  
  62. int main()
  63. {
  64.     FILE *fin,*fout;
  65.     fin=fopen("in.txt", "r");
  66.     fout=fopen("out.txt", "w");
  67.     if (fin==NULL) return -1;
  68.     if (fout==NULL) return -1;
  69.     unsigned char text[2064],sample[18]={0};
  70.     int stopsymboltable[256];
  71.     int i,str;
  72.     fgets((char*)sample,18,fin);
  73.     str=strlen((char*)sample)-1;
  74.     sample[str]='\0';
  75.     str=strlen((char*)sample)-1;
  76.     stopsymbol(sample,stopsymboltable,&i);
  77.     open(sample,text,stopsymboltable,i,fout,fin,str);
  78.     fclose(fin);
  79.     fclose(fout);
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement