Pagini recente » Cod sursa (job #1177442) | Cod sursa (job #110151) | Cod sursa (job #1107260) | Cod sursa (job #599804) | Cod sursa (job #3298002)
#include <fstream>
#include <string>
using namespace std;
struct nod{
nod *fi[26];
bool isL;
int wordCount;
nod()
{
isL = false;
wordCount = 0;
for(int i = 0 ; i < 26 ; i++)
fi[i] = nullptr;
}
};
void ins(nod *r, const string& key)
{
nod *curr = r;
for(char c : key)
{
if(curr->fi[c - 'a'] == nullptr)
{
nod *newNod = new nod();
curr->fi[c - 'a'] = newNod;
}
curr = curr->fi[c - 'a'];
}
curr->isL = true;
curr->wordCount++;
}
bool search(nod *r, const string& key)
{
nod *curr = r;
for(char c : key)
{
if(curr->fi[c - 'a'] == nullptr)
return false;
curr = curr->fi[c - 'a'];
}
return curr->isL;
}
bool isEmpty(nod *r)
{
for(int i = 0 ; i < 26 ; i++)
if(r->fi[i])
return false;
return true;
}
nod *del(nod *r, string key, int depth = 0)
{
if(!r)
return NULL;
if(depth == key.size())
{
if(r->isL)
{
r->wordCount--;
if(r->wordCount == 0)
r->isL = false;
}
if(isEmpty(r))
{
delete r;
r = NULL;
}
return r;
}
int ind = key[depth] - 'a';
r->fi[ind] = del(r->fi[ind], key, depth + 1);
if(isEmpty(r) && r->isL == false)
{
delete r;
r = NULL;
}
return r;
}
int count(nod *r, const string& key)
{
nod *curr = r;
for(auto c : key)
{
if(curr->fi[c - 'a'] == nullptr)
return 0;
curr = curr->fi[c - 'a'];
}
return curr->isL ? curr->wordCount : 0;
}
int longestCommonPrefixLength(nod *r, const string& w)
{
nod *curr = r;
int length = 0;
for(char c : w)
{
int index = c - 'a';
if(curr->fi[index] == nullptr)
break;
curr = curr->fi[index];
length++;
}
return length;
}
int main()
{
ifstream fin("trie.in");
ofstream fout("trie.out");
nod *r = new nod();
int op;
string word;
while(fin >> op >> word)
{
if(op == 0)
ins(r, word);
if(op == 1)
del(r, word);
if(op == 2)
fout << count(r, word) << '\n';
if(op == 3)
fout << longestCommonPrefixLength(r, word) << '\n';
}
return 0;
}