Pagini recente » Cei mai harnici utilizatori infoarena | Clasament arhiva | Borderou de evaluare (job #1569093) | Cod sursa (job #752243) | Cod sursa (job #1571078)
#include <cstdio>
#include <deque>
#include <vector>
const int SIGMA = 3;
using namespace std;
int NrWords, Answer;
char String[1 << 24], Word[1 << 16][1 << 6];
class state_machine {
private:
struct tree {
int cnt_words;
int cnt_children;
int cnt_occurrences;
tree *children[SIGMA];
tree *back_node;
vector <tree *> front_node;
tree () {
cnt_words = NULL;
back_node = NULL;
cnt_children = NULL;
cnt_occurrences = NULL;
for( int i = 0; i < SIGMA; i ++ )
children[i] = NULL;
front_node.clear();
}
} *Root = new tree;
void insert_word( tree *Node, char *String ) {
if( *String == NULL ) {
Node -> cnt_words ++;
return;
}
if( Node -> children[*String - 'a'] == NULL ) {
Node -> children[*String - 'a'] = new tree;
Node -> cnt_children ++;
}
insert_word( Node -> children[*String - 'a'], String + 1 );
return;
}
int delete_word( tree *Node, char *String ) {
if( *String == NULL ) Node -> cnt_words --; else
if( delete_word( Node -> children[*String - 'a'], String + 1 ) ) {
Node -> children[*String - 'a'] = NULL;
Node -> cnt_children --;
}
if( Node != Root && Node -> cnt_children == NULL && Node -> cnt_words == NULL ) {
delete Node;
return 1;
}
return 0;
}
int count_word( tree *Node, char *String ) {
if( *String == NULL )
return Node -> cnt_words;
if( Node -> children[*String - 'a'] != NULL )
return count_word( Node -> children[*String - 'a'], String + 1 );
return 0;
}
int longest_prefix( tree *Node, char *String, int prefix_length ) {
if( *String == NULL )
return prefix_length;
if( Node -> children[*String - 'a'] != NULL )
return longest_prefix( Node -> children[*String - 'a'], String + 1, prefix_length + 1 );
return prefix_length;
}
void reset_machine( tree *Node ) {
Node -> cnt_occurrences = NULL;
Node -> back_node = NULL;
Node -> front_node.clear();
for( int i = 0; i < SIGMA; i ++ )
if( Node -> children[i] != NULL )
reset_machine( Node -> children[i] );
return;
}
void build_back_edges( tree *start_node ) {
deque <tree *> Queue;
Queue.push_back(start_node);
while( !Queue.empty() ) {
tree *Node = Queue.front();
for( int i = 0; i < SIGMA; i ++ ) {
if( Node -> children[i] != NULL ) {
tree *Aux = Node -> back_node;
while( Aux && Aux -> children[i] == NULL )
Aux = Aux -> back_node;
if( Aux == NULL )
Node -> children[i] -> back_node = Root;
else
Node -> children[i] -> back_node = Aux -> children[i];
Node -> children[i] -> back_node -> front_node.push_back(Node -> children[i]);
Queue.push_back( Node -> children[i] );
}
}
Queue.pop_front();
}
return;
}
void build_occurrences( tree *Node, char *String ) {
if( *String == NULL )
return;
while( Node != Root && Node -> children[*String - 'a'] == NULL )
Node = Node -> back_node;
if( Node -> children[*String - 'a'] != NULL )
Node = Node -> children[*String - 'a'];
Node -> cnt_occurrences ++;
build_occurrences( Node, String + 1 );
return;
}
void build_partial_sum( tree *Node ) {
for( int i = 0; i < Node -> front_node.size(); i ++ ) {
build_partial_sum( Node -> front_node[i] );
Node -> cnt_occurrences += Node -> front_node[i] -> cnt_occurrences;
}
return;
}
int count_occurrences( tree *Node, char *String ) {
if( *String == NULL )
return Node -> cnt_occurrences;
if( Node -> children[*String - 'a'] != NULL )
return count_occurrences( Node -> children[*String - 'a'], String + 1 );
return 0;
}
public:
void insert_word( char *String ) {
insert_word( Root, String );
return;
}
void delete_word( char *String ) {
delete_word( Root, String );
return;
}
int count_word( char *String ) {
return count_word( Root, String );
}
int longest_prefix( char *String ) {
return longest_prefix( Root, String, 0 );
}
void build_occurrences( char *String ) {
reset_machine( Root );
build_back_edges( Root );
build_occurrences( Root, String );
build_partial_sum( Root );
return;
}
int count_occurrences( char *String ) {
return count_occurrences( Root, String );
}
} my_state_machine;
int main () {
freopen( "abc2.in" , "r", stdin );
freopen( "abc2.out", "w", stdout );
scanf( "%s", String );
while( scanf( "%s", Word[++NrWords] )) {
if( Word[NrWords][0] == NULL ) {
NrWords --;
break;
}
my_state_machine.insert_word( Word[NrWords] );
}
my_state_machine.build_occurrences( String );
for( int i = 1; i <= NrWords; i ++ ) {
Answer += my_state_machine.count_occurrences( Word[i] );
while( my_state_machine.count_word( Word[i] ) != 0 )
my_state_machine.delete_word( Word[i] );
}
printf( "%d\n", Answer );
return 0;
}