Pagini recente » Cod sursa (job #1693801) | Cod sursa (job #1425479) | Cod sursa (job #1018067) | Cod sursa (job #265221) | Cod sursa (job #815338)
Cod sursa(job #815338)
#include <fstream>
#include <map>
#include <string.h>
#include <assert.h>
class TrieNode
{
private:
typedef std::map<char, TrieNode*> Children;
private:
TrieNode *pParent;
Children children;
char ch;
int count;
public:
TrieNode() : pParent(0), ch('\0'), count(0)
{
}
TrieNode(TrieNode *_pParent, char _ch) : pParent(_pParent), ch(_ch), count(0)
{
}
void processWord(const char *_pszWord)
{
if (strlen(_pszWord))
{
Children::iterator itNode;
itNode = children.find(_pszWord[0]);
if (children.end() != itNode)
itNode->second->processWord(_pszWord + 1);
else
{
TrieNode *pNode = new TrieNode(this, _pszWord[0]);
children.insert(std::make_pair(_pszWord[0], pNode));
pNode->processWord(_pszWord + 1);
}
}
else
count ++;
}
void deleteWord(const char *_pszWord)
{
if (strlen(_pszWord))
{
Children::iterator itNode;
itNode = children.find(_pszWord[0]);
if (children.end() != itNode)
itNode->second->deleteWord(_pszWord + 1);
}
else
{
count --;
if (!count && !children.size() && pParent)
pParent->deleteChild(this->ch);
}
}
void deleteChild(char _ch)
{
Children::iterator itNode;
itNode = children.find(_ch);
if (children.end() != itNode)
{
children.erase(itNode);
if (!children.size() && !count)
{
if (pParent)
pParent->deleteChild(ch);
}
}
}
int getCount(const char *_pszWord)
{
if (strlen(_pszWord))
{
Children::iterator itNode;
itNode = children.find(_pszWord[0]);
if (children.end() != itNode)
return itNode->second->getCount(_pszWord + 1);
else
return 0;
}
else
return count;
}
int maxPrefixLength(const char *_pszWord, int _h)
{
if (strlen(_pszWord))
{
Children::iterator itNode;
itNode = children.find(_pszWord[0]);
if (children.end() != itNode)
return itNode->second->maxPrefixLength(_pszWord + 1, _h + 1);
else
return _h;
}
else
return _h;
}
bool operator < (const TrieNode &_other)
{
return ch < _other.ch;
}
};
class Tree
{
private:
TrieNode root;
public:
Tree()
{
}
void addWord(const char *_pszWord)
{
root.processWord(_pszWord);
}
void deleteWord(const char *_pszWord)
{
root.deleteWord(_pszWord);
}
int count(const char *_pszWord)
{
return root.getCount(_pszWord);
}
int maxPrefixLength(const char *_pszWord)
{
return root.maxPrefixLength(_pszWord, 0);
}
};
int main()
{
/*std::ifstream fin("trie.in");
std::ofstream fout("trie.out");
Tree tree;
while(!fin.eof())
{
char w[50];
int op;
fin>>op;
fin>>w;
switch(op)
{
case 0:
tree.addWord(w);
break;
case 1:
tree.deleteWord(w);
break;
case 2:
fout<<tree.count(w)<<'\n';
break;
case 3:
fout<<tree.maxPrefixLength(w)<<'\n';
break;
default:
break;
}
}
fin.close();
fout.close();*/
Tree tree;
char line[ 32 ];
freopen( "trie.in", "r", stdin );
freopen( "trie.out", "w", stdout );
fgets( line, 32, stdin );
while( !feof( stdin ) ) {
switch( line[0] ) {
case '0': tree.addWord(line+2); break;
case '1': tree.deleteWord(line + 2); break;
case '2': printf( "%d\n", tree.count(line + 2) ); break;
case '3': printf( "%d\n", tree.maxPrefixLength(line + 2) ); break;
}
fgets( line, 32, stdin );
}
return 0;
}