Pagini recente » Cod sursa (job #1551464) | Cod sursa (job #1140984) | Cod sursa (job #68612) | Cod sursa (job #1912464) | Cod sursa (job #2054844)
#include <fstream>
#include <vector>
#include <string>
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
struct node {
node() {
full_frecv = 0;
part_frecv = 0;
next_trie.resize(26);
for (auto &x : next_trie) {
x = NULL;
}
}
int full_frecv;
int part_frecv;
vector < node* > next_trie;
};
class Trie {
public:
Trie() {
_base_trie = new node;
}
void Solve(string s, int type) {
if (type == 0) {
_op_01(s, 1);
}
else if (type == 1) {
_op_01(s, -1);
}
else if (type == 2) {
_op_2(s);
}
else {
_op_3(s);
}
}
private:
node* _base_trie;
node* _cur_trie;
void _op_01(string s, int type) {
_cur_trie = _base_trie;
for (auto x : s) {
int index = x - 'a';
if (not _cur_trie -> next_trie[index]) {
_cur_trie -> next_trie[index] = new node();
}
_cur_trie = _cur_trie -> next_trie[index];
_cur_trie -> part_frecv += type;
}
_cur_trie -> full_frecv += type;
}
void _op_2(string s) {
_cur_trie = _base_trie;
for (auto x : s) {
int index = x - 'a';
if (not _cur_trie -> next_trie[index]) {
cout << 0 << '\n';
return;
}
_cur_trie = _cur_trie -> next_trie[index];
}
cout << _cur_trie -> full_frecv << '\n';
}
void _op_3(string s) {
int cnt = 0;
_cur_trie = _base_trie;
for (auto x : s) {
if (not _cur_trie -> next_trie[x - 'a']) {
break;
}
_cur_trie = _cur_trie -> next_trie[x - 'a'];
if (not _cur_trie -> part_frecv) {
break;
}
cnt ++;
}
cout << cnt << '\n';
}
};
int main(int argc, char const *argv[]) {
int type;
string s;
Trie* T = new Trie();
while (cin >> type) {
cin >> s;
T -> Solve(s, type);
}
}