semenrbt

Метод сиськи

May 13th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <malloc.h>
  4.  
  5. int l[500];
  6. int nods[500];
  7. int nl=0;
  8.  
  9.  
  10. int nod(int a,int b)
  11. {
  12.     int i;
  13.     for (i=a;i>1;--i)
  14.         if ((a%i==0)&&(b%i==0)) return i;
  15.     return 1;
  16. }
  17.  
  18.  
  19.  
  20. int main()
  21. {    
  22.     char *s=(char *)calloc(1,sizeof(char));
  23.     char fname[100];
  24.     char c;
  25.     unsigned int n=0,i,j,keylen;
  26.     FILE *f;
  27.     printf("file name: ");
  28.     scanf("%s",fname);
  29.     f=fopen(fname,"r");
  30.     if (f==NULL){
  31.             printf("file not found\n");
  32.             return 1;
  33.     }
  34.    
  35.     for (i=0;i<500;++i)
  36.         nods[i]=0;
  37.  
  38.    
  39.     while (!feof(f))
  40.         if (fread(&c,1,1,f)){    
  41.             s[n++]=c;              
  42.             s=(char *)realloc(s,n+1);
  43.         }
  44.  
  45.     s[n]='\0';
  46.  
  47.     char str1[4],str2[4];
  48.    
  49.     for (i=0;i<strlen(s);++i)
  50.     {
  51.         str1[0]=s[i];
  52.         str1[1]=s[i+1];
  53.         str1[2]=s[i+2];
  54.         str1[3]='\0';
  55.        
  56.         for (j=i+1;j<strlen(s);++j)
  57.         {
  58.             str2[0]=s[j];
  59.             str2[1]=s[j+1];
  60.             str2[2]=s[j+2];
  61.             str2[3]='\0';
  62.            
  63.             if (!strcmp(str1,str2)) l[nl++]=j-i;
  64.            
  65.         }
  66.     }
  67.    
  68.     for (i=0;i<nl;++i)
  69.         for (j=i+1;j<nl;++j)
  70.             nods[nod(l[i],l[j])]++;
  71.  
  72.  
  73.     keylen=0;
  74.     for (i=2;i<500;++i)
  75.         if (nods[keylen]<nods[i]) keylen=i;
  76.        
  77.     printf("%d\n",keylen);
  78.    
  79.     free(s);
  80.     return 0;
  81. }
Add Comment
Please, Sign In to add comment