[Leetcode] Word break

Source: Internet
Author: User

Given AstringS and a Dictionary of words dict, determineifs can is segmented into a space-separated sequence of one or more dictionary words. For example, given S="Leetcode", Dict= ["Leet","Code"]. ReturntrueBecause"Leetcode"can be segmented as "leet Code".

 Analysis: The input is a unordered-set<string>& worddict, a string& s, and the output is a bool value that determines whether the string s can be decomposed into a string value in Worddict.

Analyzing the problem we found that if s can be decomposed into worddict word combinations, then, for a split s->s1,s2 in its split answer, dividing s into S1 and S2,S1 and S2 must also be divided into worddict words. That is, the division of S includes the segmentation of S1 and S2, the optimal solution of the problem contains the optimal solution of its sub-problem, which has the optimal substructure property.

Now we can try to solve the problem by recursive method, we s1 each of the two characters in S to traverse S, if the division of the resulting and S2 can be decomposed into worddict words, we say s can be decomposed into worddict words, If S1 and S2 cannot be broken down into words in worddict, continue traversing s. If the split point does not have two S1 and S2 that can be decomposed until the last character in S, we say that s cannot be decomposed into S1 and S2 words.

#include <iostream>#include<vector>#include<string>#include<unordered_set>#include<algorithm>using namespacestd;classsolution{ Public:    BOOLWordbreak (strings,unordered_set<string>&worddict) {        if(worddict.size () = =0)//Boundary Condition 1        {            return false; }        if(s.size () = =0)//Boundary Condition 2        {            return false; }        if(Find (Worddict.begin (), Worddict.end (), s)! = Worddict.end ())//Recursive exit Criteria        {            return true; }intSize =s.size (); if(Size = =1)//If the worddict is not found s,s and cannot continue to split, return false        {            return false; }         for(intI=1; i<size;i++)//traversing every split point on S        {            stringS1 = S.substr (0, i); stringS2 =s.substr (i,size); if(Wordbreak (s1,worddict) &&Wordbreak (s2,worddict)) {                return true; }        }        return false; }};

Analysis of the recursive method, we found that for each s, we have to traverse its two characters between the split point, and S split out of the S1,S2, but also contains the same character sequence, that is, we repeatedly solved a lot of the same string, so the above recursive solution is very inefficient. Since we have judged that the sub-problems of the problem overlap, we use a dynamic programming approach.

A recursive solution has been obtained above, so we use the top-down method with a memo in the dynamic programming solution for this problem, the solution we require is for a specific string, the corresponding bool value, so we use a global map:map<string,bool> MP to save the results each time. Check to see if you have learned before each recursion and save the solution in the map after recursion.

#include <iostream>#include<vector>#include<string>#include<unordered_set>#include<algorithm>#include<map>using namespacestd;classsolution{ Public: Map<string,BOOL>MP; BOOLWordbreak (strings,unordered_set<string>&worddict) {        if(worddict.size () = =0)//Boundary Condition 1        {            return false; }        if(s.size () = =0)//Boundary Condition 2        {            return false; }        if(Find (Worddict.begin (), Worddict.end (), s)! = Worddict.end ())//Recursive exit Criteria        {            return true; }        intSize =s.size (); if(Size = =1)//If the worddict is not found s,s and cannot continue to split, return false        {            return false; }         for(intI=1; i<size;i++)//traversing every split point on S        {            stringS1 = S.substr (0, i); stringS2 =s.substr (i,size); BOOLFlag1 =false; BOOLFlag2 =false; BOOLIs1 =false; BOOLIs2 =false; if(Mp.empty () = =false&& (Mp.find (s1)! = Mp.end ()))//If you have obtained the solution of S1, take it directly from MP. {is1=true; Flag1= (*Mp.find (S1)). Second; }            if(Mp.empty () = =false&& (mp.find (s2)! = Mp.end ()))//If you have obtained the solution of S2, take it directly from MP. {Is2=true; Flag2= (*mp.find (S2)). Second; }            if(Is1 = =false)//If the S1 solution is not obtained, it is solved and saved in MP. {Flag1=Wordbreak (s1,worddict); MP[S1]=Flag1; }            if(Is2 = =false)//If the S2 solution is not obtained, it is solved and saved in MP. {Flag2=Wordbreak (s2,worddict); MP[S2]=Flag2; }            if(Flag1 &&Flag2) {                return true; }        }        return false; }};

[Leetcode] Word break

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.