前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode(Weekly Contest 184)题解

LeetCode(Weekly Contest 184)题解

作者头像
西凉风雷
发布2022-11-23 19:42:45
1800
发布2022-11-23 19:42:45
举报

0. 前言

1. 题解

1.1 5380. 数组中的字符串匹配(1408. String Matching in an Array)

代码语言:javascript
复制
class Solution {
public:
    vector<int> getNext(string pattern) {
        int len = (int)pattern.length();
        vector<int> res(len+1,0);
        int j = 0;
        for (int i = 1 ; i < len ; i++) {
            while (j && pattern[i] != pattern[j])   j = res[j];
            if (pattern[i] == pattern[j])   j++;
            res[i+1] = j;
        }
        return res;
    }

    int searchPosition(string text, string pattern, vector<int>& next) {
        int j = 0 , lt = (int)text.length() , lp = (int)pattern.length();
        for (int i = 0 ; i < lt ; i++) {
            while (j && text[i] != pattern[j])  j = next[j];
            if (text[i] == pattern[j])  j++;
            if (j == lp)    return i-j+1;
        }
        return -1;
    }
    bool isSubstr(string pattern, string text, unordered_map<string, vector<int>>& nextmp) {
        if (searchPosition(text, pattern, nextmp[pattern]) >= 0)    return true;
        else    return false;
    }
    vector<string> stringMatching(vector<string>& words) {
        sort(words.begin(), words.end(), [](const string& a, const string& b) {
            return a.length() < b.length();
        });
        unordered_map<string, vector<int>> nextmp;
        for (auto str : words) {
            nextmp[str] = getNext(str);
        }
        int n = words.size();
        vector<string> ans;
        unordered_map<string, bool> vis;
        for (int i = 0 ; i < n ; i++) {
            for (int j = i+1 ; j < n ; j++) {
                if (!vis[words[i]] && isSubstr(words[i], words[j], nextmp)) {
                    ans.push_back(words[i]);
                    vis[words[i]] = true;
                }
            }
        }
        return ans;
    }
};

1.2 5381. 查询带键的排列(1409. Queries on a Permutation With Key)

代码语言:javascript
复制
class Solution {
public:
    vector<int> processQueries(vector<int>& queries, int m) {
        vector<int> p = vector<int>(m, 0);
        for (int i = 0 ; i < m ; i++) {
            p[i] = i + 1;
        }
        vector<int> ans;
        for (auto q : queries) {
            for (int i = 0 ; i < m ; i++) {
                if (q == p[i]) {
                    ans.push_back(i);
                    for (int j = i ; j >= 1 ; j--) {
                        p[j] = p[j-1];
                    }
                    p[0] = q;
                }
            }
        }
        return ans;
    }
};

1.3 5382. HTML 实体解析器(1410. HTML Entity Parser)

代码语言:javascript
复制
class Solution {
public:
    string entityParser(string text) {
        int len = text.length();
        string ans = "";
        for (int i = 0 ; i < len ; ) {
            if (text[i] == '&') {
                if (text[i+1] == 'q' && text[i+2] == 'u' && text[i+3] == 'o' && text[i+4] == 't' && text[i+5] == ';') {
                    ans += '"';
                    i += 6;
                } else if (text[i+1] == 'a' && text[i+2] == 'p' && text[i+3] == 'o' && text[i+4] == 's' && text[i+5] == ';') {
                    ans += '\'';
                    i += 6;
                } else if (text[i+1] == 'a' && text[i+2] == 'm' && text[i+3] == 'p' && text[i+4] == ';') {
                    ans += '&';
                    i += 5;
                } else if (text[i+1] == 'g' && text[i+2] == 't' && text[i+3] == ';') {
                    ans += '>';
                    i += 4;
                } else if (text[i+1] == 'l' && text[i+2] == 't' && text[i+3] == ';') {
                    ans += '<';
                    i += 4;
                } else if (text[i+1] == 'f' && text[i+2] == 'r' && text[i+3] == 'a' && text[i+4] == 's' && text[i+5] == 'l' && text[i+6] == ';') {
                    ans += '/';
                    i += 7;
                } else {
                    ans += '&';
                    i++;
                }
            } else {
                ans += text[i];
                i++;
            }
        }
        return ans;
    }
};

1.4 5383. 给 N x 3 网格图涂色的方案数(1411. Number of Ways to Paint N × 3 Grid)

代码语言:javascript
复制
class Solution {
public:
    int numOfWays(int n) {
        long long mod = 1000000007;
        vector<long long> dp1 = vector<long long>(n, (long long)1);
        vector<long long> dp2 = vector<long long>(n, (long long)1);
        for (int i = 1 ; i < n ; i++) {
            dp1[i] = (dp1[i-1] * (long long)2 + dp2[i-1] * (long long)2) % mod;
            dp2[i] = (dp1[i-1] * (long long)2 + dp2[i-1] * (long long)3) % mod;
        }
        long long ans = (long long)6 * (dp1[n-1] + dp2[n-1]) % mod;
        return (int)ans;
    }
};

2. 小结

  • 本次考试相对较水,前三题不是模拟就是暴力,第四题找规律即可
  • 不要排斥暴力,有的时候也挺好用的,哈哈哈
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 0. 前言
  • 1. 题解
    • 1.1 5380. 数组中的字符串匹配(1408. String Matching in an Array)
      • 1.2 5381. 查询带键的排列(1409. Queries on a Permutation With Key)
        • 1.3 5382. HTML 实体解析器(1410. HTML Entity Parser)
          • 1.4 5383. 给 N x 3 网格图涂色的方案数(1411. Number of Ways to Paint N × 3 Grid)
          • 2. 小结
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档