Showing posts with label Datastructure. Show all posts
Showing posts with label Datastructure. Show all posts
Hashing
Resources:
Threads @ IIIT Hyderabad
Palindrome Substring Queries
Palindrome Substring Queries Implementation
CF blog
CF blog
E-maxx
uniqeSubstring Implementation
group identical implementation
Problems:
Substring Search
1517. Freedom of Choice
ADAPHOTO - Ada and Terramorphing
Minimal Shift
Cyclic Shifts
Sub-palindromes
Manacher's Algorithm
Strings - 3
Suffixes
Jitu and Strings
A2 online
H. Queries for Number of Palindromes
Implementation:
Lowest Common Ancestor
Resource:
লোয়েস্ট কমন অ্যানসেস্টর
Lowest Common Ancestor, Binary Lifting and HLD
copsiitbhu.co.in
Lowest Common Ancestor - O(N−−√) and O(logN) with O(N) preprocessing
Lowest Common Ancestor - Binary Lifting
Lowest Common Ancestor - Farach-Colton and Bender Algorithm
Solve RMQ (Range Minimum Query) by finding LCA (Lowest Common Ancestor)
Lowest Common Ancestor - Tarjan's off-line algorithm
topcoder
Lowest Common Ancestor
CF
Lowest Common Ancestor in a Binary Search Tree.
[Tutorial] Searching Binary Indexed Tree in O(log(N)) using Binary Lifting
CF blog
Youtube Video:
Gaurav Sen
Rachit jain
TusharRoy
Algorithms Live
ACM Advanced Training 2018 - Lecture 2-2 - LCA and Sparse Table
Problem:
Problems
LCA problems
Codechef
E-maxx blog problem list
LCA SOLUTION
Code:
Treap (Cartesian tree)
Resources:
E-maxx
Algorithms Thread Episode 9: Treaps
carpanese blog
quora-Part-1 Part 2
Cf blog 0
CF Blog 1
CF blog 2
Cf blog 3
Cf blog 4
ACM Cairo Science
Youtube:
2.5+ hours lecture
Algorithm Live
Implementation:
Sqrt Decomposition
Resoures:
E-Maxx
Shafayet blog
Rezwan's CP Blog
Anudip blog
Tanvir's Blog
MO'S Algo
One problem-solution discussion 2D SQRT
MO’s Algorithm (Query square root decomposition)
Mo's algorithm (HackerEarth)
GeeksForGeeks
CF blog: 1 2
Cf blog3
CF blog4
[Tutorial] Two ways to apply Mo's Algorithm on Trees
Mo's Algorithm on Trees [Tutorial]
Practice Problem
Practice Contest
Youtube:
Rachit Jain
Gaurav sen
SolverToBe
Santo vai video
Implementation:
Implementation:
Sparse Table
Note: Sparse Table is a data structure, that allows answering range queries. It can answer most range queries in O(logn) , but its true power is answering range minimum queries (or equivalent range maximum queries). For those queries it can compute the answer in O(1) time.
The only drawback of this data structure is, that it can only be used on immutable arrays. This means, that the array cannot be changed between two queries. If any element in the array changes, the complete data structure has to be recomputed.
Rsources:
Tanvir's Blog
E-maxx
GeeksForGeeks
HackerEarth
Tushar Roy Video
Topcoder
Codechef
adilet.org_blog_sparse-table
Implementation: according to (This ) problem
Suffix Array
Resources:
CP_Algorithms
Suffix Array
stanford.edu
CommonLounge
Youtube:
Suffix array playlist
Complexity: O(Nlog^2(n))
Segment Tree
Resource Link:
টিউটোরিয়াল গুলো ক্রম অনুযায়ী সাজানো নাই।
ডাটা স্ট্রাকচার: সেগমেন্ট ট্রি-১
ডাটা স্ট্রাকচার: সেগমেন্ট ট্রি-২ (লেজি প্রপাগেশন)
Kaidul blog
blog
Segment tree/ BIT part - 1
লোয়েস্ট কমন অ্যানসেস্টর
Persistent Segment Tree (Part - 01)
Persistent Segment Tree (Part - 02)
A simple approach to segment trees
A simple approach to segment trees, part 2
TopCoder Tutorial
Indian Computing Olympiad
visualgo
E-Maxx : Segment Tree
Algorithm Gym :: Everything About Segment Trees
Efficient and easy segment trees
Segment trees (for beginners)
csacademy
Segment Trees and lazy propagation
Segment Trees
Persistent segment trees – Explained with spoj problems
Persistence Made Simple - Tutorial
CF blog problem list
Problem Link
CF blog problem list
Lazy Segment tree
Binary Search Tree (BST)
Binary Search Tree
Binary Search Tree (BST) is a node-based binary tree data structure having the following properties:
- The left subtree of a
node contains only nodes with keys less than the node’s key.
- The right subtree of
a node contaisns only nodes with greater than the node’s Key.
- The left and
right subtree each must also be a binary search tree.
BST has some
basic operation like Search, Insert, Delete, Traversal (In-order, pre-order,
post-order).If we want to delete a node from BST, Firstly we search this node
and delete this node.
Now we make a
BST using some data and following the BST properties. The data is given below:
47,40,54,38,30,39,49,60,57,80;
SUBXOR - SubXor
SUBXOR - SubXor
Subarray Xor
Topic: Trie Tree
Explanation
Solution 01:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <bits/stdc++.h> using namespace std; #define INF 1<<30 #define MAX 200005 #define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); typedef long long ll; struct trie { int L,R; int lft, rgt; trie(){ L = R = 0; lft = rgt = -1; } }; int Index = 0; void Insert(int value, int idx, int depth, trie *tree) { for(int i = depth-1; i >= 0; i--){ int bit = (value >> i ) & 1; if(bit){ tree[idx].R++; if(tree[idx].rgt == -1){ tree[idx].rgt = ++Index; idx = Index; } else idx = tree[idx].rgt; } else{ tree[idx].L++; if(tree[idx].lft == -1){ tree[idx].lft = ++Index; idx = Index; } else idx = tree[idx].lft; } } } int Query(int value, int cmp, int idx, int depth, trie* tree) { int ans = 0; for(int i = depth-1; i >= 0; i--){ int vBit = (value >>i) & 1; int cBit = (cmp >> i) & 1; if(cBit){ if(vBit){ ans += tree[idx].R; idx = tree[idx].lft; } else{ ans += tree[idx].L; idx = tree[idx].rgt; } } else{ if(vBit) idx = tree[idx].rgt; else idx = tree[idx].lft; } if(idx == -1)break; } return ans; } int main() { FASTIO ///* //double start_time = clock(); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //*/ int T; cin >> T; while(T--){ trie tree[MAX]; Index = 0; int n,k; cin >> n >> k; int XOR = 0; ll ans = 0; Insert(0,0,20,tree); for(int i =0; i < n; i++){ int x; cin >> x; XOR = XOR ^ x; ans += Query(XOR,k,0,20,tree); Insert(XOR,0,20,tree); } cout << ans << endl; } //double end_time = clock(); //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000); return 0; } |
Solution 02
Solution 03
Video Conference
Video Conference
Topic: trie
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | vector<string> solve (vector<string> names) { unordered_set<string> s; unordered_map<string, int> mp; vector<string>ans; for(const auto& name: names){ auto it = mp.find(name); if(it != mp.end()){ it->second++; ans.push_back(name + ' ' + to_string (it->second)); } else{ mp[name] = 1; string t; bool inserted = false; for(auto c : name){ t += c; auto p = s.insert(t); if(!inserted && p.second){ inserted = true; ans.push_back(t); } } if(!inserted) ans.push_back(t); } } return ans; } |
Another Solution
4682 - XOR Sum
4682 - XOR Sum
Topic : Trie Tree
N.B: Output not print in a single line ):
Solution 01:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <bits/stdc++.h> using namespace std; #define INF 1<<30 #define MAX 10005 #define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); typedef long long ll; #define INT_SIZE 32 struct TrieNode { int value; TrieNode *ar[2]; }; TrieNode *newNode() { TrieNode *temp = new TrieNode; temp->value = 0; temp->ar[0] = temp->ar[1] = NULL; return temp; } void insert(TrieNode *root, int pre_xor) { TrieNode *temp = root; for(int i = INT_SIZE - 1; i >= 0; i--){ bool val = pre_xor & (1 << i); if(temp->ar[val] == NULL) temp->ar[val] = newNode(); temp = temp->ar[val]; } temp->value = pre_xor; } int query(TrieNode *root, int pre_xor) { TrieNode *temp = root; for(int i = INT_SIZE - 1; i >= 0; i--){ bool val = pre_xor & (1 << i); if(temp->ar[1-val] != NULL) temp = temp->ar[1-val]; else if(temp->ar[val] != NULL) temp = temp->ar[val]; } return pre_xor ^ (temp->value); } int maxSubarrayXOR(int ar[], int n) { TrieNode *root = newNode(); insert(root, 0); int res = INT_MIN, pre_xor = 0; for(int i = 0; i < n; i++){ pre_xor = pre_xor ^ ar[i]; insert(root, pre_xor); res = max(res, query(root, pre_xor)); } return res; } int main() { //FASTIO ///* //double start_time = clock(); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); freopen("error.txt", "w", stderr); #endif //*/ int T; cin >> T; while(T--){ int n; cin >> n; int a[n+2]; for(int i = 0; i < n; i++){ cin >> a[i]; } cout << maxSubarrayXOR(a,n) << endl;; // if(T != 0) cout << " "; } //double end_time = clock(); //printf( "Time = %lf ms\n", ( (end_time - start_time) / CLOCKS_PER_SEC)*1000); return 0; } |