Pagini recente » Cod sursa (job #321629) | Cod sursa (job #1899599) | Cod sursa (job #2732104) | Cod sursa (job #947170) | Cod sursa (job #28382)
Cod sursa(job #28382)
#include<fstream.h>
#include <stdlib.h>
#include<string.h>
long k,n;
char s[20000];
int caut(int x,int y)
{// cout<<endl<<endl;
char *ptr,*ptr_aux;
ptr=new char [n];
strcpy(ptr,s);
ptr=ptr+x;
memset(ptr+y,NULL,n-x-y);
ptr_aux=strstr(s,ptr)+1;
if (ptr_aux-s-1<x) return 0;
//
int contor=1;
while (ptr_aux=strstr(ptr_aux,ptr)) { ptr_aux++;
//ptr_aux=strstr(ptr_aux,ptr)+1;
contor++;
}
// cout<<"ptr aux "<<ptr_aux;
// strncpy(ptr,s,strlen(s));
//cout<<endl<<ptr<<" gasit de "<<contor<<" ori";
if (contor>=k) return 1; else return 0;
}
int main()
{
long poz,nr,i,j,lungime;
ifstream f("substr.in");
f>>n>>k;
f>>s;
f.close();
n=strlen(s);
for(lungime=n;lungime>=1;lungime--)
for(poz=0;poz<n-lungime;poz++)
if (caut(poz,lungime)) {
ofstream g("substr.out");
for(i=poz;i<=poz+lungime-1;i++)
g<<s[i];
g.close();
exit(1);
}
return 0;
}