i was solving the problem
https://www.spoj.pl/problems/ACPC11A/
and here is my code :
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main()
{
int tc,i,n;
scanf("%d",&tc);
while(tc--)
{
vector<string> v1,v2;
string str,w;
scanf("%d",&n);
int flag=0;
for(i=0;i<n;i++)
{
cin>>str;
if(str[0]!='#')
{
flag=1;
w=str;
}
else if(flag==0)
{
v1.push_back(str);
}
else
v2.push_back(str);
}
//print v2-->w-->v1
for(i=0;i<v2.size();i++)
{
cout<<v2[i]<<" ";
}
if(w!="")
cout<<w<<" ";
for(i=0;i<v1.size()-1;i++)
cout<<v1[i]<<" ";
cout<<v1[v1.size()-1]<<endl;
v1.clear();v2.clear();str.clear();w.clear();
}
return 0;
}
i am getting the correct output for the sample test case...but on submission my code gives segmentation fault.
my logic is simple..
i took 2 vectors 1 for storing words before a English word arrives(v1) and other for storing worlds after a English word arrives(v2)
after that i print the contents of v2 followed by word and then content of v1.
please help me in understanding why is this code giving segmentation fault.
don't bother guys...i got my mistake
Error is in line for(i=0;i<v1.size()-1;i++)
when v1.size() is 0 ,then as size() returns unsigned value...hence 0-1 will be very large value and hence the SIGSEGV
Related
I was working on a problem to find the longest substring without repeating characters. While trying for the solution I wrote this code. I'm not asking for the solution, but just the reason why the code works the way it does.
#include<iostream>
#include<map>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char,int> mp;
map<char,int>::iterator it;
int i=0,k=0,max=0;
while(i<s.length()){
it = mp.find(s[i]);
if(it==mp.end()){
k++;
}
else{
k=k-distance(mp.begin(),it);
cout<<distance(mp.begin(),it)<<" "; //this distance
mp.erase(mp.begin(),it);
}
if(k>max){
max=k;
}
mp.insert({s[i],1});
i++;
}
return max;
}
};
int main(){
Solution obj;
cout<<obj.lengthOfLongestSubstring("yvyf");
}
When I give the input as "yvyf" it prints the distance as 1, but when I give the input as "avaf" or "pvpf", it prints the distance as 0.
I'm getting a SIGABRT error when I compile the following code(PALIN problem on SPOJ).The objective of the code is to find smallest palindromic number which should be greater than the given number, where given number can have upto 1000000 digits.
Link to the problem is: http://www.spoj.com/problems/PALIN/ It runs well on codeblocks but SPOJ returns SIGABRT error. Can someone explain the reason?
#include<iostream>
#include<string>
using namespace std;
string palinodd(string num)//to find next nearest palindrome for an odd digit number
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)//checks if the middle most digit should be incremented
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2]++;
if(palin[num.size()/2]==':')//if the middle digit goes greater than 9
{
palin[num.size()/2]='0';
palin[num.size()/2-1]++;
palin[num.size()/2+1]++;
}
return palin;
}
string palineve(string num)//to find next nearest palindrome for an even digit number
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)//checks if middle digit should be incremented
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2-1]++;
if(palin[num.size()/2-1]==':')//if the middle digit goes greater than 9
{
palin[num.size()/2-2]++;
palin[num.size()/2+1]++;
palin[num.size()/2-1]='0';
}
palin[num.size()/2]=palin[num.size()/2-1];//updates the middle number
return palin;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string num="912496394";
string ans;
if(num.size()%2!=0)
ans=palinodd(num);
else
ans=palineve(num);
cout<<ans<<endl;
}
return 0;
}
Appreciate your effort, try to match c++ compiler, version in SPOJ and codeblocks. I tried your code with clang++-5.0 after changing num (912496394) to string but i didn't get expected results. I think you might have posted older version of your code, if possible you can post the updated code.
The process will be aborted with SIGABRT if there are any overflows in your code, as you are using more string operations you can refer below link which has some info on SIGABRT,
SIGABRT called when calling find() on a string element in an array
One suggestion on your approach to the problem, you can try to increment the input by one in a while loop until you find a palindrome or you reach largest number and print the results
The solution I found out for above problem is:
#include<iostream>
#include<string>
using namespace std;
string palinodd(string num)
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2]++;
if(palin[num.size()/2]==':')
{
palin[num.size()/2]='0';
palin[num.size()/2-1]++;
palin[num.size()/2+1]++;
}
for(int i=num.size()/2-1;i>0;i--)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i-1]++;
}
}
for(int i=num.size()/2+1;i<num.size()-1;i++)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i+1]++;
}
}
return palin;
}
string palineve(string num)
{
string palin=num;
int flag=0;
for(int i=num.size()/2-1;i>-1;i--)
{
if(flag==0)
{
if(num[i]<palin[num.size()-i-1])
flag=1;
else if(num[i]>palin[num.size()-i-1])
flag=-1;
}
palin[num.size()-1-i]=num[i];
}
if(flag!=-1)
palin[num.size()/2-1]++;
if(palin[num.size()/2-1]==':')
{
palin[num.size()/2-2]++;
palin[num.size()/2+1]++;
palin[num.size()/2-1]='0';
}
palin[num.size()/2]=palin[num.size()/2-1];
/*if any incremented digit becomes greater than 9,it shows a ':' so we make it 0 and increase previous(if ':' comes in the first half of the string)/next(if ':' comes in the last half of the sting) digit by 1.*/
for(int i=num.size()/2-2;i>0;i--)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i-1]++;
}
}
for(int i=num.size()/2+1;i<num.size()-1;i++)
{
if(palin[i]==':')
{
palin[i]='0';
palin[i+1]++;
}
}
return palin;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string num,ans;
cin>>num;
int cnt=0;
/*if all the digits in the string are 9, then the next palindromic number will have one more digit than the original number of digits. Thus, increasing the string length*/
for(int i=0;i<num.size();i++)
{
if(num[i]=='9')
cnt++;
}
if(cnt==num.size())
{
num[0]='1';
for(int i=1;i<num.size();i++)
num[i]='0';
num+='0';
}
if(num.size()%2!=0)
ans=palinodd(num);
else
ans=palineve(num);
cout<<ans<<endl;
}
return 0;
}
I have been debugging and seeing this code for nearly a week now still I couldn't find any solution and thus hoping to get one from the fellow developers in stack overflow.
This is the solution of the Faded Palindrome problem of September Challenge Codechef This is the problem page
#include<iostream>
#include<vector>
#include<iterator>
#include<stdlib.h>
#include<cstdio>
#include<string>
using namespace std;
string def="-1";
int main()
{
int t;
cin>>t;
//vector<string> out(t);
string out[100];
for(int i=0;i<t;i++)
{
string inp;
cin>>inp;
if(inp.size()%2) //Checking wether the number of letters is odd
{
int counter=inp.size()/2;
int counter_back=counter-1;
int counter_front=counter+1;
if(inp[counter]=='.')
inp[counter]='a';
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front=='.']){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
else{ //Checking the number of letter is even
//cout<<inp.size();
int counter_front=(inp.size())/2;
//cout<<counter_front;
int counter_back=counter_front-1;
//cout<<counter_back;
int not_fa=0;
while(counter_back>=0&&counter_front<inp.size())
{
if(inp[counter_back]==inp[counter_front])
{//cout<<"*-";
counter_back--;counter_front++;}
else if(inp[counter_back]=='.'&&inp[counter_front]=='.'){
inp[counter_back]=inp[counter_front]='a';
counter_back--;counter_front++;
}
else if(inp[counter_back]=='.'){
//cout<<"--";
inp[counter_back]=inp[counter_front];
counter_back--;counter_front++;
}
else if(inp[counter_front]=='.'){
inp[counter_front]=inp[counter_back];
counter_back--;counter_front++;
}
else{
not_fa=1;
break;
}
}
if(not_fa)
out[i]=def;
else
out[i]=inp;
}
}
/*vector<string>::iterator itr;
for(itr=out.begin();itr!=out.end();itr++)
cout<<*itr<<endl;*/
for(int j=0;j<t;j++)
if(j==t-1)
cout<<out[j];
else
cout<<out[j]<<"\n";
return 0;
}
While your code may pass all the test cases presented in the problem statement, there are still more edge cases that your code doesn't cover. Here's a sample test case:
6
..
...
..e
a..a.v.
p..p
t.xzt
This should print:
aa
aaa
eae
avaaava
paap
tzxzt
instead it prints:
..
.a.
aaa
aa.a.aa
p..p
taxat
Hints:
The first if statement is not correctly handling the case where inp[b] and inp[f] are both '.'.
There is no need for doing two different things when |s| is even or odd.
Also, in the first else if statement, you probably meant inp[counter_front]=='.' instead of inp[counter_front=='.'].
if(inp[counter_back]==inp[counter_front]) is true if they both are equal to '.' so you won't fill that gap.
Here is the link for the problem: http://www.spoj.com/problems/STAMPS/;
here is the ideone link for the current code: http://ideone.com/AcHfc6;
here is the code:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int t,x,n,sum,sum2,count,i,j;
scanf("%d",&t);
for(j=1;j<=t;j++)
{
cin>>x>>n;
sum=0;
int offer[n];
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
if(sum>=x)
{
sum2=0;
count=0;
for(i=n-1;i>=0;i--)
{
sum2+=offer[i];
if(sum2<=x)
count++;
else
break;
}
cout<<"Scenario #"<<j<<":"<<endl;
cout<<count<<endl;
cout<<endl;
}
else
{
cout<<"Scenario #"<<j<<":"<<endl;
cout<<"impossible"<<endl;
cout<<endl;
}
}
return 0;
}
The code gives the right answers for the given test cases but it causes TLE. I've tried converting my cin's and cout's to scanf/printf but, weirdly enough, the answers were not the same and I don't know how the answers were different from each other.
What's going wrong?
I suspect your main problem is here:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
sort(offer,offer+n);
}
You sort the data for every number that's entered. Also, you sort random data because you only have i rows of valid data in the array, not n rows. The sort should be done once, outside the loop:
for(i=0;i<n;i++)
{
cin>>offer[i];
sum+=offer[i];
}
sort(offer,offer+n);
Codeforces problem 373A-http://codeforces.com/problemset/problem/373/A
Instead of multiple if statements for counting number of elements of each number,I have tried to check for given condition after sorting the array.I am getting incorrect output for 1st test case(given in the link for the problem).What is wrong with my approach?What should I change in my solution.
My solution:
#include<iostream>
#include<cstring>
using namespace std;
int k,i,j,a;
char panel[17],temp,output[4];
int main()
{
cin>>k;
for(i=0;i<16;i++)
cin>>panel[i];
for(i=0;i<16;i++) //Bubble sort.
{
for(j=0;j<(15-i);j++)
{
if(panel[j]>panel[j+1])
{
temp=panel[j+1];
panel[j+1]=panel[j];
panel[j]=temp;
}
}
}
a=1;
strcpy(output,"YES");
for(i=0;i<16;i++)
{
if(panel[i]==panel[i+1])
++a;
else
a=1;
if(a>(2*k));
{
strcpy(output,"NO");
break;
}
}
cout<<output;
}
You have a semi-colon after the if statement:
if(a>(2*k));
That means, it's always going copy "NO" and break the loop. Remove it.