runtime error (SIGBART) - SPOJ Bicolorable - c++11

I'm doing this problem on SPOJ and I keep getting a runtime error (SIGABRT) and I've never really experienced this for. When I run my code in VS it works properly but SPOJ gives me this error. I was wondering if you could tell me why I'm getting it.
Here's my code:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <map>
using namespace std;
vector<int> marked;
vector<vector<int>> edges;
bool colorable;
void bfs(int v) {
colorable = true;
switch (marked[v]) {
case 0:
case 1:
marked[v] = 1;
for (int i = 0; i < edges[v].size(); i++) {
if (marked[edges[v][i]] != 1) {
marked[edges[v][i]] = 2;
}
else {
colorable = false;
break;
}
}
break;
case 2:
for (int i = 0; i < edges[v].size(); i++) {
if (marked[edges[v][i]] != 2) {
marked[edges[v][i]] = 1;
}
else {
colorable = false;
break;
}
}
}
}
int main() {
int N, M, a, b;
while (scanf("%d", &N) != 0) {
colorable = true;
while (N--) {
marked.push_back(0);
edges.push_back({ });
}
scanf("%d", &M);
while (M--) {
scanf("%d", &a);
scanf("%d", &b);
edges[a].push_back(b);
edges[b].push_back(a);
}
for (int i = 0; i < marked.size(); i++) {
bfs(i);
}
colorable ? printf("BICOLORABLE\n") : printf("NOT BICOLORABLE\n");
marked.clear();
edges.clear();
}
return 0;
}
I've tried Googling other similar issues and what a SIGABRT error is exactly and I'm still confused. Thanks for your help.

I found the problem and in case anyone ever has a similar issue I'll post it here. The line
while(scanf("%d", &N) != 0)
is apparently not appreciated by SPOJ. I split it into two lines
scanf("%d", &N);
while(N !=0 ) {}
and everything worked just fine. Thanks for your comments to help me understand the error.

Related

Why does my code giving me "mismatch in formal parameter list"?

I am trying to transfer the elements from a source stack to a destination stack. And for that i am using some variables and making sure that get transfered into the destination stack in the same order as they were in the source stack. I wrote the following code
#include <iostream>
#include <stack>
using namespace std;
template <typename S>
void transferByVar(stack<S> &source, stack<S> &dest)
{
int var = 0;
S topVal;
if (source.empty())
return;
else if (source.size() == 1)
{
dest.push(source.top());
source.pop();
}
int size = source.size();
while (count != size)
{
topVal = source.top();
source.pop();
while (source.size() != count)
{
dest.push(source.top());
source.pop();
}
source.push(topVal);
while (!dest.empty())
{
source.push(dest.top());
dest.pop();
}
++count;
}
}
int main()
{
stack <int> s1;
stack<int> s2;
s1.push(0);
s1.push(1);
s1.push(2);
s1.push(3);
s1.push(4);
s1.push(5);
s1.push(6);
s1.push(7);
s1.push(8);
s1.push(9);
transferByVar(s1, s2);
int size = s2.size();
for (int i = 0; i < size; i++)
{
cout << s2.top() << " ";
s2.pop();
}
return 0;
}
but it gives me an error of C2563: mismatch of formal parameter list. What can I do to fix this?

Tell me please how to find a solution to the challenge from the ICPC programming Contest [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I’ve been trying to find a solution to the problem described below for several days.
Nuts
Today, Sema and Yura attended the closing ceremony of one Olympiad. On the holiday tables there were n plates of nuts. The i-th plate contains ai nuts.
In one minute Sema can choose some plates and a certain number x, after which from each selected plate he picks up exactly x nuts (of course, each selected plate should have at least x nuts).
Determine in what minimum number of minutes all the nuts will be in Sema's pocket.
Input
The first line contains one integer n (1 ≤ n ≤ 50) - the number of plates with nuts.
Second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 50) - the number of the nuts in the i-th plate.
Output
Print a single number - the required minimum number of minutes.
Input example #1
4
7 4 11 7
Output example #1
2
Here is the link to the task: https://www.e-olymp.com/en/problems/8769 Here you can check the solutions.
I would be very grateful if someone would at least tell me which way to go in order to find a solution algorithm. Thanks.
My best solutions were
#include <iostream>
#include <set>
using namespace std;
int main() {
int n, inputNumber;
cin>>n;
set<int> combinations, newCombinations, inputNumbers, existValuesList;
for(int i = 0; i < n; i++) {
cin>>inputNumber;
inputNumbers.insert(inputNumber);
}
for(auto inputValue = inputNumbers.begin(); inputValue != inputNumbers.end(); ++inputValue) {
for(auto combination = combinations.begin(); combination != combinations.end(); ++combination) {
if (existValuesList.find(*inputValue) != existValuesList.end())
break;
newCombinations.insert(*combination + *inputValue);
if (inputNumbers.find(*combination + *inputValue) != inputNumbers.end()) {
existValuesList.insert(*combination + *inputValue);
}
}
combinations.insert(*inputValue);
combinations.insert(newCombinations.begin(), newCombinations.end());
newCombinations.clear();
}
cout<<inputNumbers.size() - existValuesList.size();
return 0;
}
71% of tests
and
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
class Nuts {
public:
int n;
set<int> inputNumbers;
set<int> combinations;
set<int> elementary;
set<int> brokenNumbers;
map<int, set<int>> numbersBreakDown;
set<int> temporaryCombinations;
void setN() {
cin>>n;
}
void setInputNumbers() {
int number;
for(int i = 0; i < n; i++) {
cin>>number;
inputNumbers.insert(number);
}
}
void calculateCombinations() {
for(int inputNumber : inputNumbers) {
temporaryCombinations.insert(inputNumber);
for(int combination : combinations) {
calculateCombination(inputNumber, combination);
}
combinations.insert(temporaryCombinations.begin(), temporaryCombinations.end());
temporaryCombinations.clear();
}
}
void calculateCombination(int inputNumber, int combination) {
if (brokenNumbers.find(inputNumber + combination) != brokenNumbers.end()) {
return;
}
if (inputNumbers.find(combination + inputNumber) != inputNumbers.end()) {
elementary.insert(inputNumber);
brokenNumbers.insert(inputNumber + combination);
addNumbersBreakDown(inputNumber, combination, breakDownNumber(combination));
}
temporaryCombinations.insert(combination + inputNumber);
}
void addNumbersBreakDown(int inputNumber, int combination, set<int> numberBreakDown) {
set<int> temporaryNumberBreakDown;
temporaryNumberBreakDown.insert(inputNumber);
temporaryNumberBreakDown.insert(numberBreakDown.begin(), numberBreakDown.end());
numbersBreakDown.insert(pair<int, set<int>>(inputNumber + combination, temporaryNumberBreakDown));
}
set<int> breakDownNumber(int combination, int count = 5) {
set<int> numberBreakDown;
for (int i = 0; i < count; i++) {
for(int it : inputNumbers) {
if (it > combination) {
continue;
}
if (it == combination) {
numberBreakDown.insert(combination);
return numberBreakDown;
}
if (combinations.find(combination - it) != combinations.end()) {
combination = combination - it;
break;
}
}
}
}
void throwOutElementaryBrokenNumbers() {
for(pair<int, set<int>> num : numbersBreakDown) {
if (brokenNumbers.find(num.first) == brokenNumbers.end()) {
continue;
}
throwOutElementaryBrokenNumber(num);
}
}
void throwOutElementaryBrokenNumber(pair<int, set<int>> num) {
int count = 0;
for(pair<int, set<int>> num1 : numbersBreakDown) {
if (num1.first != num.first && num1.second.find(num.first) != num1.second.end()) {
count++;
if (count > 1) {
brokenNumbers.erase(num.first);
break;
}
}
}
}
void throwOutBrokenNumbers() {
for(pair<int, set<int>> num : numbersBreakDown) {
if (brokenNumbers.find(num.first) == brokenNumbers.end()) {
continue;
}
int count = 0;
for(int number : num.second) {
if (brokenNumbers.find(number) != brokenNumbers.end()) {
count++;
if (count > 1) {
brokenNumbers.erase(number);
break;
}
}
}
}
}
void getResult() {
cout<<inputNumbers.size() - brokenNumbers.size();
}
void showSet(set<int> mn) {
for (int i : mn)
cout<<i<<" ";
cout<<endl;
}
};
int main() {
Nuts task = Nuts();
task.setN();
task.setInputNumbers();
task.calculateCombinations();
task.throwOutElementaryBrokenNumbers();
task.throwOutBrokenNumbers();
task.getResult();
return 0;
}
56% of tests
and
#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
set<int> getSumSet(set<int> inputValue, int sum, set<int> combinations, set<int> output = {}) {
set<int> tempComb;
bool ex = false;
for(int i = 0; i < 5; i++) {
for (int val : inputValue) {
tempComb.insert(val);
if (sum == val) {
output.insert(val);
combinations.clear();
return output;
}
for (int comb : combinations) {
if (combinations.find(comb - val) != combinations.end()) {
output.insert(val);
val = comb;
ex = true;
break;
}
}
if (ex) {
ex = false;
break;
}
}
}
return output;
}
int findLoc(set<int> numbers, int val) {
int result = 0;
for (int i : numbers) {
result++;
if (i == val) {
break;
}
}
return numbers.size() - result;
}
int main() {
int n, inputNumber;
cin>>n;
set<int> combinations, inputNumbers, copyInputNumbers, numbersForBreakdown, tempCombinations, elementaryNumbers, test;
for (int i = 0; i < n; i++) {
cin>>inputNumber;
inputNumbers.insert(inputNumber);
copyInputNumbers.insert(inputNumber);
}
elementaryNumbers.insert( *inputNumbers.begin() );
for (int number : inputNumbers) {
tempCombinations.insert(number);
if (copyInputNumbers.find(number) != copyInputNumbers.end()) {
copyInputNumbers.erase(number);
elementaryNumbers.insert(number);
}
for (int combination : combinations) {
if (copyInputNumbers.find(combination + number) != copyInputNumbers.end()) {
set<int> brN = getSumSet(inputNumbers, combination, combinations);
brN.insert(number);
copyInputNumbers.erase(combination + number);
set_difference(brN.begin(), brN.end(), elementaryNumbers.begin(), elementaryNumbers.end(), inserter(test, test.begin()));
if (findLoc(inputNumbers, combination + number) > test.size() && test.size() < 3) {
elementaryNumbers.insert(brN.begin(), brN.end());
}
brN.clear();
test.clear();
}
tempCombinations.insert(combination + number);
}
combinations.insert(tempCombinations.begin(), tempCombinations.end());
tempCombinations.clear();
}
cout<<elementaryNumbers.size();
return 0;
}
57% of tests
Had a go at it and got accepted, although i doubt that my solution is the intended approach.
There are a two observations that helped me:
No number is chosen more than once.
6 minutes is sufficient in all cases. (There is a subset S of {1,2,4,8,11,24} for all numbers k from 1 to 50 such that the numbers in S add up to k).
We can therefore test all subsets of {1,...,50} with at most 5 elements, and if we don't find a solution with less than 6 elements, just output 6.
There are ~2.4 million such subsets, so testing them is feasible if you are able to do this test efficiently (i got it down to linear time complexity in the number of elements in the set using bit manipulation).

SPOJ - Runtime error SIGSEGV

Following is the implementation of infix to postfix conversion, it is working fine on my computer, but as I am submitting in on SPOJ it is giving me Runtime error SIGSEGV, I am new to competitive programming and I am unable to handle such type of errors.
#include <iostream>
#include <stack>
#include<string.h>
#include<ctype.h>
using namespace std;
int prec(char ch){
switch(ch){
case '^' : return 3;
break;
case '*':
case '/': return 2;
break;
case '+':
case '-': return 1;
break;
default: return -1;
}
}
void pti(char a[]){
stack<int> post;
int k = -1;
for(int i = 0;i<strlen(a);i++){
if(isalnum(a[i]))
a[++k] = a[i];
else if(a[i] == '(')
post.push(a[i]);
else if(a[i] == ')'){
while(!post.empty() && post.top()!= '('){
a[++k] = post.top();
post.pop();
}
post.pop();
}
else {
while(!post.empty() && prec(a[i]) <= prec(post.top())){
a[++k] = post.top();
post.pop();
}
post.push(a[i]);
}
}
while(!post.empty()){
a[++k] = post.top();
post.pop();
}
a[++k] = '\0';
cout<<a<<endl;
}
int main()
{
int t;
cin>>t;
for(int i = 0;i<t;i++){
char a[100];
cin>>a;
pti(a);
}
}
You just need to make the input array longer, e.g. a size of 1000 gets AC.
The SIGSEGV signal means a segmentation fault occured, which basically means you accessed memory that doesn't belong to you.

Memory Limit Exceeded with Segment Tree in SPOJ Posters?

Given a horizontal section of wall , and N layers of paints applied from co-ordinates Xi to Yi , Output the distinct number of layers visible.
Here is the problem link http://www.spoj.com/problems/POSTERS/
Here is my solution http://ideone.com/gBJKnL
Approach :
I tried solving the problem by lazily updating child node values through a Segment Tree , the most recent value replaces the older one in their lazy updates. This way only the recent paint gets applied into the horizontal cross-section. although the code works fine on custom test cases , It takes a lot of memory and gets aborted by the Online Judge .
#include <iostream>
#include <set>
#include <vector>
#define MAX 10000000+100
typedef long long int ll;
using namespace std;
ll Tree[3*MAX],lazy[MAX*2];
void Update(ll s,ll start,ll end,ll left,ll right,ll value)
{
if(lazy[s]!=0)
{
Tree[s]=(lazy[s]*(end-start+1));
if(start!=end)lazy[2*s+1]=lazy[s],lazy[s*2+2]=lazy[s];
lazy[s]=0;
}
if(start>end||left>end||right<start)return;
if(start>=left&&end<=right)
{
Tree[s] = (value*(end-start+1));
if(start!=end)
{
lazy[2*s+1]=value;
lazy[2*s+2]=value;
}
return ;
}
ll mid=(start+end)/2;
Update(2*s+1,start,mid,left,right,value);
Update(2*s+2,mid+1,end,left,right,value);
Tree[s] = Tree[s*2+1]+Tree[2*s+2];
}
ll Read(ll s,ll start,ll end,ll left,ll right)
{
if(start>end||start>right||end<left)return 0;
if(lazy[s]!=0)
{
Tree[s]=(lazy[s]*(end-start+1));
if(start!=end)
{
lazy[2*s+1]=lazy[s];
lazy[2*s+2]=lazy[s];
}
lazy[s]=0;
}
if(start>=left&&end<=right)return Tree[s];
else return (Read(2*s+1,start,(start+end)/2,left,right)+Read(2*s+2,1+((start+end)/2),end,left,right));
}
int main() {
// your code goes here
ll t;
cin>>t;
while(t--)
{
ll n,z=1,li=-1;
cin>>n;
vector<pair<ll,ll> > b;
for(ll i=0;i<n;i++)
{
ll u,v;
li = max(li,v);
cin>>u>>v;
b.push_back(make_pair(u-1,v-1));
}
for(auto v: b)
Update(0,0,li+2,v.first,v.second,z++);
set<ll> a;
for(ll i=0;i<li+2;i++)cout<<Read(0,0,li+2,i,i)<<" ",a.insert(Read(0,0,li+2,i,i));
cout<<endl;
cout<<a.size()-1<<endl;
}
return 0;
}
Here is how you should be doing it:
#include <bits/stdc++.h>
#define mx 400005
using namespace std;
int tr[mx], lz[mx];
int t, n, l, r;
void update(int node, int s, int e, int l, int r, int val)
{
if(lz[node]!=0)
{
tr[node]=lz[node];
if(s!=e)
{
lz[node*2]=lz[node];
lz[node*2+1]=lz[node];
}
lz[node]=0;
}
if(s>e || r<s || l>e)
return;
if(s>=l && e<=r)
{
tr[node]=val;
if(s!=e)
{
lz[2*node]=val;
lz[2*node+1]=val;
}
return;
}
int m=s+(e-s)/2;
update(2*node,s,m,l,r,val);
update(2*node+1,m+1,e,l,r,val);
tr[node]=val;
//tr[node]=max(tr[2*node],tr[2*node+1]);
}
int query(int node, int s, int e, int l, int r)
{
if(r<s || e<l)
return 0;
if(lz[node]!=0)
{
tr[node]=lz[node];
if(s!=e)
{
lz[node*2]=lz[node];
lz[node*2+1]=lz[node];
}
lz[node]=0;
}
if(l<=s && r>=e)
return tr[node];
int m=s+(e-s)/2;
return query(2*node,s,m,l,r)+query(2*node+1,m+1,e,l,r);
}
int main()
{
//cout << "Hello world!" << endl;
cin>>t;
while(t--)
{
for(int i=0; i<mx; i++) tr[i]=0;
cin>>n;
int lr[n+1][2];
map<int,bool> mark;
vector<int> vec;
//int c=0;
for(int i=0; i<n; i++)
{
cin>>l>>r;
lr[i][0]=l;
lr[i][1]=r;
if(mark[l]==0)
{
vec.push_back(l);
mark[l]=1;
}
if(mark[r]==0)
{
vec.push_back(r);
mark[r]=1;
}
}
sort(vec.begin(), vec.end());
map<int,int> mp;
int c=1;
for(int i=0; i<vec.size(); i++)
mp[vec[i]]=c++;
for(int i=0; i<n; i++)
{
//cout<<mp[lr[i][0]]<<" "<<mp[lr[i][1]]<<"\n";
update(1,1,vec.size(),mp[lr[i][0]],mp[lr[i][1]],i+1);
}
set<int> ans;
for(int i=1; i<=vec.size(); i++)
{
//cout<<query(1,1,vec.size(),i,i)<<" ";
ans.insert(query(1,1,vec.size(),i,i));
}
int k = ans.size();
if(ans.find(0) != ans.end())
k--;
printf("%d\n",k);
}
return 0;
}

Extract IP from text file C++

Im a new programmer in C++ and I want to creat a code that extract IP from text files
I tried to convert txt file to Vector(string) to be easy filtering but i cant get all formes like XXX.XXX.XXX.XXX
Given that the ip could be embedded in some text, we'll parse the string and fetch it.
#include <iostream>
#include <string>
#include <iterator>
#include <vector>
#include <sstream>
using namespace std;
string ip(string str)
{
//middle portion
auto firstDot = str.find_first_of('.');
auto lastDot = str.find_last_of('.');
int dotCount = 0;
for(auto i = firstDot; i <= lastDot; i++)
{
if(!isdigit(str.at(i)) && str.at(i) != '.') //e.g 127.ss.0y.1
return string("");
if(str.at(i) == '.')
dotCount++;
}
if(dotCount != 3) //eg. 127.0.1 (not sure if this is wrong though)
return string("");
string result = str.substr(firstDot,lastDot-firstDot + 1);
//left portion
size_t x = 0; //number consegative digits from the first dot
for(auto i = firstDot-1; i >= 0; i--)
{
if(!isdigit(str.at(i)))
break;
else if(x == 3) //take first 3
break;
x++;
}
if(x == 0)
return string("");
result.insert(0,str.substr(firstDot-x,x));
//right portion
size_t y = 0;
for(auto i = lastDot + 1; i < str.length(); i++)
{
if(isdigit(str.at(i)))
{
if(y == 3)
break;
result.push_back(str.at(i));
y++;
}
else
break;
}
if(y == 0)
result.push_back('0');
return result;
}
int main()
{
string test = "1111127.0.0.11111 xx23.45.12.# xxxx.34.0.13 124.sd.2.1 sd.45.56.1";
string x,y;
vector<string> ips;
stringstream stream;
stream<<test;
while(stream>>x)
if(!(y = ip(x)).empty())
ips.push_back(y);
for(auto z : ips)
cout<<z<<"\t";
cout<<endl;
system("pause");
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ifstream myReadFile;
myReadFile.open("text.txt");
char output[100];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
cout<<output;
}
}
myReadFile.close();
return 0;
}
Use this if the text file only includes IP's on each line.
Or depending on what c++ you're using:
std::ifstream file("Read.txt");
std::string str;
std::string file_contents;
while (std::getline(file, str))
{
file_contents += str;
file_contents.push_back('\n');
}
I'm new to C++11. I did the following simple example for you. It's based on the regex library. Hope it works for you!
#include <iostream>
#include <string>
#include <regex>
int main ()
{
std::string s ("There's no place like 127.0.0.1\n");
std::smatch m;
std::regex e ("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
return 0;
}

Resources