1

Why is this code giving allocation error?Question is -Given a string,s , let U be the set of weights for all possible uniform contiguous substrings of string . I have to answer n queries, where each query consists of a single integer . For each query, print Yes on a new line if found; otherwise, print No . The weight of a string is the sum of the weights of all the string's characters. For example: for apple= 1+ 16+16+12 + 5 A uniform string consists of a single character repeated zero or more times. For example, ccc and a are uniform strings, but bcb and cd are not.

Example : if string s = abccddde then possible uniform string U are a =1; b=2;c=3;cc=3+3=6; d=4;dd=4+4=8; ddd = 4+4+4=12; e=5; and if I input a query vector { 2,6,7,9,5} output should be yes if found else no. OUTPUT= {YES, YES,NO,NO,YES }


            #include <bits/stdc++.h>
            using namespace std;
            void search(vector<int> v,int o,int item)
             {
                int low=0;int flag=0;
                  int high=o;
                   while(low<high)
                   { int mid= (low+high)/2;
                       if(v[mid]==item)
                      flag=1;
                    else if( v[mid]<item)
                         {
                            low=mid+1;
    
                               }
                             else 
                            high=mid-1;
                           }
                       if(flag==0)
                        cout<<"Yes"<<endl;
                        else 
                       cout<<"No"<<endl;
                             }
 
             // Complete the weightedUniformStrings function below.
                  void weightedUniformStrings(string s, vector<int> queries) 
                {
                 int n=s.length();int o;
    
                  int arsize= queries.size();

                  vector<int> v;
    
                    int l;int flag=0;
                   v[0]=s[0]-'a'+1;
                 for(int i=1;i<n;i++)
                {
        
                      if(s[i]==s[i-1])
                    {
                          v[i]=(s[i]-'a'+1)+v[i-1];
                       }
                     else 
                    v[i]= s[i]-'a'+1;

                      }
               o= v.size();
               for(int k=0;k<arsize;k++)
              {
                      int it= queries[k];
                      search(v,o,it);

               }
                  }


              int main()
              {
   
                   string s;
                   getline(cin, s);

                   int queries_count;
                   cin >> queries_count;
                   vector<int> queries(queries_count);

                  for (int i = 0; i < queries_count; i++) {
     
                      cin >> queries[I];
      
                      }

   
                  weightedUniformStrings(s, queries);


                    return 0;
                  }
Anami
  • 11
  • 3
  • 1
    You should clarify your question a little, ideally with a more minimal example. However I can point out that you have some undefined behaviour which may or may not be related to the problem. You create a vector, v, with no elements, then you index it with 0 and much more later on. – OMGtechy Jul 11 '20 at 14:13
  • 3
    (1) Can you clean up the indentation? (2) Can you share the input file which reproduces the problem? AddressSanitizer does have a lot of memory overhead (over 200%) and if your code uses a lot of memory normally, you may simply not have enough to also use AddressSanitizer. – Nate Eldredge Jul 11 '20 at 14:19
  • https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Werner Henze Jul 11 '20 at 14:21
  • Updated the code with example – Anami Jul 11 '20 at 15:34

0 Answers0