Cod sursa(job #101983)

Utilizator ProtomanAndrei Purice Protoman Data 13 noiembrie 2007 22:50:49
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 0.91 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SM 10000000
char c[1005];
char s[51][21];
char str[21];
long i,j,n,m,nm,dimh,h,nr;

int fn_sortare(const void * a,const void *b) 
{
	char * aa=(char *)a;
	char * bb=(char *)b;
	return (strcmp(aa,bb));
}

void search(long li,long ls)
{
	m=(li+ls)/2;
	if (strcmp (str,s[m]) == 0)
			nr++;
	else if (li<ls)
		if (strcmp (str,s[m]) < 0)
			search(li,m-1);
		else search(m+1,ls);
}

int main()
{
	freopen("abc2.in","r",stdin);
	freopen("abc2.out","w",stdout);
	fgets(c,SM+1,stdin);
	nm=strlen(c)-1;
	while (!feof(stdin)) 
	{
		i++;
		scanf("%s\n",&s[i]);
	}
	n=i;
	h=strlen(s[1]);
	qsort(s+1,n,sizeof(s[1]),fn_sortare);
	str[0]='0';
	for (i=0;i<h-1;i++)
		str[i+1]=c[i];
	for (i=h-1;i<=nm;i++)
	{
		strcpy(str,str+1);
		str[h-1]=c[i];
		search(1,n);
	}
	printf("%ld",nr);
	fclose(stdin);
	fclose(stdout);
	return 0;
}