Ali 2018 School recruit written test programming questions

Source: Internet
Author: User
Ali 2018-Autumn recruit programming questions

1. 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

Example:

Input
S = "Ilikealibaba"
D = ["I", "like", "Ali", "Liba", "Baba", "Alibaba"]
Example Output:
Output
"I like Alibaba"
Explain:
String s may be split by dictionary D
"I like Ali Baba"
"I like Alibaba"
It is obvious that the second split result is the least number of spaces in the solution.

Ideas for solving problems

Define a two pointer left and Right,[left, right] to form a string window or Word word,left or right to start with the first character of S, the basic idea is: search S, as far as possible to find a longest window word[left,right] So that word is a valid character for dict, and if found, the left pointer can skip the Word.len position and continue searching right until the end of S.
However, according to this idea, "finding as long as possible the longest word[left, right]" is likely to cause the destruction of the string after right to match, so that the problem no solution. Like what:
s= "AAAABBCCAA" Dict=[aa, AABBCC, Aabbcca, BC], if according to the above problem-solving ideas, then will find no solution, that is, in the search process, encounter AABBCC and Aabbcca This situation, select the latter, and then lead to the last only a, Unable to match, the damage of AABBCC and AA matches.
PS: See other people on the internet with this idea, you can pass Ali's test sample, then his test sample has a problem

C + + code:

#include <iostream> #include <set> #include <string> #include <vector> using namespace std; void mincut (const string& STR, const set<string>& dict) {if (Str.empty () | | Dict.empty ()) {Cou
        T << "n/a";
    Return
    int left = 0,right = 1, len = Str.size ();
    Vector<string> selections;
        while (right < Len) {string word = Str.substr (left,right-left);
            if (Dict.find (word)!=dict.end ()) {int E = right + 1;
            while (e <= len && dict.find (Str.substr (Left,e-left)) ==dict.end ()) e++;
                if (e <= len) {selections.push_back (Str.substr (left,e-left));
            left = right = e;
                } else{Selections.push_back (word);
            left = right;
    }} right++;
    } cout<<right<< "" <<left<<endl; if (right-left>1) {cout<< "N/a";
        else{len = selections.size ();
    for (int i=0; i<len; i++) cout<<selections[i]<< "";
    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);
    } 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.