Given a dictionary d of a string s and a valid word, determine the minimum number of spaces that can be inserted into s, so that the final string is completely composed of valid words in D and output the solution.
If there is no solution, you should output N/a
For example
Input
S = "Ilikealibaba"
D = ["I", "like", "Ali", "Liba", "Baba", "Alibaba"]
Example Output:
Output
"I like Alibaba" explains:
String s may be split by dictionary D
"I like Ali Baba"
"I like Alibaba" Obviously, the second check result is the least number of spaces in the solution.
The AC code is as follows
#include <iostream> #include <set> #include <string> #include <vector> using namespace std;
int Fun (String str1, string str2, int a) {int count = 0; int i = A;
int j = 0;
while (I < Str1.size () && J < Str2.size () && str1[i] = = Str2[j]) {count++; i++;
j + +;
if ((I==str1.size ()) && (J==str2.size ()) | | | j = = str2.size ()) return count;
return 0;
} void Mincut (const string& STR, const set<string>& dict) {vector<string> res;
if (Str.empty () | | | Dict.empty ()) {cout << "n/a" << Endl;
Return
int i = 0;
for (; i < str.size (); i++) {char c = str[i];
Set<string>::iterator it = Dict.begin ();
for (; it!= dict.end (); it++) {if ((*it) [0] = = c) break;
int maxlen = 0;
while (it!= dict.end () && (*it) [0] = = c) {int len = fun (str, *it, i);
if (MaxLen < len) maxlen = len;
it++; } if (MaxLen = 0) {cout << "n/a" <<Endl
Return
The string temp = Str.substr (i, maxlen);
cout << temp << "";
Res.push_back (temp);
i + = maxlen-1;
} if (i = = Str.size ()) {for (auto x:res) cout << x << "";
cout << Endl;
else cout << "N/a" << Endl;
int main (int argc, const char * argv[]) {string STRs;
String Dictstr;
int ndict;
Set<string> dict;
Cin >> STRs;
Cin >> Ndict;
for (int i = 0; i < ndict i++) {cin >> dictstr;
Dict.insert (DICTSTR);
}//for (Auto x:dict)//cout << x << "";
Mincut (STRs, dict);
return 0; }