算法基础__第7课(前缀树以及相关应用、分金条问题、IPO问题、数据流中的中位数、宣讲会的场次、字符串的最小拼接序列)

什么是前缀树、前缀树的基本特征、前缀树的应用、
所谓的字典树又被称为前缀树或者叫做trie树,是处理字符串的常用数据结构。其优点是利用字符串的公共前缀来
节约存储空间。其基本性质如下:
(1)根节点没有字符路径。除根节点之外,每一个节点都被一个字符路径找到。
(2)从根节点出发到任何一个节点,如果将沿途 的字符连接起来,一定是某个字符串的前缀。
(3)每个节点向下所有的字符路径上的字符都不同。
实现一个 Trie,包含 insert, search, 和 startsWith 这三个方法。
class TrieNode
{
public:
    int path;
    int ends;
    unordered_map<char, TrieNode *> m;//delete单词
};



class Trie {
public:
    TrieNode * root;
    Trie() {
        root = new TrieNode();//头节点
        // do intialization if necessary
    }
    
    /*
     * @param word: a word
     * @return: nothing
     */
    void insert(string &word) {
        // write your code here
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i]) == 1)
            {
                head = head->m[word[i]];
                head->path++;
                if(i == word.size() - 1 )
                    head->ends++;
            }
            else
            {
                TrieNode *node = new TrieNode();
                node->path = 1;
                if(i == word.size() - 1 )
                    node->ends++;
                else
                    node->ends = 0;
                head->m[word[i]] = node;
                head = node;
            }
        }
    }
    
    /*
     * @param word: A string
     * @return: if the word is in the trie.
     */
    bool search(string &word) {
        // write your code here
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i])== 1)
            {
                head = head->m[word[i]];
            }
            else
            {
                return false;
            }
        }
        if(head->ends >=1)
        {
            return true;
        }
        else
            return false;
        
        
    }
    
    /*
     * @param prefix: A string
     * @return: if there is any word in the trie that starts with the given prefix.
     */
    bool startsWith(string &prefix) {
        // write your code here
        
        TrieNode  *head  = root;
        for (int i = 0; i < prefix.size(); i++) {
            /* code */
            if(head->m.count(prefix[i] )== 1)
            {
                head = head->m[prefix[i]];
            }
            else
            {
                return false;
            }
        }
        return true;
        
        
    }
};
n English, we have a concept called root, which can be followed by some other words to form 
another longer word - let's call this word successor. For example, the root an, followed by 
other, which can form another word another.
Now, given a dictionary consisting of many roots and a sentence. You need to replace all 
the successor in the sentence with the root forming it. If a successor has many roots can 
form it, replace it with the root with the shortest length.
Example 1:

Input: dict = ["cat", "bat", "rat"]
sentence = "the cattle was rattled by the battery"
Output: "the cat was rat by the bat"
You need to output the sentence after the replacement.
The input will only have lower-case letters.
1 <= dict words number <= 1000
1 <= sentence words number <= 1000
1 <= root length <= 100
1 <= sentence words length <= 1000
class TrieNode
{
public:
    int path;
    int ends;
    unordered_map<char, TrieNode *> m;//delete单词
};



class Trie {
public:
    int index = 0;
    TrieNode * root;
    Trie() {
        root = new TrieNode();//头节点
        // do intialization if necessary
    }
    
    /*
     * @param word: a word
     * @return: nothing
     */
    void insert(string &word) {
        // write your code here
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i]) == 1)
            {
                head = head->m[word[i]];
                head->path++;
                if(i == word.size() - 1 )
                    head->ends++;
            }
            else
            {
                TrieNode *node = new TrieNode();
                node->path = 1;
                if(i == word.size() - 1 )
                    node->ends++;
                else
                    node->ends = 0;
                head->m[word[i]] = node;
                head = node;
            }
        }
    }
    
    /*
     * @param word: A string
     * @return: if the word is in the trie.
     */
    bool search(string &word) {
        // write your code here
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i]) == 1)
            {
                head = head->m[word[i]];//如果当前字符是结尾的话
            }
            else
            {
                return false;
            }
        }
        if(head->ends >=1)
        {
            return true;
        }
        else
            return false;
        
        
    }
        /*
     * @param word: A string
     * @return: if the word is in the trie.
     */
    bool isWord(string &word) {
        // write your code here
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i]) == 1)
            {
                head = head->m[word[i]];//如果当前字符是结尾的话
                if(head->ends >=1)
                {
                    index = i;
                    return true;//也就是说如果当前字符
                }  
            }
            else
            {
                return false;
            }
        }
        if(head->ends >=1)
        {
            return true;
        }
        else
            return false;
        
        
    }
    /*
     * @param prefix: A string
     * @return: if there is any word in the trie that starts with the given prefix.
     */
    bool startsWith(string &prefix) {
        // write your code here
        
        TrieNode  *head  = root;
        for (int i = 0; i < prefix.size(); i++) {
            /* code */
            if(head->m.count(prefix[i]) == 1)
            {
                head = head->m[prefix[i]];
            }
            else
            {
                return false;
            }
        }
        
        return true;
    }
    //查找某个字符的前缀是
};
class Solution {
public:
    string replaceWords(vector<string>& dict, string sentence) 
    {
          vector<string>v =  splitString(sentence);
          string s ="";
          Trie t;
        for(int i = 0; i < dict.size(); i++)
        {
            t.insert(dict[i]);
        }
            for(int j = 0; j < v.size(); j++)
            {
                if(t.isWord(v[j]))
                 v[j] = v[j].substr(0,t.index+1);
            }
         for(int j = 0; j < v.size()-1; j++)
             s+= v[j]+" ";
             s+=v[v.size() -1];
        return s;
    }
   vector<string> splitString(string str)
    {
    string ::size_type pos1 = 0,pos2 = 0;
    vector<string> res;
    while (str.find(" ",pos1) != string::npos) {
        pos2 = str.find(" ",pos1);//找到要查找的字符串
        res.push_back(str.substr(pos1, pos2 - pos1));
        pos1 = pos2+1;
    }
    if (pos1 != str.size()) {
        res.push_back(str.substr(pos1));
    }
    return res;
    }
    
};
//暴力解法
class Solution {
public:
    string replaceWords(vector<string>& dict, string sentence)
    {
        vector<string>v =  splitString(sentence);
        string s ="";
        for(int i = 0; i < dict.size(); i++)
        {
            for(int j = 0; j < v.size(); j++)
            {
                if(v[j].substr(0, dict[i].size()) == dict[i])//字符串截取操纵
                {
                    v[j] = dict[i];
                }
            }
            
        }
        for(int j = 0; j < v.size()-1; j++)
            s+= v[j]+" ";
        s+=v[v.size() -1];
        return s;
    }
    vector<string> splitString(string str)
    {
        string ::size_type pos1 = 0,pos2 = 0;
        vector<string> res;
        while (str.find(" ",pos1) != string::npos) {
            pos2 = str.find(" ",pos1);//找到要查找的字符串
            res.push_back(str.substr(pos1, pos2 - pos1));
            pos1 = pos2+1;
        }
        if (pos1 != str.size()) {
            res.push_back(str.substr(pos1));
        }
        return res;
    }
    
};
Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)
search(word) can search a literal word or a regular expression string containing only 
letters a-z or .. A . means it can represent any one letter.

Example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true
Note:
You may assume that all words are consist of lowercase letters a-z.
class TrieNode
{
public:
    int path;
    int ends;
    unordered_map<char, TrieNode *> m;//delete单词
};

class WordDictionary {
public:
    TrieNode * root;
    
    /** Initialize your data structure here. */
    WordDictionary()
    {
        root = new TrieNode();//头节点
    }
    
    /** Adds a word into the data structure. */
    void addWord(string word)
    {
        TrieNode  *head  = root;
        for (int i = 0; i < word.size(); i++) {
            /* code */
            if(head->m.count(word[i]) == 1)
            {
                head = head->m[word[i]];
                head->path++;
                if(i == word.size() - 1 )
                    head->ends++;
            }
            else
            {
                TrieNode *node = new TrieNode();
                node->path = 1;
                if(i == word.size() - 1 )
                {
                    node->ends++;
                }
                else
                    node->ends = 0;
                
                head->m[word[i]] = node;
                head = node;
            }
        }
        //cout <<"OK  " <<endl;
        
    }
    
    /** Returns if the word is in the data structure. A word could contain the dot 
character '.' to represent any one letter. */
    bool search(string word)
    {
        TrieNode  *head  = root;
        return proccess(word, 0, head);
        
    }
    bool  proccess(string word, int i , TrieNode *head)
    {
        if(i == word.size())//最后一个字符
        {
            if(head->ends>=1)
                return true;
            else
                return false;
        }
        
        if(word[i] == '.')
        {
            for(int mm = 0; mm < 26; mm++)
            {
                // cout << "mm" << mm<<endl;
                TrieNode *h = head;
                if(h->m.count('a'+mm) == 1)
                {
                    h = head->m['a'+mm];//下面怎么走呢
                    if(proccess( word, i+1 , h))
                        return true;
                }
            }
            return false;
        }
        else
        {
            if(head->m.count(word[i])== 1)
            {
                head = head->m[word[i]];
                return proccess(word, i+1, head);//head是当前节点 当到达s.size() -1 的时
//候,head正好指向最后一个
                //字符
            }
            else
                return false;
        }
        return true;
    }
};
/************************************************************************/
/* 
分金条问题
一块金条切成两半,是需要花费和长度数值一样的铜板的。比如 长度为20的 金条,不管切成长度多大的两半,
都要花费20个铜 板。一群人想整分整块金 条,怎么分最省铜板? 例如,给定数组{10,20,30},代表一共三个人,
整块金条长度为 10+20+30=60. 金条要分成10,20,30三个部分。 如果, 先把长 度60的金条分成10和50,
花费60 再把长度50的金条分成20和30, 花费50 一共花费110铜板。 但是如果, 先把长度60的金条分成30
和30,花费60 再把长度30 金条分成10和20,花费30 一共花费90铜板。 输入一个数组,返回分割的最小代价。
*/
/************************************************************************/
/************************************************************************/
/* 算法思想:
	要实现这个目标 :要利用哈夫曼编码 编码长度越短,代价越低



*/
/************************************************************************/
class  Less_Money
{
public:
    priority_queue<int,vector<int>,greater<int>> p;//小顶堆 默认是大顶堆
    
    int  getMin(vector<int> arr)
    {
        int sum = 0;
        int cur = 0;
        for (int i = 0; i < arr.size(); i++)
        {
            p.push(arr[i]);
        }
        while (p.size()>1)
        {
            int first = p.top();
            p.pop();
            int second = p.top();
            p.pop();
            cur = first + second;
            cout <<"first:  "<<first << "   second:    "<< second<<endl;
            sum += cur ;
            p.push(cur);
        }
        return sum;
        
    }
};
我们有如下工作:difficulty[i]是第i个工作的难度,profit[i]是第i个工作的利润。

现在我们有一些工人。 worker[i]是第i个工人的能力,这意味着这个工人最多完成难度为worker[i]的工作。

每个工人最多只能分配一份工作,但一份工作可以多次完成。

例如,如果3个人尝试完成1美元的相同工作,那么总利润将为3美元。 如果工人无法完成任何工作,他的利润为0美元。

我们可以获得的利润最大是多少?

样例
样例 1:

输入: difficulty = [2,4,6,8,10], profit = [10,20,30,40,50], worker = [4,5,6,7]
输出: 100 
解释: 工人们分别被分配工作难度 [4,4,6,6],他们各自取得的利润为  [20,20,30,30].
注意事项
1 <= difficulty.length = profit.length <= 10000
1 <= worker.length <= 10000
difficulty[i],profit[i],worker[i]在[1,10 ^ 5]范围内
class Work
{
public:
    int difficulty;
    int profit;
    Work(int d, int p)
    {
        this->difficulty = d;
        this->profit = p;
    }
 
};//
   bool operator<(Work w1, Work w2)
    {
        return w1.profit  < w2.profit;
    }

class Solution {
public:
    /**
     * @param difficulty:
     * @param profit:
     * @param worker:
     * @return: nothing
     */
    priority_queue<Work> p;
    
    int maxProfitAssignment(vector<int> &difficulty, vector<int> &profit, vector<int> &worker)
    {
        for (int i = 0; i < profit.size(); i++)
        {
            p.push(Work(difficulty[i],profit[i]));
        }
        sort(worker.begin(), worker.end(),greater<int>());
        int index = 0;
        int sum = 0;
        while (!p.empty() && index <worker.size()) {
            Work w = p.top();
            if(w.difficulty <= worker[index])
            {
                sum +=w.profit;//
                index++;
            }
            else
            {
               p.pop();
            }
            
        }
        return sum;
    }
};
数字是不断进入数组的,在每次添加一个新的数进入数组的同时返回当前新数组的中位数。

样例
样例1

输入: [1,2,3,4,5]
输出: [1,1,2,2,3]
样例说明:
[1] 和 [1,2] 的中位数是 1.
[1,2,3] 和 [1,2,3,4] 的中位数是 2.
[1,2,3,4,5] 的中位数是 3.
样例2

输入: [4,5,1,3,2,6,0]
输出: [4,4,4,3,3,3,3]
样例说明:
[4], [4,5] 和 [4,5,1] 的中位数是 4.
[4,5,1,3], [4,5,1,3,2], [4,5,1,3,2,6] 和 [4,5,1,3,2,6,0] 的中位数是 3.
挑战
时间复杂度为O(nlogn)

说明
中位数的定义:

这里的中位数不等同于数学定义里的中位数。
A[(n−1)/2]。
比如:数组A=[1,2,3]的中位数是2,数组A=[1,19]的中位数是1。
输入测试数据 (每行一个参数)
如何理解测试数据?
class Solution {
public:
    /**
     * @param nums: A list of integers
     * @return: the median of numbers
     */
    priority_queue<int,vector<int>, greater<int>> pMin;
    priority_queue<int,vector<int>, less<int>> pMax;
    //使用大顶堆来保存左边的数据,使用小顶堆保存右边的数据
    vector<int> medianII(vector<int> &nums) { //分金条问题
        // write your code here
        //特殊处理
        vector<int> res;
        for (int i = 0; i < nums.size(); i++)
        {
            /* code */
            if(pMax.empty())
                pMax.push(nums[i]);
            else
            {
                if(nums[i] > pMax.top())//关键步骤
                    pMin.push(nums[i]);
                else
                    pMax.push(nums[i]);
            }
            int l =  pMax.size();
            int r = pMin.size();
            if(l - r ==2)
            {
                int t = pMax.top();
                pMax.pop();
                pMin.push(t);
            }
            if(r - l ==2)
            {
                int t = pMin.top();
                pMin.pop();
                pMax.push(t);
            }
             l =  pMax.size();
             r = pMin.size();
            if(l-r == 0)
            {
                res.push_back(pMax.top());
            }
            else if(l-r == 1)
                res.push_back(pMax.top());
            else
                res.push_back(pMin.top());
        }
              return res;
    }
};
一些项目要占用一个会议室宣讲,会议室不能同时容纳两个项目 的宣讲。 给你每一个项目开始的时间和结束的时
间(给你一个数 组,里面 是一个个具体的项目),你来安排宣讲的日程,要求会 议室进行 的宣讲的场次最多。
返回这个最多的宣讲场次。
​​给定一个字符串类型的数组strs,找到一种拼接方式,使得把所 有字 符串拼起来之后形成的字符串具有最低的
字典序。
bool compare(string s1, string s2)
{
    return s1+s2 < s2+s1;
}



class Solution
{
public:
    string getMinStr(vector<string> arr)
    {
        
        sort(arr.begin(), arr.end(),compare);
        string res ="";
        for (int i = 0; i < arr.size(); i++)
        {
            res +=arr[i];
        }
        return res;
    }
};

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值