Showing posts with label Trie. Show all posts
Showing posts with label Trie. Show all posts

Monday, March 4, 2024

[Toph] B. Dictionary Tree

 

Problem Link    : B. Dictionary Tree
Category        : Trie, Tree, Dfs
Contest         : Intra AIUB Programming Contest 2024 (Senior)

#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
struct Trie {
    int counter;
    Trie *child[26];
    Trie *parent;
 
    Trie() {
        counter = 0;
        parent = nullptr;
        for (int i = 0; i < 26; i++) {
            child[i] = nullptr;
        }
    }
    ~Trie() {
        for (int i = 0; i < 26; i++) {
            if (child[i] and this != child[i]) {
                delete child[i];
            }
        }
    }
};
 
typedef Trie* pnode;
pnode root;
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = true; string FILE = "in.txt";
    if (FILEIO and fopen(FILE.c_str(), "r")) {
        freopen(FILE.c_str(), "r", stdin);
    }
 
    int n, k;
    cin >> n >> k;
    vector<vector<int>> graph(n + 1);
    for (int e = 1; e < n; e++) {
        int u, v;
        cin >> u >> v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    vector<char> letters(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> letters[i];
    }
    for (int i = 1; i <= n; i++) {
        sort(graph[i].begin(), graph[i].end(), [&](int x, int y)
            {
               return letters[x] < letters[y];
            });
    }
    root = new Trie();
    pnode curRoot = root;
    function<void(int, int)> Dfs = [&](int u, int p) {
        if (curRoot->child[ letters[u] - 'a' ] == nullptr) {
            curRoot->child[ letters[u] - 'a' ] = new Trie();
            curRoot->child[ letters[u] - 'a' ]->parent = curRoot;
            curRoot = curRoot->child[ letters[u] - 'a' ];
            curRoot->counter++;
        } else {
            curRoot->child[ letters[u] - 'a' ]->parent = curRoot;
            curRoot = curRoot->child[ letters[u] - 'a' ];
            curRoot->counter++;
        }
        for (int v : graph[u]) {
            if (v == p) {
                continue;
            }
            Dfs(v, u);
        }
        curRoot = curRoot->parent;
    };
    Dfs(1, 0);
    string path = "";
    function<void(pnode)> Solve = [&](pnode rt) {
        if (rt == nullptr) {
            return;
        }
        if (k <= 0) {
            cout << path << endl;
            exit(0);
        }
        for (int i = 0; i < 26; i++) {
            if (rt->child[i] == nullptr) {
                continue;
            }
            path += static_cast<char>(i + 'a');
            k -= rt->child[i]->counter;
            Solve(rt->child[i]);
            path.pop_back();
        }
    };
    Solve(root);
}

Saturday, January 27, 2024

[Hackerearth] Special Subarrays

 

Problem Link    : Special Subarrays
Category        : Trie
Contest         : DSA Coding Contest - January '24

#include "bits/stdc++.h"
using namespace std;
#define int     long long int
#define endl    '\n'
 
template <const int32_t MOD> struct modint {
    int32_t value;
    modint() = default;
    modint(int32_t value_) : value(value_) {}
    inline modint<MOD> operator + (modint<MOD> other) const { int32_t c = this->value + other.value; return modint<MOD>(c >= MOD ? c - MOD : c); }
    inline modint<MOD> operator - (modint<MOD> other) const { int32_t c = this->value - other.value; return modint<MOD>(c <    0 ? c + MOD : c); }
    inline modint<MOD> operator * (modint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return modint<MOD>(c < 0 ? c + MOD : c); }
    inline modint<MOD> & operator += (modint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline modint<MOD> & operator -= (modint<MOD> other) { this->value -= other.value; if (this->value < 0) this->value += MOD; return *this; }
    inline modint<MOD> & operator *= (modint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
    inline modint<MOD> operator - () const { return modint<MOD>(this->value ? MOD - this->value : 0); }
    modint<MOD> pow(uint64_t k) const { modint<MOD> x = *this, y = 1; for (; k; k >>= 1) { if (k & 1) y *= x; x *= x; } return y; }
    modint<MOD> inv() const { return pow(MOD - 2); }  // MOD must be a prime
    inline modint<MOD> operator /  (modint<MOD> other) const { return *this *  other.inv(); }
    inline modint<MOD> operator /= (modint<MOD> other)       { return *this *= other.inv(); }
    inline bool operator == (modint<MOD> other) const { return value == other.value; }
    inline bool operator != (modint<MOD> other) const { return value != other.value; }
    inline bool operator < (modint<MOD> other) const { return value < other.value; }
    inline bool operator > (modint<MOD> other) const { return value > other.value; }
};
template <int32_t MOD> modint<MOD> operator * (int64_t value, modint<MOD> n) { return modint<MOD>(value) * n; }
template <int32_t MOD> modint<MOD> operator * (int32_t value, modint<MOD> n) { return modint<MOD>(value % MOD) * n; }
template <int32_t MOD> istream & operator >> (istream & in, modint<MOD> &n) { return in >> n.value; }
template <int32_t MOD> ostream & operator << (ostream & out, modint<MOD> n) { return out << n.value; }
 
const int mod = 1e9 + 7;
using mint = modint<mod>;
 
struct Trie {
    int ones;
    Trie *child[2];
 
    Trie() {
        ones = 0;
        for (int i = 0; i < 2; i++) {
            child[i] = nullptr;
        }
    }
    ~Trie() {
        for (int i = 0; i < 2; i++) {
            if (child[i] and this != child[i]) {
                delete child[i];
            }
        }
    }
};
 
typedef Trie* pnode;
pnode root;
 
void add(string &s) {
    pnode curRoot = root;
    for (char ch : s) {
        int val = ch - '0';
        if (curRoot->child[val] == nullptr) {
            curRoot->child[val] = new Trie();
        }
        curRoot = curRoot->child[val];
    }
}
 
mint ans;
 
void dfs(pnode curRoot, int ones) {
    for (int i = 0; i < 2; i++) {
        if (curRoot->child[i]) {
            int nones = ones + (i == 0);
            ans += max(0ll, nones - 1);
            dfs(curRoot->child[i], nones);
        }
    }
}
 
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout.precision(12);
 
    bool FILEIO = 1;
    if (FILEIO and fopen("in.txt", "r")) {
        freopen("in.txt", "r", stdin);
    }
 
    int n;
    cin >> n;
    root = new Trie();
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        add(s);
    }
    ans = 0;
    dfs(root, 0);
    cout << ans << endl;
}

Tuesday, May 7, 2019

[Toph] Subset of Sequences

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : Subset of Sequences
Source            : Toph
Category          : Data Structure, String
Algorithm         : Trie using Map 
Verdict           : Accepted 

  1. #include <bits/stdc++.h>  
  2.   
  3. using namespace std;  
  4.   
  5. #define ll            long long int  
  6. #define vi            vector <int>  
  7.   
  8. static const int maxn = 2e6 + 5;  
  9. static const ll mod = 1e9 + 7;  
  10.   
  11. int n;  
  12. ll power[maxn], ans[maxn], length[maxn];  
  13.   
  14. struct Node  
  15. {  
  16.       ll ed;  
  17.       map <int, Node*> Next;  
  18.       Node()  
  19.       {  
  20.             ed = 0;  
  21.             Next.clear();  
  22.       }  
  23. };  
  24.   
  25. typedef Node*     pnode;  
  26. pnode root;  
  27.   
  28. void add(vi &vec)  
  29. {  
  30.       pnode cur = root;  
  31.       for (int val : vec)  
  32.       {  
  33.             if (cur->Next.find(val) == cur->Next.end()) cur->Next[val] = new Node;  
  34.             cur = cur->Next[val];  
  35.             cur->ed += 1;  
  36.       }  
  37. }  
  38.   
  39. void srch(vi &vec)  
  40. {  
  41.       pnode cur = root;  
  42.       int len = vec.size();  
  43.       for (int i = 0; i < len; i++)  
  44.       {  
  45.             int val = vec[i];  
  46.             if (cur->Next.find(val) == cur->Next.end()) return;  
  47.             cur = cur->Next[val];  
  48.             int cnt = cur->ed;  
  49.             ans[i+1] = (ans[i+1] + (power[cnt] - 1) % mod) % mod;  
  50.       }  
  51. }  
  52.   
  53. int main()  
  54. {  
  55.       ios_base::sync_with_stdio(false);  
  56.       cin.tie(nullptr);  
  57.       cout.tie(nullptr);  
  58.   
  59.       #ifndef ONLINE_JUDGE  
  60.             freopen("in.txt", "r", stdin);  
  61.       #endif // ONLINE_JUDGE  
  62.   
  63.   
  64.       power[0] = 1;  
  65.       for (int i = 1; i < maxn; i++) power[i] = (2LL * power[i-1]) % mod;  
  66.   
  67.       int n;  
  68.       cin >> n;  
  69.       root = new Node;  
  70.       for (int i = 1; i <= n; i++)  
  71.       {  
  72.             int m;  
  73.             cin >> m;  
  74.             length[0]++;  
  75.             vector <int> vec(m);  
  76.             for (int i = 0; i < m; i++)  
  77.             {  
  78.                   length[i+1]++;  
  79.                   cin >> vec[i];  
  80.             }  
  81.             srch(vec);  
  82.             add(vec);  
  83.       }  
  84.       ans[0] = (power[ length[0] ] - 1 - length[0] + mod) % mod;  
  85.       int q;  
  86.       cin >> q;  
  87.       for (int tcase = 1; tcase <= q; tcase++)  
  88.       {  
  89.             int k;  
  90.             cin >> k;  
  91.             cout << "Case " << tcase << ": " << (ans[k] + length[k]) % mod << '\n';  
  92.       }  
  93. }