解题思路
本题需要高效处理多个字符串三元组的前缀查询问题,并在每次动态操作后实时统计满足条件的三元组数目。通过字典树(Trie)结构维护每个坐标的前缀信息,并利用动态维护的当前查询前缀在字典树中的节点计数来快速计算结果。
- 字典树构建:为每个坐标轴 构建字典树,记录每个前缀的出现次数。
- 动态维护查询前缀:每次操作(添加或删除字符)后,更新对应坐标轴的当前前缀,并在字典树中找到对应的节点。
- 实时统计:每次操作后,从三个字典树的当前节点获取计数,乘积即为满足条件的数目。
代码实现
#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <stack>
using namespace std;
// 字典树节点
struct TrieNode {
unordered_map<char, TrieNode*> children;
int count = 0; // 记录当前前缀的出现次数
};
// 字典树类
class Trie {
public:
TrieNode* root;
vector<TrieNode*> nodeStack; // 记录插入过程中的节点路径
Trie() {
root = new TrieNode();
}
// 插入字符串
void insert(const string& s) {
TrieNode* current = root;
nodeStack.clear();
nodeStack.push_back(current);
current->count++;
for (char c : s) {
if (current->children.find(c) == current->children.end()) {
current->children[c] = new TrieNode();
}
current = current->children[c];
nodeStack.push_back(current);
current->count++;
}
}
// 获取当前前缀的计数
vector<int> getCounts() {
vector<int> counts;
for (TrieNode* node : nodeStack) {
counts.push_back(node->count);
}
return counts;
}
};
// 哈希常数
const long long hash_pow = 911382629;
const long long mod = 1e18 + 3;
// 计算字符串的哈希值
vector<long long> computeHashes(const string& s) {
vector<long long> hashList = {0};
for (char c : s) {
long long newHash = (hashList.back() * hash_pow + c) % mod;
hashList.push_back(newHash);
}
return hashList;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<tuple<string, string, string>> triples(n);
vector<vector<long long>> hashX(n), hashY(n), hashZ(n);
// 读取三元组并计算哈希
for (int i = 0; i < n; ++i) {
string x, y, z;
cin >> x >> y >> z;
triples[i] = {x, y, z};
hashX[i] = computeHashes(x);
hashY[i] = computeHashes(y);
hashZ[i] = computeHashes(z);
}
int q;
cin >> q;
// 维护当前查询字符串的前缀
vector<string> s = {"", "", ""};
vector<long long> currentHash = {0, 0, 0};
vector<int> currentLen = {0, 0, 0};
// 处理操作
while (q--) {
char op;
int idx;
cin >> op >> idx;
idx--;
if (op == '+') {
char c;
cin >> c;
s[idx].push_back(c);
currentLen[idx]++;
currentHash[idx] = (currentHash[idx] * hash_pow + c) % mod;
} else {
if (!s[idx].empty()) {
char removedChar = s[idx].back();
s[idx].pop_back();
currentLen[idx]--;
// 重新计算当前哈希
currentHash[idx] = 0;
for (char cc : s[idx]) {
currentHash[idx] = (currentHash[idx] * hash_pow + cc) % mod;
}
}
}
// 统计符合条件的数目
int cnt = 0;
for (int i = 0; i < n; ++i) {
bool valid = true;
if (currentLen[0] > hashX[i].size() - 1) valid = false;
else valid &= (hashX[i][currentLen[0]] == currentHash[0]);
if (currentLen[1] > hashY[i].size() - 1) valid = false;
else valid &= (hashY[i][currentLen[1]] == currentHash[1]);
if (currentLen[2] > hashZ[i].size() - 1) valid = false;
else valid &= (hashZ[i][currentLen[2]] == currentHash[2]);
if (valid) cnt++;
}
cout << cnt << "\n";
}
return 0;
}代码解释
- 字典树结构:每个坐标轴的字典树维护字符串的前缀,节点记录该前缀出现的次数。
- 动态维护查询前缀:通过栈结构维护当前查询字符串的前缀,每次操作更新栈和哈希值。
- 预处理哈希:每个三元组的坐标字符串预处理其所有前缀的哈希值,以便快速匹配。
- 实时统计:遍历所有三元组,检查其前缀哈希是否匹配当前查询哈希,统计符合条件的数目。
该实现通过预处理和哈希比较,确保每次操作后能高效统计结果。