Dictionary word breaker Code (C)
This address: Http://blog.csdn.net/caroline_wendy
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)