Esempio n. 1
0
void doIt(){
    int n, a[2002];
    Trie* t = new Trie();
    scanf("%d", &n);
    Rep(i, n){
        scanf("%d", &a[i]);
        t->addWord(b2s(a[i], MAX_BIT));
    }
Esempio n. 2
0
string find_shortest_prefix(const string& s, const unordered_set<string>& D) {
  // Build a trie according to given dictionary D
  Trie T;
  for (const string& word : D) {
    T.insert(word);
  }
  return T.getShortestUniquePrefix(s);
}
int main(int argc, char const *argv[])
{
    Trie trie;
    trie.insert("ab");
    trie.insert("door");
    cout<<trie.startsWith("ke")<<endl;
    return 0;
}
Esempio n. 4
0
 int countWords(const char *s) {
    if(*s=='\0')
       return words;
    Trie *t = child[*s-'a'];
    if(t==NULL)
       return 0;
    return t->countWords(s+1);
 }
Esempio n. 5
0
int main(){
	Trie trie;
	trie.insert("abcdefgh");
	trie.insert("abdefg");
	trie.insert("abdefg");
	trie.insert("abdefg");
	cout<<trie.prefix("abdee")<<endl;
}
Esempio n. 6
0
 int countPreffixes(const char *s) {
    if(*s=='\0')
      return prefixes;
    Trie *t = child[*s-'a'];
    if(t==NULL) 
       return 0;
    return t->countPreffixes(s+1);
 }
int main()
{
	Trie t;
	cout<<(t.search("")?"true":"false");
	t.insert("apple");
	cout<<(t.startsWith("a")?"true":"false");
	
}
	long long cantPrefFreeSubsets(vector <string> words) {
		Trie T;

		for (int i=0; i<(int)words.size(); ++i)
			T.insert(words[i]);

		return calc(T);
	}
Esempio n. 9
0
int main(int argc, const char * argv[]) {
    
    if(argc<2){
        show_usage();
        cerr << "not enought parameters!" << endl;
        exit(1);
    }
    
    clock_t curr_time;
    curr_time = clock();
    
    // read from file
    Trie* trie = new Trie();
    cout << "Read from File" << endl;
    string path = argv[1];
    ifstream iFile(path);
    if(!iFile) {
        cerr << "unable to open file" << endl;
        exit(1);
    }
    string line;
    while (getline(iFile, line)) {
        if (!line.empty() && line[line.size() - 1] == '\r')
            line.erase(line.size() - 1);
        trie->addWord(line);
    }
    
    curr_time = clock() - curr_time;
    cout << "Read File and Build Trie Done! Time: " << ((float)curr_time)/CLOCKS_PER_SEC << " seconds" << endl;
    
    // sortedSet stores all the compound words in decresing order in terms of string length
    set<string, cmpStruct> sortedSet;
    while(!trie->suffix_queue.empty()) {
        StringPair sp = trie->suffix_queue.front();
        trie->suffix_queue.pop();
        if(trie->searchWord(sp.first, sp.second)) {
            sortedSet.insert(sp.first);
        }
    }
    curr_time = clock() - curr_time;
    cout << "Search For Compound Words Done! Time: " << ((float)curr_time)/CLOCKS_PER_SEC << " seconds" << endl;
    
    std::set<string>::iterator it;
    int i = 0;
    for (it = sortedSet.begin(); i < 2 && it != sortedSet.end(); ++it, ++i)
        cout << "The " << i+1 << " Longest Compound Word:" << *it << endl;
    cout << "Number of Total Compound Words: " << sortedSet.size() << endl;
    
    //write all compound words to file
    std::ofstream out("compoundWords.txt");
    for (it = sortedSet.begin(); it != sortedSet.end(); ++it) {
        out << *it << endl;
    }
    out.close();
    
    delete trie;
    return 0;
}
Esempio n. 10
0
int main(int argc, char const *argv[])
{
    Trie trie;
    trie.insert("abc");
    trie.insert("ab");
    cout << trie.search("hel") << endl;
    cout << trie.search("ab") << endl;
    return 0;
}
Esempio n. 11
0
int main()
{
    Trie t;
    string s("abcd");
    string prefix("abc");
    t.insert(s);
    cout << t.search(s) << endl;
    cout << t.startsWith(prefix) << endl;
}
Esempio n. 12
0
TrieChildren::TrieChildren(const char label, ITrie* child, 
	const std::string& word, const bool storeEmpty) : isEmpty_(storeEmpty)
{
    memset(children_, 0, sizeof(children_));
    children_[tolower(label) - 'a'] = child;

    Trie t;
    children_[tolower(word[0]) - 'a'] = t.Add(word.substr(1));
}
 string findBestRecipe(const char *s) {
    if(*s=='\0') {
      if(best >= 0 && best < N) return dict[best];
      return "NO";
    }
    Trie *t = child[(int)*s];
    if(t==NULL) return "NO";
    return t->findBestRecipe(s+1);
 }
int main()
{
    Trie obj = Trie();
    obj.insert("");
    obj.insert("search");
    cout << obj.search("") << endl;
    cout << obj.search("a") << endl;
    return 0;
}
Esempio n. 15
0
bool has_common_prefix(const std::vector<std::string>& values) {
  Trie trie;
  for (const auto& value : values) {
    if (trie.add(value)) {
      return true;
    }
  }
  return false;
}
int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);

	unsigned int N;
	cin >> N;
	cin.ignore();

	vector<string> facilities = { " " };
	vector<bool> foundFacilities = { false };
	for (unsigned int i = 0; i < N; i++){
		string facility;
		getline(cin, facility);
		facilities.emplace_back(facility);
		foundFacilities.push_back(false);
	}
	std::sort(facilities.begin(), facilities.end());

	Trie facilitiesTrie;
	for (unsigned int i = 1; i <= N; i++){
		facilitiesTrie.insert(i, facilities[i]);
	}
	//facilitiesTrie.print();

	string text;
	getline(cin, text);

	auto& start = facilitiesTrie.root->nodes;
	for (int i = 0, n = text.length(); i < n; i++){
		auto result = start.find(tolower(text[i]));
		if (result != start.end()){
			TrieNode* cur = result->second;
			if (cur->matchCode)
				foundFacilities[cur->matchCode] = true;
			
			int j = i + 1;
			while (cur != nullptr){
				auto result = cur->nodes.find(tolower(text[j++]));
				if (result != cur->nodes.end()){
					cur = result->second;
					if (cur->matchCode)
						foundFacilities[cur->matchCode] = true;
				}
				else
					cur = nullptr;
			}
		}
	}

	for (int i = 1; i <= N; i++){
		if (foundFacilities[i])
			cout << facilities[i] << endl;
	}

	system("pause");
}
Esempio n. 17
0
int main(){

Trie t;

t.addWord("bel");
t.addWord("hbel");
t.addWord("hello");
t.addWord("hel");
return 0;
}
Esempio n. 18
0
int main()
{
    Trie trie;
    trie.init();
    trie.loadDict("../dicts/segdict.gbk.v2.1");
    //trie.loadDict("tmp");
    cout<<trie.getMinLogFreq()<<endl;
    trie.dispose();
    return 0;
}
Esempio n. 19
0
void Trie::insert(const string &word) {
    Trie * node = this;
    int size = word.length();
    for (int i = 0; i < size; i++) {
        const char c = word[i];
        if (node->children.find(c) == node->children.end())
            node->add(c);
        node = &node->children[c];
    }
    node->flag = true;
}
/* Function to load all the words from the given ile in the Trie tree */
void
loadData(Trie &T, char *filename) {

        ifstream file;
        file.open(filename);
        for (string word; getline(file, word);) {
                T.stripWord(word);
                T.insertWord(word);
        }
        file.close();
}
Esempio n. 21
0
 vector<string> findWords(vector<vector<char> >& board, vector<string>& words) {
     // 创建字典树
     Trie trie;
     for (vector<string>::iterator it = words.begin(), end = words.end(); it != end; ++it)
         trie.insert(*it);
     // 查找
     vector<string> ret = findWords(board, trie);
     // 释放内存
     trie.free();
     return ret;
 }
Esempio n. 22
0
int main()
{
	//test();
	//TrieNode test;
	//cout << test.dataValue.counter << endl;
	Trie test;
	test.build();
	vector<TrieNode*> words;
	test.showTrie(NULL, words);
	return 0;
}
 void addWord(const char *s, const int ind) {
      if(*s=='\0') return;
      Trie *t = child[(int)*s];
      if(child[(int)*s]==NULL) {
         t = child[(int)*s] = new Trie();  
         t->best = ind;
      } else {
         if(P[ind] > P[t->best]) t->best = ind;
      }
      t->addWord(s+1, ind);
 }
Esempio n. 24
0
int main()
{

	int numtests;
	char newelem[StrMaxElem];
	Trie t;
	t.Readlist();
	cout<<endl<<" "<<t.Count()<<endl;

	return 0;
}
Esempio n. 25
0
int main()
{
    Trie t;
    t.insert(0);
    t.insert(1);
    t.insert(2);
    t.insert(3);

   cout<<t.query_max(3);

    return 0;
}
Esempio n. 26
0
int main() {
	
	Trie* t = new Trie();
	t->addWord("hello");
	t->addWord("world");
	t->addWord("max");
	t->addWord("min");
	t->addWord("maximum");
	
	alphabetize(t->getRoot());
	return 0;
}
Esempio n. 27
0
void test() {
  Trie *t = new Trie();
  char *words[] = {"abc", "a", "b", "a", "xyz", "bc"};
  char *absentWords[] = {"bas", "git", "oglum", "c", "bak"};
  for (int i = 0; i < 6; ++i) {
    t->insert(words[i]);
    printf("%s %d\n", words[i], t->contains(words[i]));
  }
  for (int i = 0; i < 5; ++i) {
    printf("%s %d\n", absentWords[i], t->contains(absentWords[i]));
  }
}
Esempio n. 28
0
int main(int argc, char** argv)
{
	if (argc < 4)
	{
		std::cout << "Usage: get_TrieArray <fastafile> <matrix> <peptideLength> <outfile> " << std::endl;
		return -1;
	}

	string fastafile(argv[1]);
	string matrix(argv[2]);
	cout << "test" << endl;
	int peptideLength(atoi(argv[3]));
	string outname(argv[4]);

	
	//std::cout << fastafile << "\t" << outname << std::endl;
		
//----------------------------------------------------------------------------------------
	cout << "Reading FASTA file..." << endl;

	Sequences s(fastafile);
	cout << "Read " << s.size() << " sequences." << endl;

	cout << "Generating peptides..." << endl;
	Sequences ninemers;
	generateAllSubstrings(ninemers, s, peptideLength);
	cout << "Generated " << ninemers.size() << " peptides." << endl;

	s.clear();


  	//Matrix m("/abi-projects/dist2self/matrices/BLOSUM45_distance_normal.dat"); cout << "Initializing trie. " << endl; Trie t; 
  	Matrix m(matrix); cout << "Initializing trie. " << endl; Trie t;
	Matrix::IndexSequence indices; 
  	for (size_t i = 0; i < ninemers.size(); ++i) {
 
  	 m.translate(ninemers[i], indices); t.add(indices);
  	} 
    t.dump();


	cout << "Converting to trie array." << endl;
  	TrieArray ta(t, peptideLength);

	cout << "Done." << endl;

//	std::ofstream ofs("test.trie");
	std::ofstream ofs(outname.c_str());
	boost::archive::text_oarchive oa(ofs);
	ta.save(oa,1);


}
Esempio n. 29
0
 vector<string> Recommend(const vector <string> &remaining_commands) {
     if (remaining_commands.size() == 1) {
         return RecommendNames(remaining_commands[0]);
     } else {
         Trie* trie = GetTrie(remaining_commands[0]);
         if (trie == nullptr) {
             return {};
         } else {
             return trie->GetPrefix(remaining_commands.back());
         }
     }
 }
Esempio n. 30
0
int main(int argc, char* argv[])
{
  Trie trie;

  for(const auto& w : dict)
  {
    trie.addWord(w);
  }

  std::cerr << trie.guessWord() << std::endl;
  return 0;
}