Pagini recente » Cod sursa (job #3213128) | Cod sursa (job #101352)
Cod sursa(job #101352)
#include <cstdio>
class elem {
//elem* nx[3];
int nx[3];
bool fin;
public:
elem() { nx[0] = nx[1] = nx[2] = NULL; fin = false; };
int& operator[] ( char x ) { return nx[x-'a']; };
bool word() { return fin; };
void set_ok() { fin = true; };
/* void erase() {
if (nx[0]) {
(*(nx[0])).erase();
delete nx[0];
}
if (nx[1]) {
(*(nx[1])).erase();
delete nx[1];
}
if (nx[2]) {
(*(nx[2])).erase();
delete nx[2];
}
};*/
};
const int SM = 10000000;
const int CM = 20;
const int EM = 1000000;
char s[SM+1];
char cuv[CM+1];
int nv;
elem v[EM+1];
int main() {
freopen("abc2.in","rt",stdin);
freopen("abc2.out","wt",stdout);
fgets(s,SM+1,stdin);
nv = 0;
int root = 0, cur = 0;
while (!feof(stdin)) {
scanf("%s\n",cuv);
cur = root;
for (int i = 0; cuv[i] != '\0' && !v[cur].word(); ++i) {
if (v[cur][cuv[i]] == NULL) v[cur][cuv[i]] = ++nv;
cur = v[cur][cuv[i]];
}
v[cur].set_ok();
}
int r = 0;
for (int i = 0; s[i] != '\n'; ++i) {
cur = root;
for (int j = i; s[j] != '\n' && v[cur][s[j]]; ++j) {
cur = v[cur][s[j]];
if (v[cur].word()) {
++r;
break;
}
}
}
printf("%d\n",r);
return 0;
}