[Leetcode] Word break

Source: Internet
Author: User

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given

S = "leetcode ",

Dict = ["Leet", "code"].

Return true because "leetcode" can be segmented as "Leet code ".

Solution:

LTE code:

string srcStr;unordered_set<string> dic;bool word_break(int start){    //cout << "Start with " << start << endl;    if(start >= srcStr.length())         return true;    for(int i = start;i < srcStr.length();i++)    {        string curStr = srcStr.substr(start, i - start + 1);        if(dic.find(curStr) != dic.end())        {            //return word_break(i+1); //The reason is that if current recursion return false, doest not mean the             //break should fail, we need to check other combinations.            if(word_break(i + 1))                 return true;            else                 continue;        }        else continue;    }    return false;}bool wordBreak(string s, unordered_set<string> &dict) {        srcStr = s;        dic = dict;        return word_break(0);    }

AC code:

string srcStr;unordered_set<string> dic;int visited[1000][1000];int checked[1000];string srcStr;unordered_set<string> dic;int visited[1000][1000];int checked[1000];bool word_break(int start){    //cout << "Start with " << start << endl;    if(start >= srcStr.length())         return true;    for(int i = start;i < srcStr.length();i++)    {        string curStr = srcStr.substr(start, i - start + 1);        int finded = 0;        if(visited[start][i] == 1)            finded = true;        else if(visited[start][i] == 2)            finded = false;        else        {            finded = (dic.find(curStr) != dic.end()) ? 1 : 2;            visited[start][i] = finded;        }        if(finded == 1)        {            if(checked[i + 1] == 1)                return true;            else if(checked[i + 1] == 2)                continue;            else             {                checked[i + 1] = word_break(i + 1) ? 1 : 2;                if(checked[i + 1] == 1)                    return true;                else                    continue;            }        }        else continue;    }    return false;}bool wordBreak(string s, unordered_set<string> &dict) {        srcStr = s;        dic = dict;        for(int i = 0;i < 1000;i++)            memset(visited[i], 0, sizeof(int) * 1000);        memset(checked, 0, sizeof(int) * 1000);        if(s.length() > 1000) return false;        return word_break(0);    }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.