題目鏈接:牛客--接頭密匙
該題是一個很顯然的前綴樹問題,只需要構建a中所有數組對應的前綴樹,之后求b所處前綴個數即可。關于前綴樹的構建,可以觀看左老師算法講解045的視頻,簡單來講就是用特殊字符將實際數據隔開,同時將實際數據離散化。示例題解如下:
class Solution {private:static constexpr int N = 1e6 + 1;vector<vector<int>> trie = vector<vector<int>>(N,vector<int>(12)); // 0 - 9 & '-' & '#'vector<int> passArr = vector<int>(N);vector<int> endArr = vector<int>(N);int cnt;int getPath(char c) {if (c == '-') {return 10;}if (c == '#') {return 11;}return c - '0';}void buildTrie() {cnt = 1;}void reBuildTrie() {for (int i = 0; i < cnt; ++i) {fill(&trie[i][0], &trie[i][0] + 12, 0);passArr[i] = 0;endArr[i] = 0;}}void insert(const string& word) {int cur = 1, path;passArr[cur]++;for (char c : word) {path = getPath(c);if (trie[cur][path] == 0) {trie[cur][path] = ++cnt;}cur = trie[cur][path];passArr[cur]++;}endArr[cur]++;}int prefixCount(const string& word) {int cur = 1, path;for (char c : word) {path = getPath(c);if (trie[cur][path] == 0) {return 0;}cur = trie[cur][path];}return passArr[cur];}public:vector<int> countConsistentKeys(vector<vector<int> >& b,vector<vector<int> >& a) {// 建立前綴樹buildTrie();for (int i = 0; i < a.size(); ++i) {// a[i] = vector<int>string s = "";for (int j = 1; j < a[i].size(); j++) {s = s + std::to_string(a[i][j] - a[i][j - 1]) + "#";}insert(s);}vector<int> ans;// 搜索前綴樹for (int i = 0; i < b.size(); ++i) {// b[i] = vector<int>string pre = "";for (int j = 1; j < b[i].size(); ++j) {pre = pre + std::to_string(b[i][j] - b[i][j - 1]) + "#";}ans.push_back(prefixCount(pre));}reBuildTrie();return ans;}
};