int main() {
    char s[100];
    int cases = 0;
    while(gets(s)) {
        flag = 0, size = 0;
        nd[0].l = 0, nd[0].r = 0;
        Trie(s);
        while(gets(s)) {
            if(s[0] == '9') break;
            if(flag)    continue;
            Trie(s);
        }
        printf("Set %d is %simmediately decodable\n", ++cases, !flag ? "" : "not ");
    }
    return 0;
}
int main()
{
    Trie trie = Trie();
    trie.insert("ted"); 
    trie.insert("to"); 
	trie.insert("tea"); 
	trie.insert("ten"); 
	trie.insert("in"); 
	trie.insert("inn");
    time_t t1,t2;
    time(&t1);
    trie.buildTrie("/home/harishkrupo/Desktop/uc6.txt");

    time(&t2);
    cout << "Build completed in "<< difftime(t2,t1) << " seconds"<<endl;
    string s;
    while(true)
    {
        cout << "Enter the keyword : ";
        getline(cin,s);
        if(cin.eof())
        {
            cout << endl;
            break;
        }
        vector<string>* x = trie.AStarSearch(s, 20);
        unsigned int i;
    
        for(i=0;i<x->size();i++)
        {
            cout << x->at(i) << endl;
        }    
    }
    
    return 0;
}
Beispiel #3
0
void Trie::add(char c) {
    if (value == "")
        children[c] = Trie(string(1, c));
    else
        children[c] = Trie(value + c);
}
 inline Trie* next(Trie* cur, char c) {
     if (cur->next.find(c) == cur->next.end()) {
         cur->next[c] = Trie();
     }
     return &(cur->next[c]);
 }
Beispiel #5
0
void *largestPalindrome(node *T, char *word, int size, node *P){
	int i, j, k, pos, test;
	char *largest, *aux_largest, *aux_word;
	node *pointer, *aux;
	
	P = Trie();
	
	if (!(largest = malloc(sizeof(char)*size))){
		printf("Falta de memória");
		exit(1);
	}
	
	if (!(aux_largest = malloc(sizeof(char)*size))){
		printf("Falta de memória");
		exit(1);
	}
	
	if (!(aux_word = malloc(sizeof(char)*size))){
		printf("Falta de memória");
		exit(1);
	}
	
	largest[0] = '\0';
	pointer = T;

	for (i = 0; i < size; i++){
		aux = T;
		pos = position(word[i]);
		j = 0;
		k = i;

		while(aux->key[pos]){
			aux_largest[j] = word[k];
			j++;
			k++;
			aux = aux->key[pos];
			pos = position(word[k]);
		}
		
		aux_largest[j] = '\0';

		while (j > 0){
			if (!palindrome(aux_largest)){
				j--;
				aux_largest[j] = '\0';
			} else {
				break;
			}
		}
		
		if (lenght(aux_largest) > lenght(largest)){
			copy(largest, aux_largest); // largest = aux_largest
			/* Caso tenha uma palavra maior que anterior
			 * limpar a trie para conter somente as maiores palíndromas */
			emptyTree(P);
		}
		
		copy(aux_word, aux_largest);
		insert(P, aux_word);
	}

	printf("-- Maiores palindormas:");
	printTree(P, lenght(largest));
}
Beispiel #6
0
int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		std::string message = "ERROR: 2 filenames must be passed in: the first is the filename of dictionary words to add to the trie, the second is the filename of words to search the trie for.";
		std::cout << message << std::endl;
		return 1;
	}
	std::string dictionaryFile = argv[1];
	std::string queryFile = argv[2];
	//std::cout << "dictionaryFile " << dictionaryFile << " - queryFile " << queryFile << std::endl;
	std::vector<std::string> dictionary;
	std::vector<std::string> queries;	
	dictionary = getWordsFromFile(dictionaryFile);
	queries = getWordsFromFile(queryFile);
	if(dictionary.empty() || queries.empty())
	{
		std::cout << "ERROR: There was a problem reading the file or file was empty." << std::endl;
		return 1;
	}
	Trie t = Trie();
	for(int i=0; i<dictionary.size(); i++)
	{
		t.addWord(dictionary[i]);
	}
	for(int i=0; i<queries.size(); i++)
	{
		if(t.isWord(queries[i]))
		{
			std::cout << queries[i] << " is found" << std::endl;
		}
		else
		{
			std::cout << queries[i] << " is not found, did you mean:" << std::endl;
			std::vector<std::string> alternates = t.allWordsWithPrefix(queries[i]);
			for(int j=0; j<alternates.size(); j++)
			{
				std::cout << "   " << alternates[j] << std::endl;
			}
		}
	}	
	/*
	std::string s = "dog";
	std::string s2 = "dog";
	std::string s3 = "cat";
	std::string s4 = "cow";
	std::string s5 = "cattt";
	Trie t1 = Trie();
	t1.addWord(s);
	t1.addWord(s2);
	t1.addWord(s3);
	t1.addWord(s4);	
	t1.addWord(s5);
	std::cout << "t1 isWord cow" << t1.isWord(s3) << std::endl;
	std::cout << "t1 isWord ca" << t1.isWord("ca") << std::endl;
	std::cout << "t1 isWord do" << t1.isWord("do") << std::endl;
	std::cout << "t1 isWord dog" << t1.isWord("dog") << std::endl;
	std::cout << "t1 isWord cattt" << t1.isWord(s4) << std::endl;
	std::vector<std::string> wl1 = t1.allWordsWithPrefix("");
	std::cout << wl1.size() << std::endl;
	for(int i=0; i<wl1.size(); i++)
	{
		std::cout << wl1[i] << ", ";
	}
	std::cout << std::endl;
	std::vector<std::string> wl2 = t1.allWordsWithPrefix("co");
	for(int i=0; i<wl2.size(); i++)
	{
		std::cout << wl2[i] << ", ";
	}
	std::cout << std::endl;
	
	Trie t3 = Trie(t1);
	std::cout << "t3 isWord cow" << t3.isWord(s3) << std::endl;
	std::cout << "t3 isWord ca" << t3.isWord("ca") << std::endl;
	std::cout << "t3 isWord do" << t3.isWord("do") << std::endl;
	std::cout << "t3 isWord dog" << t3.isWord("dog") << std::endl;
	std::cout << "t3 isWord cattt" << t3.isWord(s4) << std::endl;

	Trie t4 = t3;
*/
	return 0;
}