每日一题:字符串高效查找——前缀树

该代码实现了一个名为Solution的类,用于解决在给定的二维字符矩阵中查找指定单词集合的匹配问题。通过深度优先搜索(DFS)策略,遍历矩阵并寻找与给定单词集中的单词匹配的序列,同时处理了包含通配符?的情况。
摘要由CSDN通过智能技术生成

废话不多说,直接看代码:


/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2021. All rights reserved.
 * Description: 上机编程认证
 * Note: 缺省代码仅供参考,可自行决定使用、修改或删除
 */

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>

using namespace std;

class Solution {
public:
    // 待实现函数,在此函数中填入答题代码;
    int GetMatchedWordCount(const vector<vector<char>> &charMatrix, const vector<string> &words)
    {
        this->words = words;
        m = charMatrix.size(), n = charMatrix[0].size();
        mat = new char *[m];
        for (int i = 0; i < m; i++) {
            mat[i] = new char[n];
            for (int j = 0; j < n; j++) {
                mat[i][j] = charMatrix[i][j];
            }
        }
        visi = new bool *[m];
        for (int i = 0; i < m; i++) {
            visi[i] = new bool[n];
        }
        getMaxLen();

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                resetVisi();
                dfs(i, j, "");
                if (found.size() == words.size()) {
                    return found.size();
                }
            }
        }

        return found.size();
    }

    void dfs(int i, int j, string word)
    {
        if (i < 0 || j < 0 || i >= m || j >= n || visi[i][j]) {
            return;
        }
        if (found.size() >= words.size()) {
            return;
        }
        if (word.size() > maxLen + 2) {
            return;
        }
        findWord(word);
        string cur = word + mat[i][j];
        visi[i][j] = true;
        dfs(i + 1, j, cur);
        dfs(i - 1, j, cur);
        dfs(i, j + 1, cur);
        dfs(i, j - 1, cur);
        visi[i][j] = false;
    }

    void resetVisi()
    {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                visi[i][j] = false;
            }
        }
    }

    bool findWord(const string &word)
    {
        for (const auto &item : words) {
            if (item.size() != word.size()) {
                continue;
            }
            int sz = item.size();
            int i = 0;
            for (i = 0; i < sz; i++) {
                if (word[i] != '?' && word[i] != item[i]) {
                    break;
                }
            }
            if (i == sz) {
                found.insert(item);
            }
            if (word.find('?') == string::npos) {
                return true;
            }
        }
        return false;
    }

    void getMaxLen()
    {
        maxLen = 0;
        for (const string &word : words) {
            if (word.size() > maxLen) {
                maxLen = word.size();
            }
        }
    }

private:
    int m;
    int n;
    int maxLen = 0;
    char **mat;
    bool **visi;
    vector<string> words;
    int ans = 0;
    unordered_set<string> found;
};

inline int ReadInt()
{
    int number;
    cin >> number;
    return number;
}

template <typename T>
inline vector<T> ReadVector(int size)
{
    vector<T> objects(size);
    for (int i = 0; i < size; ++i) {
        cin >> objects[i];
    }
    return objects;
}

int main()
{
    int row = ReadInt();
    int col = ReadInt();
    vector<vector<char>> charMatrix;

    for (int i = 0; i < row; i++) {
        vector<char> oneRow = ReadVector<char>(col);
        charMatrix.push_back(oneRow);
    }
    int wordsNum = ReadInt();
    vector<string> words = ReadVector<string>(wordsNum);

    Solution solution;
    int result = solution.GetMatchedWordCount(charMatrix, words);
    cout << result << endl;
    return 0;
}

KMP算法是一种字符串匹配算法,用于在一个文本串S内查找一个模式串P的出现位置。它的时间复杂度为O(n+m),其中n为文本串的长度,m为模式串的长度。 KMP算法的核心思想是利用已知信息来避免不必要的字符比较。具体来说,它维护一个next数组,其中next[i]表示当第i个字符匹配失败时,下一次匹配应该从模式串的第next[i]个字符开始。 我们可以通过一个简单的例子来理解KMP算法的思想。假设文本串为S="ababababca",模式串为P="abababca",我们想要在S中查找P的出现位置。 首先,我们可以将P的每个前缀和后缀进行比较,得到next数组: | i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | | --- | - | - | - | - | - | - | - | - | | P | a | b | a | b | a | b | c | a | | next| 0 | 0 | 1 | 2 | 3 | 4 | 0 | 1 | 接下来,我们从S的第一个字符开始匹配P。当S的第七个字符和P的第七个字符匹配失败时,我们可以利用next[6]=4,将P向右移动4个字符,使得P的第五个字符与S的第七个字符对齐。此时,我们可以发现P的前五个字符和S的前五个字符已经匹配成功了。因此,我们可以继续从S的第六个字符开始匹配P。 当S的第十个字符和P的第八个字符匹配失败时,我们可以利用next[7]=1,将P向右移动一个字符,使得P的第一个字符和S的第十个字符对齐。此时,我们可以发现P的前一个字符和S的第十个字符已经匹配成功了。因此,我们可以继续从S的第十一个字符开始匹配P。 最终,我们可以发现P出现在S的第二个位置。 下面是KMP算法的C++代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值