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;
}