Cod sursa(job #338145)

Utilizator ilincaSorescu Ilinca ilinca Data 5 august 2009 13:27:14
Problema Prefix Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.97 kb
/*
PROG: prefix
LANG: C++
ID: ilinca.2
 */

#include <stdio.h>
#include <string.h>

#define nmax 200025
#define pmax 205
#define lmax 25

int n, np;
char p [pmax] [lmax], a [nmax];
bool v [nmax];

void prefix (char s [])
{
	char *w;
	w=strtok (s, " ");
	while (w != NULL)
	{
		strcpy (p [++np], w);
		w=strtok (NULL, " ");
	}
}

void scan ()
{
	char aux [225];
	do
	{
		scanf ("%s", aux);
		prefix (aux);
	} while (aux [0] != '.');
	--np;
	while (scanf ("%s", aux) != EOF)
	{
		strcat (a, aux);
	}
}

bool ok (int u, int x)
{
	int i;
	for (i=0; p [x] [i]; ++i, ++u)
		if (p [x] [i] != a [u])
			return false;
	return true;
}

int dynamic ()
{
	int u=0, i, j;
	v [0]=true;
	for (i=0; a [i]; ++i)
		if (v [i] == true)
		{
			u=i;
			for (j=1; j <= np; ++j)
				if (ok (i, j))
					v [i+strlen (p [j])]=true;
		}
	if (v [i] == true)
		u=i;
	return u;
}

int main ()
{
	freopen ("prefix.in", "r", stdin);
	freopen ("prefix.out", "w", stdout);
	scan ();
	printf ("%d\n", dynamic ());
	return 0;
}