You are not logged in.
I was participating in a contest yesterday and my program wasnt working.when I run a code compiled by gcc it shows an error and I think it has something to do with string cuz it doesnt happen when im using ll or int, here is the error:
4 1
0000
/usr/include/c++/15.1.1/bits/basic_string.h:1369: constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; reference = char&; size_type = long unsigned int]: Assertion '__pos <= size()' failed.
fish: Job 1, './pali' terminated by signal SIGABRT (Abort)
and here is the code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n,k;
string b;
int score = 0;
cin >> n >> k;
cin >> b;
for(int i=0;i<n;i++){
if(b[i]==b[i-n+1]){score++;}
}
if(score==k){
cout << "YES" << "\n";
}
else{
cout << "NO" << "\n";
}
}
here is my g++ command:
g++ code.cpp -o code -std=c++20 && ./code
Last edited by lelouchvibritannia (2025-05-27 21:41:38)
Offline
for(int i=0;i<n;i++){
if(b[i]==b[i-n+1]){score++;}
}
you're reading out of bounds here (let's say that n=10, the first comparison would be between b[0] and b[-9])
Offline
holy I didnt notice thank you :3
Offline
no problem! :3
Offline
Please always remember to mark resolved threads by editing your initial posts subject - so others will know that there's no task left, but maybe a solution to find.
Thanks.
Offline
Mod note: moving to Programming and Scripting
Sakura:-
Mobo: MSI MAG X570S TORPEDO MAX // Processor: AMD Ryzen 9 5950X @4.9GHz // GFX: AMD Radeon RX 5700 XT // RAM: 32GB (4x 8GB) Corsair DDR4 (@ 3000MHz) // Storage: 1x 3TB HDD, 6x 1TB SSD, 2x 120GB SSD, 1x 275GB M2 SSD
Making lemonade from lemons since 2015.
Offline