Cod sursa(job #28377)

Utilizator conttPop Mircea contt Data 7 martie 2007 19:30:06
Problema Substr Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<fstream.h>
#include <stdlib.h>
#include<string.h>
long k,n;
char s[100];
 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;

}