Exemplo n.º 1
0
    int ladderLength(string beginWord, string endWord, unordered_set<string>& wordDict) 
    {
		if(wordDict.empty())
			return 0;
		stepWord *tempStep = new stepWord();
		tempStep->word = beginWord;
		tempStep->step = 1;
        queue<stepWord*> sBegin;		
		sBegin.push(tempStep);
		stepWord *tempStepend = new stepWord();
		tempStepend->word = endWord;
		tempStepend->step = 1;
		queue<stepWord*> sEnd;
		sEnd.push(tempStepend);
		stepWord *ptrBegin;
		stepWord *ptrEnd;
		while(!sBegin.empty() || !sEnd.empty())
		{
			if(!sBegin.empty())
			{
				ptrBegin = sBegin.front();
				sBegin.pop();
			}
			if(!sEnd.empty())
			{
				ptrEnd = sEnd.front();
				sEnd.pop();
			}
			cout << "Õ»¶¥µ¥´Ê£º" << ptrBegin->word << "\t" << ptrEnd->word <<endl;
			if(isSimilar(ptrBegin->word, ptrEnd->word))
				return ptrBegin->step + ptrEnd->step;						
			if(wordDict.empty())
				continue;
			for(unordered_set<string>::iterator p = wordDict.begin(); p != wordDict.end();)
			{
				if(isSimilar(*p, ptrBegin->word))
				{
					stepWord *tempStep = new stepWord();
					tempStep->word = *p;
					tempStep->step = ptrBegin->step + 1;
					sBegin.push(tempStep);
					p = wordDict.erase(p);
				}
				else if(isSimilar(*p, ptrEnd->word))
				{
					stepWord *tempStep = new stepWord();
					tempStep->word = *p;
					tempStep->step = ptrEnd->step + 1;
					sEnd.push(tempStep);
					p = wordDict.erase(p);
				}
				else
					++ p;
			}
		}
		return 0;
    }
    // looking for connection from both ends
    bool bfs(unordered_set<string> &forward, unordered_set<string> &backward, bool isForward, 
                unordered_set<string> &wordList, unordered_map<string, vector<string>> &transMap) {
        if (forward.empty() || backward.empty())
        {
            return false;
        }

        // always do bfs with less nodes
        if (forward.size() > backward.size())
        {
            return bfs(backward, forward, !isForward, wordList, transMap);
        }

        // remove added node in the wordList to avoid duplication
        unordered_set<string>::iterator it;
        for (it = forward.begin(); it != forward.end(); it++)
        {
            wordList.erase(*it);
        }

        for (it = backward.begin(); it != backward.end(); it++)
        {
            wordList.erase(*it);
        }

        unordered_set<string> nextlevel;
        bool isConnected = false;
        for (it = forward.begin(); it != forward.end(); it++)
        {
            string word = *it;
            for (int i = 0; i < word.size(); i++)
            {
                for (char ch = 'a'; ch <= 'z'; ch++)
                {
                    if (word[i] != ch)
                    {
                        // word is in forward list, str is in backward list
                        string str(word);
                        str[i] = ch;
                        if (backward.find(str) != backward.end())
                        {
                            isConnected = true;
                            connect(word, str, isForward, transMap);
                        }
                        else if (!isConnected && wordList.find(str) != wordList.end())
                        {
                            nextlevel.insert(str);
                            connect(word, str, isForward, transMap);
                        }
                    }
                }
            }
        }

        return isConnected || bfs(nextlevel, backward, isForward, wordList, transMap);
    }
Exemplo n.º 3
0
int Sched::run() { __logc;
    int i;
    for (i = 0; i < P; i++) {
        auto it = pending.begin();
        (*it)->attach(procs[i]);
        assoc[i] = *it;
        busy[i] = true;
        pending.erase(it);
        running.insert(assoc[i]);
    }
    while (!pending.empty() || !running.empty()) {
        logger.print("wait");
        Watcher::getInstance().wait();
        for (i = 0; i < P; i++) {
            if (!busy[i])
                continue;
            if (!procs[i]->responsed()) {
                logger.print("process %p has no response", procs[i]);
                assoc[i]->detach();
                delete procs[i];
                procs[i] = new Subprocess(sorting.c_str());
                assoc[i]->attach(procs[i]);
            }
        }
    }
    logger.print("sorting done");
    std::for_each(tasks, tasks + N, [](Task *t) { t->reset(); });

    int topidx, heap_c = N, heap[NMAX];
    int32_t num[NMAX];
    auto cmp = [&num](int idxa, int idxb) { return num[idxa] > num[idxb]; };
    for (i = 0; i < N; i++) {
        num[i] = tasks[i]->getInteger();
        heap[i] = i;
    }
    std::make_heap(heap, heap + heap_c, cmp);
    while (--K) {
        //printf("K = %d, heap_c = %d, min num = %d\n", K, heap_c, num[heap[0]]);
        topidx = heap[0];
        std::pop_heap(heap, heap + heap_c, cmp);
        if (tasks[topidx]->eof()) {
            heap_c--;
            continue;
        }
        num[topidx] = tasks[topidx]->getInteger();
        std::push_heap(heap, heap + heap_c, cmp);
    }
    printf("%d\n", num[heap[0]]);
    return 0;
}
Exemplo n.º 4
0
    // determine if s can be segmented and calculate the effective length of the substring.
    bool canWordBreak(string& s, unordered_set<string>& wordDict) {
        if (wordDict.empty())
            return false;
        len = s.size(), min_len = max_len = wordDict.begin()->size();
        int temp;
        for (auto& word : wordDict)
        {
            temp = word.size();
            if (temp > max_len)
                max_len = temp;
            else if (temp < min_len)
                min_len = temp;
        }

        vector<bool> flag(len + 1);
        flag[len] = true;
        for (int i = len - 1; i >= 0; --i)
            for (int j = min_len; j <= min(max_len, len - i); ++j)
                if (flag[i + j] && wordDict.find(s.substr(i, j)) != wordDict.end())
                {
                    flag[i] = true;
                    break;
                }

        return flag[0];
    }
Exemplo n.º 5
0
 int ladderLengthHelper(unordered_set<string>& words1, unordered_set<string>& words2, unordered_set<string>& wordList, int level) {
     if (words1.empty())
         return 0;
     if (words1.size() > words2.size())
         return ladderLengthHelper(words2, words1, wordList, level);
     unordered_set<string> words3;  // the new layer
     for (auto it = words1.begin(); it != words1.end(); ++it)
     {
         string word = *it;
         for (auto ch = word.begin(); ch != word.end(); ++ch)
         {
             char tmp = *ch;
             for (*ch = 'a'; *ch <= 'z'; ++(*ch))
             {
                 if (*ch == tmp)
                     continue;
                 if (words2.find(word) != words2.end())
                     return level + 1;
                 if (wordList.find(word) != wordList.end())
                 {
                     wordList.erase(word);
                     words3.insert(word);
                 }
             }
             *ch = tmp;
         }
     }
     return ladderLengthHelper(words2, words3, wordList, level + 1);  // put words2 first
 }
Exemplo n.º 6
0
 /** Provide a number which is not assigned to anyone.
     @return - Return an available number. Return -1 if none is available. */
 int get() {
     if(pool.empty()) return -1;
     auto it = pool.begin();
     int res = *it;
     pool.erase(it);
     return res;
 }
// 双向BFS搜索
// --------                     ----------
// |       |                    |        |
// |words1 |  <---------------> | words2 |
// |       |                    |        |
// ---------                    ----------   
// 不断地从两端向中心搜索,直至搜索到中间单词temp在另一个词袋中
int searchwithDoubleBFS(unordered_set<string>& words1, unordered_set<string>& words2,
	unordered_set<string>& dict, int level) {
	
	if (words1.empty()) return 0;		
	if (words1.size() > words2.size()) 
		return searchwithDoubleBFS(words2, words1, dict, level);
	
	unordered_set<string> words3;
	for (auto it = words1.begin(); it != words1.end(); ++it) {
		string word = *it;
		for (size_t i = 0; i < word.size(); ++i) {
			int ch = word[i];
			for (int k = 0; k < 26; ++k) {
				int ch2 = 'a' + k;
				if (ch2 != ch) {
					string temp = word;
					temp[i] = ch2;
					if (words2.find(temp) != words2.end()) {
						return level+1;
					} else {
						if (dict.find(temp) != dict.end()) {
							dict.erase(temp);
							words3.insert(temp);
						}
					}
					
				}
			}
		}
	}
	return searchwithDoubleBFS(words3, words2, dict, level+1);
}
Exemplo n.º 8
0
 //If there is no '&' before set, the time cost will be tripled;
 int ladderLength(unordered_set<string>& bList, unordered_set<string>& eList, unordered_set<string>& wordList, int len)
 {
     if(bList.empty())
         return 0;
     if(bList.size() > eList.size())
         return ladderLength(eList, bList, wordList, len);
     unordered_set<string> tmpList;
     for(auto it=bList.begin(); it!=bList.end(); it++)
     {
         string cur = *it;
         for(int i=0; i<cur.size(); i++)
         {
             char c0 = cur[i];
             for(char c='a'; c<='z'; c++)
             {
                 cur[i]=c;
                 if(eList.count(cur)) return len+1;
                 if(wordList.count(cur))
                 {
                     tmpList.insert(cur);
                     wordList.erase(cur);
                 }
             }
             cur[i] = c0;
         }
     }
     return ladderLength(tmpList, eList, wordList, len+1);
 }
Exemplo n.º 9
0
    void recursiveFindLadder(vector<string>& ladder, unordered_set<string>& pool) {
        if (pool.empty()) {
            return;
        }
        
        printLadder(ladder);
        printPool(pool);
        printf("\n");

        string beginWord = ladder[ladder.size()-1];

        vector<string> candidates;
        for (unordered_set<string>::iterator it = pool.begin(); it != pool.end(); ++it) {
            int diffCount = diffWord(beginWord, *it, NULL);
            if (diffCount == 1) {
                candidates.push_back(*it);
            }
        }
        
        for (int i=0; i< candidates.size(); ++i) {
            ladder.push_back(candidates[i]);
            pool.erase(candidates[i]);
            recursiveFindLadder(ladder, pool);
        }
    }
Exemplo n.º 10
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     vector<string> result;
     vector<string> path;
     vector<bool> flag;
     bool breakable = false;
     int pre=0;
     if(wordDict.empty())
         return result;
     for(int i=0; i<s.size(); ++i){
         if(i != 0)
             if(flag[i-1] == false)
                 continue;
         
         for(auto word : wordDict){
             if(s.compare(i,word.size(),word) == 0){
                 if(i+word.size() == s.size())
                     breakable = true;
                 flag[i+word.size()-1] = true;    
                     
             }
                 
         }
     }
     if(breakable == false)
         return result;
     dfs(s, wordDict, flag, 0,path, result);
     return result;
 }
Exemplo n.º 11
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     vector<string> res;
     if (s.empty() || wordDict.empty()) return res;
     int n = s.size();
     vector<bool> dp(n+1, false);
     vector<vector<string> > m(n+1, vector<string>());
     dp[0] = true;
     for (int i = 1; i <= n; i++) {
         for (int j = 0; j < i; j++) {
             string cur = s.substr(j, i-j);
             if (dp[j] && wordDict.find(cur) != wordDict.end()) {
                 dp[i] = true;
                 if (m[j].empty()) m[i].push_back(cur);
                 else {
                     vector<string> tmp = m[j];
                     for (int k = 0; k < tmp.size(); k++) {
                         string s_tmp = tmp[k]+" "+cur;
                         m[i].push_back(s_tmp);
                     }
                 }
             }
         }
     }
     return m[n];
 }
    bool wordBreak(string s, unordered_set<string> &dict) {
		int n;
		int i, j;
		string str;
		vector<int> dp;
		
		n = (int)s.length();
		if (n == 0 || dict.empty()) {
			return false;
		}
		dp.resize(n);
		for (i = 0; i < n; ++i) {
			str = s.substr(0, i + 1);
			if (dict.find(str) != dict.end()) {
				dp[i] = 1;
			} else {
				for (j = 0; j < i; ++j) {
					if (dp[j] && dict.find(s.substr(j + 1, i - j)) != dict.end()) {
						dp[i] = 1;
						break;
					}
				}
				if (j == i) {
					dp[i] = 0;
				}
			}
		}
		
		i = dp[n - 1];
		dp.clear();
		return i == 1;
    }
Exemplo n.º 13
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     vector<string> ans;
     if (s.empty() || wordDict.empty())
         return ans;
     int small = INT_MAX, big = 0;
     for (auto word : wordDict) {
         if (word.size() < small)
             small = word.size();
         if (word.size() > big)
             big = word.size();
     }
     vector<bool> flag(s.size()+1, false);
     flag[s.size()] = true;
     for (int i = s.size()-1; i>=0; i--) {
         for (int len = small; len <= big && i + len <= s.size(); len++) {
             if (flag[i+len] && wordDict.find(s.substr(i, len)) != wordDict.end()) {
                 flag[i] = true;
                 break;
             }
         }
     }
     if (flag[0] == false)
         return ans;
     dfs(s, wordDict, 0, "", ans, flag, small, big);
     return ans;
 }
Exemplo n.º 14
0
int ladderLength(string start, string end, unordered_set<string> &dict)
{
    if (dict.empty() || dict.find(start) == dict.end() || dict.find(end) == dict.end())
        return 0;

    queue<string> q;
    q.push(start);
    unordered_map<string, int> visited;  // visited track the distance
    visited[start] = 1;
    unordered_set<string> unvisited = dict;  // unvisited prevent searching through the whole dict
    unvisited.erase(start);

    while (!q.empty()) {
        string word = q.front(); q.pop();
        auto itr = unvisited.begin();
        while (itr != unvisited.end()) {
            string adjWord = *itr;
            if (oneCharDiff(word, adjWord)) {
                visited[adjWord] = visited[word] + 1;
                if (adjWord == end)
                    return visited[adjWord];
                itr = unvisited.erase(itr);  // tricky here
                q.push(adjWord);
            }
            else
                ++itr;
        }
    }
    return 0;
}
Exemplo n.º 15
0
 bool buildTree(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, 
 unordered_map<string, vector<string>> &tree, bool reversed){
       if (forward.empty())
             return false;
       if (forward.size() > backward.size())
       	return buildTree(backward, forward, dict, tree, !reversed);
       for (auto &word : forward)
       	dict.erase(word);
       for (auto &word : backward)
       	dict.erase(word);
       unordered_set<string> nextLevel;
       bool done = false; // guarantee shortest ladder
       for (auto &it : forward){ //traverse each word in the forward -> the current level of the tree;
             string word = it;
             for (auto &c : word){
                   char c0 = c; //store the original;
                   for (c = 'a'; c <= 'z'; ++c) //try each case;
                   if (c != c0){ //avoid futile checking;
                         //using count is an accelerating method;
                         if (backward.count(word)){    // count is O(1) while isChangedByOne is O(size(word)* size(backward))
                               done = true;
                               //keep the tree generation direction consistent;
                               !reversed ? tree[it].push_back(word) : tree[word].push_back(it); 
                         }
                         else if (!done && dict.count(word)){
                               nextLevel.insert(word);
                               !reversed ? tree[it].push_back(word) : tree[word].push_back(it);
                         }
                   }
                   c = c0; //restore the word;
       	}
       }
       return done || buildTree(nextLevel, backward, dict, tree, reversed);
 }
Exemplo n.º 16
0
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        if (start == end)
            return 1;
        if (dict.empty())
            return 0;

        queue<string> q;
        unordered_map<string, int> vis;
        unordered_set<string> unvis(dict);

        q.push(start);
        vis[start] = 1;
        unvis.insert(end);
        while (!q.empty()) {
            string str = q.front();
            q.pop();
            auto itr = unvis.begin();
            while (itr != unvis.end()) {
                string word = *itr;
                if (checkChar(word, str)) {
                    if (word == end)
                        return vis[str] + 1;
                    vis[word] = vis[str] + 1;
                    itr = unvis.erase(itr);
                    q.push(word);
                } else {
                    ++itr;
                }
            }
        }
        return 0;
    }
Exemplo n.º 17
0
 bool helper(unordered_set<string>& word1, unordered_set<string>& word2, unordered_set<string>& dict){
     if(word1.empty()) return 0;
     if(word1.size() > word2.size()) return helper(word2, word1, dict);
     for(auto v : word1) dict.erase(v);
     for(auto v : word2) dict.erase(v);
     
     ++res;
     bool reach = false;
     unordered_set<string> word3;
     for(auto it = word1.begin(); it != word1.end(); ++it){
         string word = *it;
         for(int i = 0; i < word.size(); ++i){
             char letter = word[i];
             for(int k = 0; k < 26; ++k){
                 word[i] = 'a'+k;
                 if(word2.count(word)){
                     reach = true;
                     return reach;
                 }
                 if(dict.count(word) && !reach){
                     word3.insert(word);
                 }
             }
             word[i] = letter;
         }
     }
     return reach || helper(word2, word3, dict);
 }
Exemplo n.º 18
0
  vector<string> wordBreak(string s, unordered_set<string> &dict) {
    
    m_result.clear();
    if(dict.empty() || s.empty()){return m_result;}
    
    m_s = s;
    m_dict = dict;
    m_len = s.length();
      
    int pos;
    string stmp;
    
    for(pos = m_len-1; pos >= 0; pos--)
      {
	stmp = m_s.substr(pos, m_len-pos);
	if(m_dict.count(stmp) == 1)
	  {
	    dfs(pos, stmp);
	  }
  
      }
        
    return m_result;
   
  }
Exemplo n.º 19
0
// This method uses dynamic programming to check whether that word is composed
// of elements from the periodic table or not.
//
// e.g of word = "Helicopter"
// 1. Overlapping subproblems.
// 2. Optimal substructure.
// Let's say we are evaluating the element at i -> So DP[i]
// DP[i] = true, if word[i] exists in ptable && DP[i-1] == true;
//				 || DP[i-2] == true && word[i-1,i] exists in ptable
// else returns false; 
//
bool ExistsInPeriodicTable(char* word, unordered_set<string>& ptable)
{
	if (nullptr == word || ptable.empty())
		return false;
	
	bool dpresult = false;
	bool dp1 = true;
	bool dp2 = true;

	int len = strlen(word);
	string singleCharElem;
	singleCharElem.assign(word, 1);
	string doubleCharElem;
	if (ptable.find(singleCharElem) != ptable.end())
		dpresult = true;

	for (int i = 1; i < len; i++)
	{
		singleCharElem.clear();
		doubleCharElem.clear();
		singleCharElem.assign(word + i, 1);
		doubleCharElem.assign(word + i - 1, 2);
			dpresult = dp1 && (ptable.find(singleCharElem) != ptable.end()) || dp2 && (ptable.find(doubleCharElem) != ptable.end());
		
		dp2 = dp1;
		dp1 = dpresult;
	}

	return dpresult;
}
 bool wordBreak(string s, unordered_set<string>& wordDict)
 {
     if(wordDict.empty()) return false;
     if(s.empty() ) return true;
     for(auto str : wordDict)
     {
         size_t pos = s.find(str);
         cout << "str : " << str << " pos : " << pos << endl;
         if( pos != string::npos)
         {
             if(pos == 0)
             {
                 string temp(s.begin()+str.size(), s.end());
                 if(wordBreak(temp, wordDict))
                 {
                     return true;
                 }
             }
             else
             {
                 string temp1(s.begin(), s.begin()+pos);
                 string temp2(s.begin()+pos+str.size(), s.end());
                 if(wordBreak(temp1, wordDict) && wordBreak(temp2,wordDict))
                 {
                     return true;
                 }
             }
         }
     }
     return false;
 }
Exemplo n.º 21
0
	vector<string> wordBreakII(string s, unordered_set<string> &dict) {
        vector<string> v;
		if(s.size()==0||dict.empty())
			return v;
		vector<vector<int> > seq(s.size()+1, vector<int>());
		seq[0].push_back(-1);
		for(int i=1;i<=s.size();i++){
			for (int j = 0; j < i; j++) {
				if(seq[j].size()>0&&dict.find(s.substr(j,i-j))!=dict.end())
					seq[i].push_back(j);
			}
		}
		queue<pair<string, int> > q;
		for(int j=0; j<seq[s.size()].size();j++)
			q.push(make_pair(s.substr(seq[s.size()][j],s.size()-seq[s.size()][j]),seq[s.size()][j]));
		while(!q.empty()){
			pair<string, int> tmp=q.front();
			q.pop();
			if(seq[tmp.second][0]==-1){
				v.push_back(tmp.first);
			}
			else{
				for(int i=0;i<seq[tmp.second].size();i++){
					string stmp=s.substr(seq[tmp.second][i],tmp.second-seq[tmp.second][i])+" "+tmp.first;
					q.push(make_pair(stmp, seq[tmp.second][i]));
				}
			}
		}
		return v;
    }
Exemplo n.º 22
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     vector<string> result;
     string path("");
     if (s.empty() || wordDict.empty()) return result;
     vector<int> possible(s.size(),1);
     dfs(s,wordDict,0,result,path,possible);
     return result;
 }
Exemplo n.º 23
0
	void pop(){
		// if (!priority_queue::empty()){
		if (!nodes.empty()){
			cout << "popping" << endl;
			nodes.erase(priority_queue::top().name);
			// Node temp = priority_queue::top();
			priority_queue::pop();
			
		}
	}
Exemplo n.º 24
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     for(auto w:wordDict){
         w.size()>max_len ? max_len=w.size() : max_len;
         w.size()<min_len ? min_len=w.size() : min_len;
     }
     vector<string> res;
     if(!wordDict.empty())
         wordBreak(s, wordDict, res, 0);
     return inter_res[s.size()-1];
 }
Exemplo n.º 25
0
 vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
     for(auto w:wordDict){
         w.size()>max_len ? max_len=w.size() : max_len;
         w.size()<min_len ? min_len=w.size() : min_len;
     }
     unordered_map<int, vector<string>> inter_res;   // inter
     if(!wordDict.empty())
         wordBreak(s, wordDict, inter_res, 0);
     return inter_res[0];
 }
Exemplo n.º 26
0
 int ladderLength(string start, string end, unordered_set<string> &dict) {
     // Start typing your C/C++ solution below
     // DO NOT write int main() function
     if(start.size()!=end.size() || dict.empty())
         return 0;
     //dict.insert(end);
     queue<string> q[2];
     int cur = 0;
     q[cur].push(start);
     int length = 0;
     bool find = false;
     while(!q[cur].empty())
     {
         while(!q[cur].empty())
         {
             string s = q[cur].front();
             q[cur].pop();
             int ssize = s.size();
             for(int i = 0; i < ssize && !find; i++)
             {
                 string tmp = s;
                 for(int j = 'a'; j <= 'z'; j++)
                 {
                     tmp[i] = j;
                     if(tmp == s)
                     {
                         dict.erase(s);
                         continue;
                     }
                     if(tmp == end && length != 0)
                     {
                         find = true;
                         break;
                     }
                     if(dict.find(tmp) != dict.end())
                     {
                         if(tmp == end)
                         {
                             find = true;
                             break;
                         }
                         q[cur^1].push(tmp);
                         dict.erase(tmp);
                     }
                 }
             }
             if(find)
                 return length+2;
         }
         cur ^= 1;
         length++;
     }
     return 0;
 }
Exemplo n.º 27
0
   vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        vector<vector<string> > ret;
        unordered_map<string,string> previous;
     
        queue<pair<string,int> > coada;
        coada.push(make_pair(start,0));
		dict.erase(start);
		dict.erase(end);
        if(start==end && dict.empty()==true ) 
            return ret;
        pair<string,int> curent;
		previous[start]="###";
        int n;
		int lungime = 0;
        while(coada.empty()==false){
			curent = coada.front();
			coada.pop();
			if(lungime !=0 ){
				if(curent.second+1 !=lungime) break;
			}
            n = curent.first.length()-1;
            string aux = curent.first;
            char c ;
            for(int i=0;i<=n;++i){
                c = aux[i];
                for(int j='a';j<='z';++j){
                    aux[i]=j;
                    if(curent.first == aux) continue;
                    if(aux==end){
                        vector<string> forret;
                        forret.push_back(end);
						forret.push_back(curent.first);
						string prev = previous[curent.first];
						
						while(prev!="###"){
							forret.push_back(prev);
							prev = previous[prev];
						}
						reverse(forret.begin(),forret.end());
						ret.push_back(forret);
						lungime = curent.second+1;
                    }else
                    if(dict.find(aux)!=dict.end()){
                        coada.push(make_pair(aux,curent.second+1));
						previous[aux]=curent.first;
						dict.erase(aux);
                    }
                }
				 aux[i] = c;
            }
        }
		
        return ret;
    }
Exemplo n.º 28
0
    vector<string> wordBreak(const string& s, const unordered_set<string>& wordDict) {
        vector<string> result;
        if (wordDict.empty() || s.empty()) {
            return std::move(result);
        }

        vector<bool> possible(s.size() + 1, true);
        vector<string> stack;
        this->recursiveSolve(result, stack, possible, s.cbegin(), s, wordDict);

        return std::move(result);
    }
Exemplo n.º 29
0
    bool wordBreakI(string s, unordered_set<string> &dict) {
        if(s.size()==0)
			return true;
		if(dict.empty())
			return false;
		vector<bool> flag(s.size()+1, false);
		flag[0]=true;
		for(int i=1;i<=s.size();i++){
			for (int j = 0; j < i && !flag[i]; j++) {
				flag[i] = flag[j] && dict.find(s.substr(j,i-j))!=dict.end();
			}
		}
		return flag[s.size()];
    }
Exemplo n.º 30
0
 bool canForm(const string& s, const unordered_set<string>& st) {
     if(st.empty()) return false;
     int n = s.size();
     vector<bool> dp(n+1);
     dp[0] = true;
     for(int i = 1; i <= n; i++) {
         for(int j = 0; j < i; j++) {
             if(dp[j] && st.count(s.substr(j, i - j))) {
                 dp[i] = true;
                 break;
             }
         }
     }
     return dp[n];
 }