Cod sursa(job #98429)

Utilizator DorinOltean Dorin Dorin Data 10 noiembrie 2007 13:23:23
Problema Abc2 Scor 0
Compilator cpp Status done
Runda Happy Coding 2007 Marime 2.49 kb
# include <stdio.h>
# include <string>

using namespace std;

# define input "abc2.in"
# define output "abc2.out"

# define max 10000001

struct abc
{
	int a,b,c;
	abc *urma,*urmb,*urmc;
}*dic;

int l,j;
long n,i,rez;

char s[max],cuv[22];

int main()
{
	freopen(input,"r",stdin);
	freopen(output,"w",stdout);

	scanf("%s",s);
	scanf("%s",cuv);

	l = strlen(cuv);

//	dic = new abc;
	abc * last;
				  /*
	switch(cuv[0])
	{
		case 'a':dic->a = 1;last = dic->urma; break;
		case 'b':dic->b = 1;last = dic->urmb; break;
		case 'c':dic->c = 1;last = dic->urmc; break;
	}           */

	dic = new abc;
	dic->a=0;
	dic->b=0;
	dic->c=0;
	last = dic;

	for(i=0;i<l;i++)
	{
		switch(cuv[i])
		{
			case 'a':
				last->a = 1;
				last->urma = new abc;
				last = last->urma; break;
			case 'b':
				last->b = 1;
				last->urmb = new abc;
				last = last->urmb; break;
			case 'c':
				last->c = 1;
				last->urmc = new abc;
				last = last->urmc; break;
		}
		last->a=0;
		last->b=0;
		last->c=0;
	}

	last = dic;
			  /*
	for(i=0;i<l;i++)
	{
		if(last->a)
		{
			last = last->urma;
			printf("a ");
		}
		if(last->b)
		{
			last = last->urmb;
			printf("b ");
		}
		if(last->c)
		{
			last = last->urmc;
			printf("c ");
		}
	}
			*/
	while(scanf("%s",cuv)!= EOF)
	{
		last = dic;
		for(i=0;i<l;i++)
		{
			switch(cuv[i])
			{
				case 'a':
				if(last->b)
					last = last->urma;
				else
				{
					last->a = 1;
					last->urma = new abc;
					last = last->urma;
					last->a = last->b = last -> c = 0;
				}
				break;
				case 'b':
				if(last->b)
					last = last->urmb;
				else
				{
					last->b = 1;
					last->urmb = new abc;
					last = last->urmb;
					last->a = last->b = last->c = 0;
				}
				break;
				case 'c':
				if(last->c)
					last = last->urmc;
				else
				{
					last->c = 1;
					last->urmc = new abc;
					last = last->urmc;
					last->a = last->b = last -> c = 0;
				}
				break;
			}
		}
	}
	n = strlen(s);
	int ok;

	for(i=0;i<n-l+1;++i)
	{
		ok = 1;
		last = dic;
		for(j=0;j<l && ok;j++)
		{
			switch(s[i+j])
			{
				case 'a':
				if(!last->a)
					ok=0;
				else
					last = last->urma;
				break;
				case 'b':
					if(!last->b)
						ok=0;
					else
						last = last->urmb;
					break;
				case 'c':
					if(!last->c)
						ok=0;
					else
						last = last->urmc;
				break;
			}
		}
		if(ok)
			rez++;
	}
	printf("%ld",rez);


	return 0;
}