int main(char** argv, int argc) { dict.insert("liu"); dict.insert("kai"); dict.insert("liuk"); dict.insert("ai"); split("liukai", dict, printVector); split("kaiai", dict, printVector); split("", dict, printVector); return 0; }
// Parsea as palavras lidas void parser() { char *pch = strtok (text, "\n"); int countShort=0; int countCompound=0; while(pch != NULL){ if(strlen(pch) <= 5) shortDict.insert(countShort++, pch); else compoundDict.insert(countCompound++, pch); pch = strtok (NULL, "\n"); } }
bool build(Dict &_front, Dict &_back, Dict &dict, Tree &tree, bool flag) { if (_front.empty()) return false; if (_front.size() > _back.size()) return build(_back, _front, dict, tree, !flag); for (auto &word : _front) dict.erase(word); for (auto &word : _back) dict.erase(word); Dict hoge; bool piyo = false; for (auto &i : _front) { string word = i; for (auto &c : word) { char fuga = c; for (c = 'a'; c <= 'z'; c++) { if (c == fuga) continue; if (_back.count(word)) { piyo = true; !flag ? tree[i].push_back(word): tree[word].push_back(i); } else if (!piyo && dict.count(word)) { hoge.insert(word); !flag ? tree[i].push_back(word): tree[word].push_back(i); } } c = fuga; } } return piyo || build(hoge, _back, dict, tree, flag); }
// Parsea as palavras lidas void parser() { char *pch = strtok (text, "\n"); while(pch != NULL){ mydict.insert(strdup(pch)); pch = strtok (NULL, "\n"); } free(text); }
int main(int argc, char* argv[]) { if(argc != 2) { cout << "Uso: " << argv[0] << " palavras.txt" << endl; exit(-1); } //Inicializa o dicionario init_dict(argv[1]); //Gera as palavras com até cinco letras Dict::iterator itset; tic(); while(true) { int tamanho = gera_tamanho_palavra(); char *new_word = gera_palavra(tamanho); itset = mydict.find(new_word); if(itset != mydict.end()) { mydict_found.insert(*itset); mydict.erase(*itset); qtd_encontradas++; imprime_prop(); float prop = qtd_encontradas / (float) total_palavras; if(prop >= 1.0) break; } free(new_word); } cout << mydict_found.size() << ":" << mydict.size() << endl; total_palavras = mydict.size(); controle=1; // Junta duas palavras para gerar palavras maiores Dict::iterator it[2]; char* combined[2]; for(it[0] = mydict_found.begin(); it[0]!=mydict_found.end(); ++it[0]) { for(it[1] = mydict_found.begin(); it[1]!=mydict_found.end(); ++it[1]) { combined[0] = mystrcat(*it[0], *it[1]); combined[1] = mystrcat(*it[1], *it[0]); for(int i=0; i<2; i++) { itset = mydict.find(combined[i]); if(itset != mydict.end()) { //cout << qtd_encontradas << " - combined: " << combined[i] << endl; mydict_found.insert(*itset); mydict.erase(*itset); qtd_encontradas++; imprime_prop(); float prop = qtd_encontradas / (float) total_palavras; if(prop >= 1.0) break; } free(combined[i]); } } } fclose(arq_palavras); cout << "elapsed time: " << toc() << endl; return 0; }