In the following code we have to first calculate the weights of uniform substrings present in our strings . Uniform sub strings are those which contain just one character like "a" or "aaa". The weight of the character is defined as a-1 b-2......z-26.
After calculating the weights of all the valid uniform substrings we will be given with various queries and we have to check whether the given no. is the array or not.
Here is the link of the code and corresponding output to it:
https://www.ideone.com/pIBPtQ
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int i=0,j=0,k=0;
int arr[10000];
int c=0;
while(s[i]!='\0')
{
int x=(int)s[i];
x=x-96;
arr[c++]=x;
j=i+1;
int sum=x;
while(s[j]==s[i])
{
sum+=x;
arr[c++]=sum;
j++;
}
i=j;
}
int q;
cin>>q;
for(i=0;i<q;i++)
{
int val;
cin>>val;
bool exists=find(begin(arr),end(arr),val)!=end(arr);
if(exists==true)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
cout<<"the elements of the array are:"<<endl;
for(i=0;i<c;i++)
cout<<arr[i]<<" ";
return 0;
}
You forgot to initialize arr.
Change
int arr[1000];
to
int arr[1000] = {0};
https://www.ideone.com/wIj4vp
Also x=x-96; should be better written as x -= 'a';.
Related
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;
}
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
Input
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements
Output
For each testcase, in a new line, output the rotated array
Example
Input
1 2 3 4 5
Output
3 4 5 1 2
#include <iostream>
#include <stack>
using namespace std;
void rotate(int *a,int s,int r) {
stack<int> st;
for(int i=0;i<r;i++) {
st.push(a[i]);
}
for(int j=r;j<s;j++) {
a[j-r] = a[j];
}
for(int k=s-1;k>r+1;k--) {
a[k] = st.top();
st.pop();
}
for(int l=0;l<s;l++) {
cout<<a[l]<<" ";
}
}
int main() {
//code
int T;
cin>>T;
while(T--) {
int N,r;
cin>>N>>r;
int A[N];
for(int i=0;i<N;i++) {
cin>>A[i];
}
rotate(A,N,r);
cout<<endl;
}
return 0;
}
I followed your logic, it seems like there is problem in your backfill part.
for(int k=s-1;k>=s-r;k--) { // change k>r+1 to k>=s-r
a[k] = st.top();
st.pop();
}
sorry my bad, int third for loop in rotate function there should be k>s-r-1
So I'm totally new to pointers, I apologize for this, I'm supposed to pass an array of pointers and get the mode of that array. After the array passes as a set of pointers, I can't manipulate the array to find the mode, everything I try results in a syntax error.
EDIT: I changed list to an array of pointers and I get a runtime error.
int main()
{
int size=0;
int *list[size];
cout<<"Please enter the size of your array: ";
cin>>size;
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>*list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
int getMode (int* list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int *number=list[0];
int count1=0;
int count2=0;
int mode=0;
for(int j=1;j<arraySize;j++)
{
for(int i=1;i<arraySize;i++)
{
if(list[i]==number)
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= *list[j];
count2=count1;
}
count1=0;
}
return mode;
}
When you pass an array to a function, it automatically decays to a pointer, so you don't need to use &list. And in the function, you shouldn't declare it int *list[], it should just be int list[] or int *list.
Also, in the getMode() function, you need to count the matches of list[j]. You're just counting the repetitions of number, which is list[0].
#include <iostream>
#include <iomanip>
using namespace std;
int getMode (int list[], int arraySize)
{
cout<<"The array you entered is listed below\n "<<list[0];
for(int i=0;i<arraySize;i++)
{cout<<setw(3)<<list[i];}
int count1=0;
int count2=0;
int mode=0;
for(int j=0;j<arraySize;j++)
{
for(int i=0;i<arraySize;i++)
{
if(list[i]==list[j])
{
count1++; //counts the number of instances that the number occurs
}
}
if(count1>count2)
{
mode= list[j];
count2=count1;
}
count1=0;
}
return mode;
}
int main()
{
int size;
int *list;
cout<<"Please enter the size of your array: ";
cin>>size;
list=new int[size];
cout<<"\nPlease enter the numbers in your list seperated by spaces: ";
for(int i=0;i<size;i++)
{
cin>>list[i];
}
cout<<endl;
int mode=getMode(list,size);
cout<<"\n"<<mode<<endl;
return 0;
}
DEMO
Question: Given a number k, find the sum of k positive big integers.
This is my code,it works, but our Online Judge is rejecting it, says segfault. Why does it show segfault? I could do it with two strings but why isn't this working?
#include <iostream>
#include <string.h>
using namespace std;
void add(int l,int k);
void append(char a[], int temp);
int o;
int tf=0;
int carry=0;
char b[1000000];
char a[10000][10000];
char c[1000000];
int main()
{
int k,x=0,l=0,m=0;
cin>>k;
while(x<k)
{
cin>>a[x];
if(strlen(a[x])>l)
{
l=strlen(a[x]);
}
x++;
}
x=0;
while(x<k)
{
if(strlen(a[x])<l)
{
int temp=0;
append(a[x],l-strlen(a[x]));
}
x++;
}
add(l,k);
if(carry!=0)
{
cout<<carry;
}
while(o>=0)
{
cout<<(int)b[o];
o--;
}
}
void add(int l,int k)
{
int lb=l-1;
int r=k-1;
int sum=0;
int x=0;
int neg=0;
while(lb>=0)
{
r=k-1;
sum=0;
while(r>=0)
{
sum=sum+a[r][lb]-48;
r--;
}
sum=sum+carry;
lb--;
if(sum>=10)
{
b[x]=sum%10;
carry=sum/10;
}
else
{
b[x]=sum;
carry=0;
}
sum=0;
o=x;
x++;
}
}
void append(char a[], int temp)
{
int l=0,m;
int tempb=temp;
m=strlen(a)-1;
while(temp>0)
{
c[l]='0';
l++;
temp--;
}
int z=0;
while(z<=m)
{
c[l]=a[z];
z++;
l++;
}
z=0;
while(z<=m+tempb)
{
a[z]=c[z];
z++;
}
}
Input Format:
First line contains k , which specifies the number of big numbers. Each of the next k lines contains a
big positive integer.
Output Format:
For each test case print the new big integer in a new line
Sample Input:
3
1331331
1313
453535322
Sample Output:
454867966
Constraints:
1<=k<=10
1<=number of digits in big numbers<=10000
Based on the problem statement:
Each input entry my have at most nmax = 10000 digits
Since each entry is stored as a C-style, zero-terminated string, each character array must have a length of (nmax + 1) = 10001 characters, to accomodate for the C-string terminator '\0'.
As you stored the entries into character arrays without leaving room for the zero terminator, assuming each entry was 10000-characters long:
Each entry with k>=1 overwrote the terminator or entry k-1, thus coalescing the entries together;
You thus ended with one giant string, with l = strlen(a[0]) = 100000;
From then on, all further processings were performed with these incorrect (coalesced) inputs and length, leading to buffer overrun at some point later in the execution.
I have to implement c++ code to find out the number of occurrences of a small string in a large string with overlapping allowed. For eg- if large string is acacab and small string is aca, then answer should be 2.
I'm not getting correct answer by making this code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10],tstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0;i<strlen(lstr);i++)
{
if(lstr[i]==sstr[0])
{
j=i;
for(k=0;k<strlen(sstr);k++,j++)
{
tstr[k]=lstr[j];
}
}
if(strcmp(tstr,sstr)==0)
c++;
}
cout<<c;
return 0;
}
It's much easier if you use C++ strings instead of C ones:
int c=0;
string lstr, sstr;
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for (size_t pos = 0;
(pos = lstr.find(sstr, pos)) != string::npos;
pos++)
{
c++;
}
cout<<c;
This should work, add a '\0' to terminate c-style string, and move if(strcmp(tstr,sstr)==0) in the if(lstr[i]==sstr[0]) statement(otherwise you will continuing increment c when lstr[i] != sstr[0]):
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10],tstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0;i<strlen(lstr);i++)
{
if(lstr[i]==sstr[0])
{
j=i;
for(k=0;k<strlen(sstr) && j<strlen(lstr);k++,j++)
{
tstr[k]=lstr[j];
}
tstr[k] = 0;
// ^^^^^^^^^^^^
if(strcmp(tstr,sstr)==0)
c++;
}
}
cout<<c;
return 0;
}
This will solve your problem more efficiently in O(n*m) complexity, where n is large string size and m is small string size.
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int i, j, k, c=0;
char lstr[30],sstr[10];
cout<<"Enter large string:"<<endl;
cin>>lstr;
cout<<"Enter small string:"<<endl;
cin>>sstr;
for(i=0; lstr[i]; i++) // You don't need to calculate strlen each time. This loop will break when lstr reaches null ('\0') character
{
if(lstr[i]==sstr[0])
{
for(k=0,j=i; sstr[k] && lstr[j]; k++,j++) // don't need to calculate strlen each time
{
if(sstr[k]!=lstr[j]) // Break if not match
break;
}
if(k==strlen(sstr)) // Whole string matched
c++;
}
}
cout<<c<<"\n";
return 0;
}
If you want to solve this problem in O(n) complexity you can use KMP algorithm. Which is the best choice for this kind of problems.