[Leetcode] Word Break Terry

Source: Internet
Author: User

Serie A: Infer whether the string given by the string constitutes a dictionary.

To infer that the target string matches the entire dictionary. We need to infer that each prefix of the target string starts with the next race, which needs to match the success of the target string with the enumeration of all prefixes.


Class Trienode{//from http://www.cnblogs.com/x1957/p/3492926.htmlpublic:trienode* ch[26];//char pointer array bool IsWord;    Trienode (): Isword (False) {memset (ch,0,sizeof (trienode*) *26);        } void Insert (const string& PS) {trienode*q=this;        int id;        Const char* P=PS.C_STR ();            while (*p) {id=p[0]-' a ';            if (Null==q->ch[id]) q->ch[id]=new trienode ();            q=q->ch[id];        p++;    } q->isword=true;//is a prefix} ~trienode () {for (int i=0;i<26;++i) Delete ch[i];    }};class solution {Public:bool *find;    Trienode *root;        void Match (String &s,int st,int ed) {trienode*p=root;                for (int i=st;i<ed;++i) {if (p->ch[s[i]-' a ']) {p=p->ch[s[i]-' a '];            if (P->isword) find[i]=true;        } else break; }} bool Wordbreak (string s, unordered_set<string> &dict) {inT i,n=s.size ();        Unordered_set<string>::iterator bg,ed;        Root=new Trienode ();        For (Bg=dict.begin (), Ed=dict.end (); bg!=ed;bg++) {Root->insert (*BG);        } find=new Bool[n];        memset (find,0,sizeof (bool) *n);        Matches the prefix match (s,0,n) first; Then start with all the matching words to match for (I=0;i<n&&find[n-1]==false;++i) if (Find[i]) match (s,i+1,        n);        BOOL Ans=find[n-1];        Delete[]find;        Delete root;    return ans; }};


DP version:

A string ab can be seen as two substrings of a, B constitute. Suppose both A and B match. Then AB matches. Use Dp[i] to indicate whether the prefix (0,i) matches, then Dp[n]=dp[0,k]&dp[k+1,n],k∈[0,n].

Here Dp[0,k] more easy, to infer whether the suffix dp[k+1,n] is in the dict. Except through Set.find (Dp[k+1,n]) I don't know what else to do.


BOOL Wordbreak (string s, unordered_set<string> &dict) {        int i,j,n=s.size ();        BOOL *dp=new bool[s.size ()]; memset (dp,0,sizeof (BOOL) *n), for (I=0;i<n;++i) {Dp[i]=dict.find (S.substr (0,i+1))!=dict.end (), for (j=0;j<i &&!DP[I];++J) {dp[i]= (dp[j]& (Dict.find (S.SUBSTR)) J+1,i-j ())}} BOOL Ans=dp[n-1];d elete[]dp;return ans;    }


Issue 2: Output all matching strings https://oj.leetcode.com/problems/word-break-ii/

Method: At each match, for each matched substring, records can be matched to the starting position of the substring at that location. One of the characters in such a string may have multiple matching results.

It's all possible to just backtrack from backwards.


Class Trienode{public:trienode*child[26];bool Isword; Trienode (): Isword (False) {memset (child,0,sizeof (trienode*) *26);} void Insert (const string &str) {int n=str.size (), I,id; Trienode*p=this;for (i=0;i<n;++i) {id= str[i]-' a '; if (!p->child[id]) {p->child[id]=new TrieNode ();} P=p->child[id];} P->isword=true;} ~trienode () {for (int i=0;i<26;++i) {delete child[i];child[i]=null;}}}; Class Solution{public:trienode*root;bool *find;vector<string>ans;void Match (String &str,int st,int Ed, Vector<set<int> >&pos) {int i,id; Trienode*p=root;for (i=st;i<=ed;++i) {id=str[i]-' a '; if (P->child[id]) {p=p->child[id];if (P->isWord) { Find[i]=true;pos[i].insert (ST);}} else break;}} void Dfs (String &str,int id,vector<set<int> >&pos,int *b,int len) {if (id<0) {string tmp;for (int i =len-1;i>0;--i) {if (i<len-1) tmp+= ""; Tmp+=str.substr (B[i],b[i-1]-b[i]);} Ans.push_back (TMP); return;} Set<int>::iterator Bg=pos[id].begin (), Ed=pos[id].end (); for (; bg!=ed;bg++) {B[len]=*bg;dfs (str,b[len]-1,pos,b,len+1);}} Vector<string>wordbreak (string s, unordered_set<string>&dict) {if (S.size () ==0) {return vector< String> ();} Unordered_set<string>::iterator bg,ed;root=new trienode (); int n=s.size (), I; For (Bg=dict.begin (), Ed=dict.end (); bg!=ed;bg++) {Root->insert (*BG);} Find=new bool[n];vector<set<int> >pos (n); int *b=new Int[n+2];memset (find,0,sizeof (bool) *n); Match (s,0, N-1,pos); for (i=0;i<n;++i) {if (Find[i]) match (S,i+1,n-1,pos);} int x=0;//cout<<pos[6].size (); if (find[n-1]) {B[x++]=n;dfs (s,n-1,pos,b,x);} Delete[] find;delete[] b;return ans;};



Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

[Leetcode] Word Break Terry

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.