Problem → CodeChef: Practical coding for everyone
This code is giving SIGSEGV error.
int n, k; cin>>n>>k;
string s; cin>>s;
int dist=0;
for(int i=0; i<n-1; i++){
dist+= (s[i]!=s[i+1])? 1: 2;
}
while(k--){
int i; cin>>i;
i--;
if((i+1)<n) dist-=(s[i]!=s[i+1])?1:2;
if((i-1)>=0) dist-= (s[i]!=s[i-1])?1:2;
if(s[i]=='0') s[i]='1';
else s[i]='0';
if((i+1)<n) dist+=(s[i]!=s[i+1])?1:2;
if((i-1)>=0) dist+= (s[i]!=s[i-1])?1:2;
cout<<dist<<endl;
}
ssjgz
2
Please post all of your code (or link to your submission)
Edit:
If it’s this, there’s an out-of-bounds access with the sample test input:
[simon@simon-laptop][12:28:14]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh
Compiling m_vaidya-TANDJ1.cpp
Executing command:
g++ -std=c++17 m_vaidya-TANDJ1.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
Successful
[simon@simon-laptop][12:28:36]
[~/devel/hackerrank/otherpeoples]>echo "3
1 1 2 2 2
1 1 2 3 4
1 1 1 0 3
" | ./a.out
0
1
/usr/include/c++/9/bits/basic_string.h:1067: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = long unsigned int]: Assertion '__pos <= size()' failed.
Aborted (core dumped)
Edit2:
Is that code even intended for this Problem? The code looks like it’s supposed to input and use binary strings.
Extremely Sorry !
I posted link to the different question and also submitted correct solution to wrong problem.
Once again, extremely sorry.
This was the question: CodeChef: Practical coding for everyone
Anyway, got AC !
1 Like