RunTime Error on online compilers when using string in c++ - c++

I am trying to run this code on online compilers and it shows Runtime errors while it runs perfectly locally. The parts of code that are not being used have been commented out.
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main() {
// your code goes here
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int x,y;
cin>>x>>y;
string ans;
int noOfApple=0,noOfBanana=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='a')
{
noOfApple++;
}
else
{
noOfBanana++;
}
}
int noOfABlock=ceil(noOfApple/x);
//int noOfBBlock=ceil(noOfBanana/y);
int length=floor(noOfBanana/(noOfABlock-1));
int remainderB=noOfBanana%(noOfABlock-1);
int counter=0;
//condition for '*'
if(noOfBanana<(noOfABlock-1))
{
int q=noOfBanana;
for(int i=0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
if(q>0)
{
ans[counter++]='b';
q--;
}
else
{
ans[counter++]='*';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
else{
for(int i =0;i<(noOfABlock-1);i++)
{
for(int j=0;j<x;j++)
{
ans[counter++]='a';
}
for(int j=0;j<length;j++)
{
ans[counter++]='b';
}
while(remainderB>0)
{
remainderB--;
ans[counter++]='b';
}
}
for(int i=0;i<x;i++)
{
ans[counter++]='a';
}
}
cout<<ans<<endl;
}
return 0;
}
Input for this program:-
6
ab
1 1
aab
1 1
aabb
1 2
aaaaab
2 1
aaaa
1 3
aaaaa
2 2
When Arrays are used we have to initialize the size of the arrays as they are static but as string are dynamic we do not need to initialize with a size.

Related

My code to find no. of a substrings in a string. Why is j<=str.length() - 3 causing runtime error?

#include <iostream>
#include <string>
using namespace std;
int main() {
int T;
int count = 0;
cin>>T;
for(int i = 0; i<T; i++)
{
string str;
cin>>str;
count = 0;
This part is for checking how many occurrences of "xyz" are there in my code.
The test conditon j<=str.length() - 3 in for loop is causing error for some test case(s).
When I run the for loop test condition j<=str.length() it does not give error.
for(int j = 0;j<=str.length()-3; j++)
{
string x;
x = str.substr(j, 3);
if(x =="xyz")
{
count++;
}
}
if(count)
{
cout<<count<<endl;
}
else
{
cout<<"-1"<<endl;
}
}
return 0;
}
String lengths use unsigned math; 2-3 becomes 2^64-1. Try j<-3 || j+3<=str.length()

adding 2 large integer with array

ive been trying to add 2 large integer on c++ using array and the output is always 0000 and i cant identified the problem
what im trying to achieve is like 30000000000 + 50000000 = 30050000000.
and this is the code that i wrote
, can anyone help me trying to figure it out why the output is 0000?
#include <math.h>
using namespace std;
int array1[100];
int array2[100];
int hasil[100];
int func1(int n)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array1[b]=n%10;
n=n/10;
b--;
}
int func2(int p)
{
int b;
while(b>0)
b=sizeof(array1[100]);
array2[b]=p%10;
p=p/10;
b--;
}
int func3(int i)
{ int satuan,puluhan,bil;
for(i=sizeof(array2[100]);i>0;i--)
{
int bil = array1[100]+array2[100];
satuan=bil%10;
hasil[i]=satuan+puluhan;
puluhan=bil/10;
}
}
int main ()
{ int n,p,i;
int func1(n);
int func2(p);
int func3(i);
int satuan=0;
int puluhan=0;
int x,y;
cout<<"masukan bilangan pertama = ";
cin>>n; cout<<endl;
cout<<"masukan bilangan ke dua = ";
cin>>p; cout<<endl;
for(x=sizeof(hasil[100]);x>0;x--)
{
y=0;
cout<<hasil[y];
y++;
}
} ```

Wrong answer on SPOJ (PRIME1)

Here is the link to the problem -> PRIME1
It asks us to print all the prime numbers between two numbers m and n
I used segmented sieve to solve the problem. Stored all the primes till sqrt(10^9) and used them to get primes in a specific range. Please help me out.
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
vector<bool> prime(32022);
void precompute()
{
int n=32000;
prime[0]=prime[1]=true;
for(int i=2;i*i<=32000;i++)
{
for(int j=i*i;j<=32000;j+=i)
prime[j]=true;
}
for(int i=0;i<=32000;i++)
if(!prime[i]) v.push_back(i);
}
int main() {
precompute();
int t; scanf("%d",&t);
while(t--)
{
int m,n; scanf("%d%d",&m,&n);
if(n<=32000)
{
for(int i:v)
{
if(i>n) break;
if(i>=m && i<=n)
printf("%d\n",i);
}
}
else
{
vector<bool> prime(n-m+1,true);
for(int i:v)
{
int st=(m/i)*i;
if(st<m) st+=i;
while(st<=n)
{
prime[st-m]=false;
st+=i;
}
}
for(int i=0;i<n-m+1;i++)
{
if(prime[i]) printf("%d\n",i+m);
}
}
printf("\n");
}
}

Intersection of 2 bidimensional arrays

I want to determine the intersection of 2 bi-dimensional arrays.
Here is my code:
#include < iostream >
using namespace std;
void type(int mat[][10],int n)
{
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>mat[i][j];
}
void inter(int a[][10], int n,int b[][10],int m)
{
int k1=0,p, x,ok,j, c[50],i;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
p=0; x=0;
while(p<m)
{
ok=0;
while(x<m)
{
if(a[i][j]==b[p][x]) ok=1;
if(ok) c[k1++]=a[i][j];
x++;
}
p++;
}
for(i=0;i<n;i++)
{
cout<<c[i]<<" ";
cout<<endl;}
}
}
}
} // !!! added in edit!!!
int main()
{
int n,m,mat[10][10],c[30],mat2[10][10];
cout<<"n= ";cin>>n;
type(mat,n);
cout<<"m= "; cin>>m;
type(mat2,m);
inter(mat,n,mat2,m);
return 0;
}
It doesn't show me the expected answer. It shoes me numbers like these : 1
4758024
1
4758024
Can somebody help me?

WA in scubadiv spoj (top down dp)

I am stuck on this question getting WA.
I have seen many bottom up implementations of this question. My top-down implementation is not working with memoization and is working fine without it. How can i correct it ??
#include<cstdio>
#include<cstring>
#include<iostream>
#define INF 0x7FFFFFFF
using namespace std;
int o,n,num,ox[2000],nt[2000],wt[2000];
int dp[2000][2000];
int dive(int index,int oxygen,int nitrogen,int weight) {
if(dp[oxygen][nitrogen]!=-1) return dp[oxygen][nitrogen];
int &ret=dp[oxygen][nitrogen];
if(oxygen>=o&&nitrogen>=n) {
ret=weight;
return ret;
}
if(index==num) {
ret=INF;
return ret;
}
ret= min(dive(index+1,oxygen+ox[index],nitrogen+nt[index],weight+wt[index]),dive(index+1,oxygen,nitrogen,weight));
return ret;
}
main() {
int c;
scanf("%d",&c);
while(c--) {
memset(dp,-1,sizeof(dp));
scanf("%d%d",&o,&n);
scanf("%d",&num);
for(int i=0;i<num;++i) {
scanf("%d%d%d",&ox[i],&nt[i],&wt[i]);
}
printf("%d\n",dive(0,0,0,0));
}
return 0;
}
Try this test:
1
21 79
5
1 1 800
1 1 800
1 1 800
1 1 800
17 75 800
It seems to me the correct answer for it should be 800 * 5 = 4000 right? Your program outputs something else.
This gives wrong answer too :( could someone check pls
#include<iostream>
using namespace std;
int swap( int &a,int &b)
{
int temp=a;
a=b;
b=temp;
}
int min(int a, int b)
{
if(a<b)return a;else return b;
}
int max(int a,int b)
{
if(a>b)return a;else return b;
}
int minweight(int ans[][22][80],int arr[][3],int n,int oxy,int nit)
{
int prev=0,curr=1;
for(int i=1;i<=n;i++)
{
for(int j=0;j<=21;j++)
for(int k=0;k<=79;k++)
ans[curr][j][k]=min( ans[prev][j][k], arr[i][2]+ans[prev][max(0,j-arr[i][0])][max(0,k-arr[i][1])]);
swap(curr,prev);
}
return ans[n&1][oxy][nit];
}
int main()
{
int cases;
cin>>cases;
while(cases--)
{
int oxy,nit;
cin>>oxy>>nit;
int n;
cin>>n;
int arr[n+1][3];
for(int i=1;i<=n;i++)
{
cin>>arr[i][0]>>arr[i][1]>>arr[i][2];
}
int ans[2][22][80];
for(int j=0;j<oxy+1;j++)
for(int k=0;k<nit+1;k++)
ans[0][j][k]=9999;
ans[0][0][0]=0;
cout<<minweight(ans,arr,n,oxy,nit);
}
}