# Programming Algorithms-Dictionary Participle Code (C)

Source: Internet
Author: User

Dictionary word breaker Code (C)

given a dictionary, given a word, word breaker.

Methods that use deep traversal (DFS) .

Use a parameter stringto save the sentence after the current branch's participle; Use one of the vector vectorsto save all possible combinations.

Use a validation function to infer whether a sentence is enough to be participle.

Code:

`/* * main.cpp * * Created on:2014.9.18 * author:spike * COPYRIGHT (c) 2014 WCL. All rights reserved. *//*eclipse CDT, gcc 4.8.1*/#include <iostream> #include <vector> #include <string> #include <set >using namespace Std;bool Match (string s, string m) {int L = m.length (); if (s.substr (0, l) = = m) {return true;} return false;} BOOL Validate (string s, vector<string> &dict) {//1. Calculate all alphabets in the queryset<char> sc;for ( size_t i = 0; I < s.length (); i++) {Sc.insert (s[i]);} 2. Calculate all alphabets in the dictionaryset<char> dc;for (vector<string>::iterator it = Dict.begin (); it! = di Ct.end (); it++) {for (size_t i = 0; i < (*it). Length (); i++) {Dc.insert ((*it) [i]);}} for (Set<char>::iterator it = Sc.begin (); It! = Sc.end (); it++) {if (Dc.find (*it) = = Dc.end ()) {return false;}} return true;} String Split (string s, vector<string> &dict, string cur, vector<string>& list) {if (s.length () = = 0) { List.puSh_back (cur); return s;} for (Vector<string>::iterator it = Dict.begin (); It! = Dict.end (); it++) {if (Match (S, *it)) {string tmp = Cur;strin G latter = S.substr (It->length (), s.length ()-it->length ()), cur + = (*it) + "~"; Add current Word to cur_strcur + = Split (latter, dict, cur, list); Split remaining wordscur = tmp; Back-to-last Status}}return "No Result";} Vector<string> splitwords (string s, vector<string> &dict) {string cur = "";vector<string> list;if (! Validate (S, dict)) {return list;} Split (S, dict, cur, list); return list;}    int main () {vector<string> dict={"program Ape", "Civil servant", "member", "I", "HI", "Do", "program", "One", "Huan", "like", "do One", "one"};    vector<string> words = Splitwords ("I like to do a program ape", Dict);    For (Vector<string>::iterator It=words.begin (); It!=words.end (); it++) {cout<< (*it) <<endl; } return 0;}`

Simplified version number (no validation):

`/* * main.cpp * * Created on:2014.9.18 * author:spike * COPYRIGHT (c) 2014 WCL. All rights reserved. *//*eclipse CDT, gcc 4.8.1*/#include <iostream> #include <vector> #include <string> #include <set >using namespace Std;bool Match (string s, string m) {int L = m.length (); if (s.substr (0, l) = = m) {return true;} return false;} String Split (string s, vector<string> &dict, string cur, vector<string>& list) {if (s.length () = = 0) { List.push_back (cur); return s;} for (Vector<string>::iterator it = Dict.begin (); It! = Dict.end (); it++) {if (Match (S, *it)) {string tmp = Cur;strin G latter = S.substr (It->length ()); cur + = (*it) + "|"; Add current Word to cur_strcur + = Split (latter, dict, cur, list); Split remaining wordscur = tmp; Back-to-last Status}}return "No Result";} Vector<string> splitwords (string s, vector<string> &dict) {string cur = "";vector<string> list; Split (S, dict, cur, list); return list;}   int main () { vector<string> dict={"program Ape", "Civil servant", "member", "I", "HI", "Do", "program", "One", "Huan", "like", "do One", "one"};    string s = "I like to be a program ape";    vector<string> words = Splitwords (s, dict);    For (Vector<string>::iterator It=words.begin (); It!=words.end (); it++) {cout<< (*it) <<endl; } return 0;}`

Output:

`I ~ Happy ~ to do ~ a ~ Program Ape ~ I ~ Happy ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ I ~ ~ ~ ~ ~ I ~ the program Ape ~ i ~ Joy ~ do a ~ program ~ Staff ~ i ~ like ~ do ~ a ~ Program Ape ~ I ~ like ~ do ~ a ~ program ~ Staff ~ i ~ like ~ do a program Ape ~ i ~ like ~ to do a ~ Program ~ Staff ~`

Programming Algorithms-Dictionary Participle Code (C)

Related Keywords:
Related Article

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.