Pagini recente » Cod sursa (job #2600519) | Cod sursa (job #2033928) | Cod sursa (job #824563) | Cod sursa (job #2848392) | Cod sursa (job #2311979)
#include <unordered_map>
#include <fstream>
#include <cstdlib>
#include <chrono>
#include <random>
using namespace std;
ifstream cin("trie.in");
ofstream cout("trie.out");
unordered_map <string, int> f;
unordered_map <string, int> way;
inline void add(string s, int x)
{
string b;
for (auto &ch : s)
{
b += ch;
way[b] += x;
}
f[b] += x;
}
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
int main()
{
int a[4];
a[0] = min(rand(), 100000000); a[1] = min(a[1], 1000);
a[2] = min(rand(), 100000000); a[3] = min(a[1], 1000);
f.rehash(max(1000000, a[0] % a[1]));
way.rehash(max(1000000, a[2] % a[3]));
int type;
string s;
while (cin >> type >> s)
{
if (type == 0)
{
add(s, 1);
}
if (type == 1)
{
add(s, -1);
}
if (type == 2)
{
cout << f[s] << "\n";
}
if (type == 3)
{
int res = 0;
string b;
for (auto &ch : s)
{
b += ch;
if (way[b])
{
res = (int) b.size();
}
}
cout << res << "\n";
}
}
}