You are on page 1of 4

#include<stdio.

h>
#include<string.h>
#include<math.h>

int scompare(char ara1[],char ara2[])


{
int i=0;
while(1)
{
if(ara1[i]=='\0' && ara2[i]=='\0')
{
return 1;}
else if(ara1[i]=='\0')
{
return 3;}

else if(ara2[i]=='\0')
{
return 2;}

else if(ara1[i]>ara2[i])
{
return 2;}

else if(ara1[i]<ara2[i])
{
return 3;}
i++;
}
}

int slength(char ara[])


{
int c=0,i;
for(i=0;ara[i]!='\0';i++)
{
c++;
}
return c;
}

void scopy(char arra[],char s[])


{
int i;
for(i=0;s[i]!='\0';i++)
arra[i]=s[i];
arra[i]='\0';
}

char str[1000005];
char word[1000005][5];
char word2[1000005][5];
int serial[1000005];

void sorting(int l,int m,int r,int i,int j,int coun)


{
if(coun==r+1)
return;
if(i==m-l+1)//If the elements of the first half is finished with sorting
{
scopy(word[coun],word2[m+j+1]);
j++;
coun++;
}

else if(j==r-m)//if elements of the second half is finished with sorting


{

scopy(word[coun],word2[l+i]);
coun++;
i++;
}

else if( scompare(word2[l+i],word2[m+j+1])==3 ||


scompare(word2[l+i],word2[m+j+1])==1 )
{
scopy(word[coun],word2[l+i] );
coun++;
i++;
}

else if(scompare(word2[l+i],word2[m+j+1])==2)
{
scopy(word[coun],word2[m+j+1]);
coun++;
j++;
}
sorting(l,m,r,i,j,coun);
}

void merged(int l,int r)


{
if(l>=r)
return;
int m=(l+r)/2;//middle point m
merged(l,m);//Sorting first half
merged(m+1,r);//Sorting second half
sorting(l,m,r,0,0,l);//Merge two sorted half
//Will Copy elements of word array to word2 array from l to r
for(int j=l;j<=r;j++)
{
scopy(word2[j],word[j]);//copy elements of word array to word2 array from l
to r
}
}

int main()
{
scanf("%[^\n]s",str);
int i,j,c=0;
int n=slength(str);
if(n>=4)
{
word[c][0]=word2[c][0]=str[0];
word[c][1]=word2[c][1]=str[1];
word[c][2]=word2[c][2]=str[2];
word[c][3]=word2[c][3]=str[3];
word[c][4]=word2[c][4]='\0';

c++;
}

for(i=4; i<n;i++)
{
word[c][0]=word2[c][0]=word[c-1][1];
word[c][1]=word2[c][1]=word[c-1][2];
word[c][2]=word2[c][2]=word[c-1][3];
word[c][3]=word2[c][3]=str[i];
word[c][4]=word2[c][4]='\0';

c++;

int l=0,r=c-1;
merged(l,r);
//if length of str is less than 4,c will be 0;
if(c==0)
{
printf("\n");
}

else
{
int maxc=1;

int cnt=1;//cnt is the frequency of any string

int f1=0;// f1 is the frequency of the string that occurs the highest time

serial[f1]=0;//serial keeps the index of the strings that occur the highest
time

for(i=0;i<c-1;i=i+1)
{
if(scompare(word[i],word[i+1])==1)
{
cnt++;
}

else

{
if(cnt==maxc)
{
serial[f1]=i;
f1++;
}
else if(cnt>maxc)
{
maxc=cnt;
serial[0]=i;
f1=1;
}

cnt=1;

}
}
//check for the last of the loop which was not checked
if(cnt==maxc)
{
serial[f1]=i;
f1++;
cnt=1;
}

else if(cnt>maxc)
{
maxc=cnt;
cnt=1;
serial[0]=i;
f1=1;
}

for(i=0;i<f1;i=i+1)
{
printf("%s\n",word[ serial[i] ]);
}
}
return 0;
}

You might also like