Pagini recente » Cod sursa (job #694080) | Profil florinhaja | Cod sursa (job #2275981) | Cod sursa (job #2296935) | Cod sursa (job #107645)
Cod sursa(job #107645)
#include <cstdio>
#include <cstring>
#define Nmax 10000015
#define Mmax 50015
#define Lmax 22
#define Smax 1024 * 1024
int n, m, l, ct;
char sir[Nmax];
char cuv[Mmax][Lmax];
int trie[Smax][3];
int atm[Smax][3];
int c[Smax];
int t[Smax];
char last[Smax];
char ok[Smax];
void citire()
{
fgets(sir, Nmax, stdin);
while(!feof(stdin))
{
fgets(cuv[m + 1], Lmax, stdin);
if ('a' <= cuv[m + 1][0] && cuv[m + 1][0] <= 'c') ++m;
}
}
void insert(int ind)
{
int i, pos = 0;
for (i = 0; i < l; ++i)
{
if (!trie[pos][(int)cuv[ind][i]])
trie[pos][(int)cuv[ind][i]] = ++ct;
pos = trie[pos][(int)cuv[ind][i]];
}
ok[pos] = 1;
}
void BF()
{
int st = 0, dr = 1, nod, i, bunic, next;
while (st < dr)
{
nod = c[++st];
for (i = 0; i < 3; ++i)
if (nod)
{
bunic = atm[t[nod]][(int)last[nod]];
if (trie[bunic][i]) atm[nod][i] = trie[bunic][i];
else atm[nod][i] = atm[bunic][i];
}
atm[t[nod]][(int)last[nod]] = nod;
for (i = 0; i < 3; ++i)
{
next = trie[nod][i];
if (next)
{
c[++dr] = next;
t[next] = nod;
last[next] = i;
}
}
}
}
void solve()
{
int i, j, pos = 0, sol = 0;
l = strlen(cuv[1]);
while (l > 0 && !('a' <= cuv[1][l - 1] && cuv[1][l - 1] <= 'c')) --l;
for (i = 1; i <= m; ++i)
for (j = 0; j < l; ++j)
cuv[i][j] -= 'a';
for (i = 1; i <= m; ++i)
insert(i);
BF();
n = strlen(sir);
while (n > 0 && !('a' <= sir[n - 1] && sir[n - 1] <= 'c')) --n;
for (i = 0; i < n; ++i)
sir[i] -= 'a';
for (i = 0; i < n; ++i)
{
pos = atm[pos][(int)sir[i]];
sol += ok[pos];
}
printf("%d\n", sol);
}
int main()
{
freopen("abc2.in", "r", stdin);
freopen("abc2.out", "w", stdout);
citire();
solve();
return 0;
}