Пример #1
0
int main() {
	cout << "!!!Hello Trie !!!" << endl;

	Trie* ac = new Trie;
    ac->AddWord("California");
    ac->AddWord("Colarado");
    ac->AddWord("Alaska");
    ac->AddWord("Arizona");

    if ( ac->SearchWord("Barcelona") )
    	cout << "Found Barcelona" << endl;
    else
    	cout << "Barcelona not found" << endl;

    if ( ac->SearchWord("California") )
       	cout << "California found" << endl;
    else
       	cout << "California not found" << endl;

}
int main(int argc, char** argv) {
  if ( argc <= 1 ) {
    cout << "Please enter the filename with sorted" << 
             "words (lower case 'a' to 'z' without space)" << endl;
    return 1;
  } 
 
  Trie t;
  queue<pair<string, string> > q;
  
  char *file_name = argv[1];
  ifstream ifs(file_name, ifstream::in);

  string line;
  while ( ifs >> line ) {
    t.AddWord(line);   
    vector<pair<string, string> > word_prefix_pairs = t.GetAllPrefixWords(line);
    if ( word_prefix_pairs.size() != 0 ) {
      for ( int i = 0; i < word_prefix_pairs.size(); ++i ) {
        q.push(word_prefix_pairs[i]);
      }
    }  
  }
  
  int max_val = 0;
  
  set<string> v;

  while ( !q.empty() ) {
    if ( t.ContainsWord(q.front().second)) {
      v.insert(q.front().first);
      max_val = max(max_val, (int)(q.front().first.size()));
      q.pop();
    } else {
      vector<pair<string, string> > word_prefix_pairs = t.GetAllPrefixWords(q.front().second);
      if ( word_prefix_pairs.size() != 0 ) {
        for ( int i = 0; i < word_prefix_pairs.size(); ++i ) {
          pair<string, string> p(q.front().first, word_prefix_pairs[i].second);
          q.push(p);
        }
      }
      q.pop();  
    }
  }
  

  cout << "max: " << max_val << " ctr: " << v.size() << endl;
  return 0;
}