Pagini recente » Cod sursa (job #2345478) | Cod sursa (job #2763542) | Borderou de evaluare (job #1129683) | Clasament s013 | Cod sursa (job #1591822)
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream fin("abc2.in");
ofstream fout("abc2.out");
const int DimC = 22;
struct Nod
{
Nod()
{
nr_c = 0;
memset( son, 0, sizeof(son) );
}
int nr_c;
Nod* son[4];
};
int n, m, l, nr;
Nod* T = new Nod;
string txt, c;
char cuv[DimC];
vector<string> G;
void Read();
void Insert(Nod* &nod, int i, int j);
int Count( Nod* nod, int i, int j);
int main()
{
Read();
sort(G.begin(), G.end());
G.erase(unique(G.begin(), G.end()), G.end() );
m = txt.size();
// fout << l << ' ' << n;
for ( int i = 0; i <= m - l; i++ )
{
Insert( T , i, i + l - 1 );
}
for ( auto y : G )
{
c = y;
nr += Count( T, 0 , l);
// fout << '\n';
}
fout << nr;
fin.close();
fout.close();
return 0;
}
void Read()
{
getline(fin, txt);
while ( fin.getline( cuv, DimC ) )
{
n++;
// cout << cuv << '\n';;
if ( l == 0 )
l = strlen(cuv);
G.push_back(cuv);
}
}
void Insert(Nod* &nod, int i, int j)
{
if ( i > j )
return;
if ( nod == 0 )
nod = new Nod;
if ( i == j )
nod->nr_c++;
Insert( nod->son[txt[i] - 'a'], i + 1, j);
}
int Count( Nod* nod, int i, int j)
{
if ( i == l - 1 )
{
return nod->nr_c;
}
if ( nod->son[c[i] - 'a'] )
return Count(nod->son[c[i] - 'a'], i + 1, j);
return 0;
}