20200412

5380. 数组中的字符串匹配

image.png

  1. //我的代码
  2. class Solution
  3. {
  4. public:
  5. vector<string> stringMatching(vector<string> &words)
  6. {
  7. vector<string> res;
  8. unordered_set<string> a;
  9. for (int i = 0; i < words.size() - 1; i++)
  10. {
  11. for (int j = i + 1; j < words.size(); j++)
  12. {
  13. if (IsContainsStr(words[i], words[j]))
  14. {
  15. a.insert(words[j]);
  16. }
  17. if (IsContainsStr(words[j], words[i]))
  18. {
  19. a.insert(words[i]);
  20. }
  21. }
  22. }
  23. for (auto it : a)
  24. {
  25. res.push_back(it);
  26. }
  27. return res;
  28. }
  29. bool IsContainsStr(string str, string contains_str)
  30. {
  31. string::size_type idx = str.find(contains_str);
  32. if (idx != string::npos)
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. }
  41. };
  1. //大佬的代码
  2. class Solution {
  3. public:
  4. vector<string> stringMatching(vector<string>& a) {
  5. vector<string> ans;
  6. for(int i=0;i<a.size();++i) {
  7. int ok = 0;
  8. for(int j=0;j<a.size();++j) {
  9. if( i != j) {
  10. if(a[j].find(a[i]) != string::npos) ok = 1;
  11. }
  12. }
  13. if(ok) ans.push_back(a[i]);
  14. }
  15. return ans;
  16. }
  17. };

总结:
  1. npos可以表示string的结束位子
  2. //改进
  3. //是否为子列
  4. bool IsContainsStr(string str, string contains_str)
  5. {
  6. return str.find(contains_str) != string::npos?true:false;
  7. }

5382. HTML 实体解析器

image.png

//我的代码
class Solution
{
public:
    string entityParser(string text)
    {
        unordered_map<string, string> a;
        StringReplace(text, "&amp;", "&");
        StringReplace(text, "&apos;", "'");
        StringReplace(text, "&quot;", "\"");
        StringReplace(text, "&gt;", ">");
        StringReplace(text, "&lt;", "<");
        StringReplace(text, "&frasl;", "/");
        return text;
    }
    //替换
    void StringReplace(string &strBase, string strSrc, string strDes)
    {
        string::size_type pos = 0;
        string::size_type srcLen = strSrc.size();
        string::size_type desLen = strDes.size();
        pos = strBase.find(strSrc, pos);
        while ((pos != string::npos))
        {
            strBase.replace(pos, srcLen, strDes);
            pos = strBase.find(strSrc, (pos + desLen));
        }
    }
};

5381. 查询带键的排列

image.png

class Solution {
public:
    vector<int> processQueries(vector<int>& queries, int m) {
        vector<int> res;
        vector<int> a;
        for(int j=0;j<m;j++)
        {
            a.push_back(j+1);
        }

        for(int i=0;i<queries.size();i++)
        {
            auto it=find(a.begin(),a.end(),queries[i]);
            auto index=distance(begin(a),it);
            res.push_back(index);
            gengxin(a,queries[i],index);
        }
        return res;
    }
        void gengxin(vector<int> &a,int nums,int i)
        {
            a.erase(a.begin()+i);
            a.insert(a.begin(),nums);
        }

};

总结:
//找到元素 返回下表
auto it=find(a.begin(),a.end(),queries[i]);
auto index=distance(begin(a),it);