## Tags » Define

#### TRIE Problems

Problem : Polycarp’s phone book
Solution :

```#include<bits/stdc++.h>
using namespace std;

#define MOD          (int)1e9+7
#define MAX          (int)1e5+5
#define ll           long long
struct Trie
{
Trie *digits;
int cnt,idx;
Trie()
{
for(int i=0;i<=9;i++)
digits[i] = NULL;
cnt = 0;
idx = -1;
}
};

Trie *root;

void Insert(string s,int ii)
{
Trie *cur = root;
bool flag = 0;
for(int i=0;i<s.size();i++)
{
int d = s[i]-'0';
if(cur->digits[d] == NULL)
cur->digits[d] = new Trie();
cur = cur->digits[d];
if(cur->idx != ii)
flag = 1 , cur->idx = ii;
if(flag)
cur->cnt++;
}
}
bool Search(string s)
{
Trie *cur = root;
for(int i=0;i<s.size();i++)
{
int d = s[i]-'0';
cur = cur->digits[d];
if(cur->cnt == 1)
return true;
}
return false;
}
int main()
{

int n;
cin >> n;
string s , temp , mn;
root = new Trie();
for(int i=0;i<n;i++)
{
cin >> s[i];
for(int j=0;j<s[i].size();j++)
{
temp = "";
for(int k=j;k<s[i].size();k++)
{
temp += s[i][k];
Insert(temp,i);
}
}
}
for(int i=0;i<n;i++)
{
mn = "9999999999";
for(int j=0;j<s[i].size();j++)
{
temp = "";
for(int k=j;k<s[i].size();k++)
{
temp += s[i][k];
if(Search(temp) && temp.size() < mn.size())
mn = temp;
}
}
cout << mn << endl;
}
}
```

———————————————————————————————————————————————————————–
———————————————————————————————————————————————————————–

Latest Posts

#### Count trailing zeroes in factorial of a number

Counting how many trailing zeroes are of ( N ! ) :

```#include<bits/stdc++.h>
using namespace std;
#define ll           long long
int trailingZeroes(int n)
{
int cnt = 0 , f = 5;
while(f <= n)
{
cnt += n/f;
f *= 5;
}
return cnt;
}
int main()
{
int n;
while(cin >> n)
{
cout << trailingZeroes(n) << endl;
}
}
… 145 more words```
Latest Posts

### Nor by the information-containing codewords from our fathers… 37 more words

#### Hashing Problems

Problem : Watto and Mechanism
Solution with Single Hash :

```#include<bits/stdc++.h>
using namespace std;

#define ll           unsigned long long
#define MOD          (ll)1e9+7
#define MAX          (ll)1e6+5
#define Time()       cout<< "\nElapsed Time : " << (clock()*1.0) / CLOCKS_PER_SEC <<" sec"<< endl;

ll save;
ll prime = 313;
void pre()
{
save[0] = 1;
for(ll i=1; i<MAX; i++)
save[i] = (save*prime)%MOD;
}
ll getHash(string s)
{
ll hashValue = 0;
for(ll i=0; i<s.size(); i++)
hashValue += s[i]*save[i];
return hashValue;
}
ll a, idx;

bool test(ll hashValue,char ch,ll i)
{
hashValue += ch*save[i];
if(binary_search(a,a+idx,hashValue))
return true;
return false;
}
bool checkHash(string s)
{
ll hashValue = getHash(s) , x;

for(ll i=0; i<s.size(); i++)
{
x = hashValue - s[i]*save[i];

for(char ch = 'a' ; ch <= 'c' ; ch++)
{
if(s[i] != ch && test(x,ch,i))
return true;
}
}
return false;
}
int main()
{
ll n,m;
cin >> n >> m;
pre();
string s;
for(ll i=0; i<n; i++)
{
cin >> s;
a = getHash(s);
}

sort(a,a+idx);

while(m--)
{
cin >> s;
if(checkHash(s))
cout << "YES\n";
else
cout << "NO\n";
}
} 1,083 more words```
Latest Posts

#### Compassion and the Opposition

Thank you to all you loyal readers for subscribing and for sticking with me, despite my absence. I had major oral surgery recently and took some time off from writing. 2,001 more words

Christianity

# I want you to know WHO I AM is not really WHO YOU SEE.

I’m not a woman without needs, desperate to be fulfilled. 532 more words

Inspirational

#### Work and perks

The sound of endless clicks and typings can be heard almost everyday during office hours.

Everyone in the workplace are hard workers, trying to reach and impress their head of their department. 537 more words

Stories And Spills