class Solution { public: bool pal(string s){ int l=0,r=s.size()-1; while(l<r){…
- User
class Solution { public: bool pal(string s){ int l=0,r=s.size()-1; while(l<r){ if(s[l]!=s[r]) return false; l++; r--; } return true; } string dfs(int it, unordered_map<int,vector>& child,string s,unordered_map<int,string>& mp){ if(mp.find(it)!=mp.end()) return mp[it]; string mah = ""; for(auto itr:child[it]) mah+=dfs(itr,child,s,mp); mah+=s[it]; mp[it]=mah; return mah; } vector findAnswer(vector& parent, string s) { unordered_map<int,string> mp; int n=s.size(); unordered_map<int,vector> child; for(int i=0; i<parent.size(); i++){ if(parent[i]==-1) continue; child[parent[i]].push_back(i); } dfs(0,child,s,mp); vector ans; for(int i=0; i<n; i++){ ans.push_back(pal(mp[i])); } return ans; } }; fix memory limit exceeded error
- Assistant
- User
class Solution { public: bool isPalRange(string s,int start,int end){ while (start < end) { if (s[start] != s[end]) return false; start++; end--; } return true; } bool dfs(int it, unordered_map<int,vector>& child,string s,vector& result){ bool isPal = true; for(auto itr:child[it]) isPal=dfs(itr,child,s,result) && isPal; result[it] = isPalRange(s,0,it) && isPal; return result[it]; } vector findAnswer(vector& parent, string s) { int n=s.size(); unordered_map<int,vector> child; for(int i=0; i<parent.size(); i++){ if(parent[i]==-1) continue; child[parent[i]].push_back(i); } vector ans; dfs(0,child,s,ans); return ans; } }; Line 96: Char 2: runtime error: store to null pointer of type '_Bit_type' (aka 'unsigned long') (stl_bvector.h) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/stl_bvector.h:105:2
- Assistant
The runtime error you're encountering (
store to null pointer of type '_Bit_type' (aka 'unsigned long')) is due to the fact that the is not initialized with any size, and then you're trying to index into it using .