Beispiel #1
0
int main( int argc, char * argv[] )
{
    if( argc != 3 ){
        std::cerr << "Usage: " << argv[ 0 ] << " dictionary_file text_file"
            << std::endl;
        return 1;
    }

    AhoCorasick ac;

    auto const print = []( size_t const pos, std::string const & word ){
        std::cout << "> " << std::string( pos, ' ' ) << word << std::endl;
    };

    std::string line;
    std::ifstream dictFile( argv[ 1 ] );
    std::ifstream textFile( argv[ 2 ] );

    while( std::getline( dictFile, line ) ){
        ac.insert( line );
    }

    while( std::getline( textFile, line ) ){
        std::cout << "> " << line << "\n";
        ac.search( line, print );
        std::cout << std::string( 80, '-' ) << "\n";
    }
}
Beispiel #2
0
int main(){
  ios_base::sync_with_stdio(false); 
  int N;
  while (cin >> N && N) {
    ac.reset();
    vector<string> words(N);
    map<string, int> ms;
    for (int i = 0; i < N; i++) cin >> words[i];
    for (int i = 0; i < N; i++) {
      ac.insert(words[i].c_str(), i + 1); 
      ms[words[i]] = i + 1;
    }
    ac.getFail();
    string text; cin >> text;
    ac.find(text.c_str());
    int best = *max_element(ac.cnt, ac.cnt + N + 1);
    cout << best << endl; 
    for (int i = 0; i < N; i++){
      if (ac.cnt[ms[words[i]]] == best){
        cout << words[i] << endl;
      }
    }
  }
}