Ali School Entrance Examination--given a dictionary of string s and valid words D, determine the minimum number of spaces that can be inserted into s, so that the final string is entirely composed of valid words in D and output the solution.

Source: Internet
Author: User

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; }


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.