@baljot
can you please tell me where i am going wrong?
my sample test cases are passing but i am getting run time error
my code #include <bits/stdc++.h>
using namespace std; #define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
ll tc = 0;
while (t--)
{
string s;
cin >> s;
vector<string> v1;
string s1 = "KICK";
string s2 = "START";
for (ll i = 0; i < s.size() - 3; i++)
{
if (s1.compare(s.substr(i, 4)) == 0)
v1.push_back("KICK");
if (i == s.size() - 4)
break;
else if (s2.compare(s.substr(i, 5)) == 0)
v1.push_back("START");
}
bool lock = 0;
ll kick_count = 0;
ll res = 0;
for (ll i = 0; i < v1.size(); i++)
{
if (v1[i] == "KICK" || lock == 1)
{
lock = 1;
if (v1[i] == "KICK")
kick_count++;
else if (v1[i] == "START")
res = res + kick_count;
}
}
tc++;
cout << "Case #" << tc << ": " << res << endl;
}
return 0;
What if size of string s is less than equal to 3 ? Your code will give RTE their , Moreover if you want any other help then either format your code or provide link.
@dhruv788
thank you soo much for replying
but if the string is less than or equal to 3 it wont enter for loops and hence it will print 0 because i initialized ‘int res=0’ and i believe its a correct ans.
correct me if i am wrong
here is my modified(logic remains the same) and formated code
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
int tc = 0;
while (t--)
{
string s;
cin >> s;
vector<string> v1;
string s1 = "KICK";
string s2 = "START";
for (int i = 0; i < s.size() - 3; i++)
{
if (s1.compare(s.substr(i, 4)) == 0)
v1.push_back(s1);
else
{
if ((s2.compare(s.substr(i, 5)) == 0) && (i < s.size() - 4))
v1.push_back(s2);
}
}
bool lock = 0;
int kick_count = 0;
int res = 0;
for (int i = 0; i < v1.size(); i++)
{
if (v1[i] == s1 || lock == 1)
{
lock = 1;
if (v1[i] == s1)
kick_count++;
else
res = res + kick_count;
}
}
tc++;
cout << "Case #" << tc << ": " << res << endl;
}
return 0;
}
your s.substr is getting out of range.
and moreover cond1&&cond2 checked sequentially and your i<s.size-4 will never work it will throw error in first cond1 itself if it is out of index or bound.
@baljot
thank you soo much for replying
as you said if it is a index out of range error then it has to print some gibberish values once it goes out of the range, which will result in failing sample test cases as well, but my sample test cases are passing.
to check i just ran this code by trying to print all the substrings
for (int i = 0; i < s.size() - 3; i++)
{
if (s1.compare(s.substr(i, 4)) == 0)
{
v1.push_back(s1);
cout << s.substr(i, 4) << endl;
}
else
{
if ((s2.compare(s.substr(i, 5)) == 0) && (i < s.size() - 4))
{
v1.push_back(s2);
cout << s.substr(i, 5) << endl;
}
}
}
Test Case 1:AKICKSTARTPROBLEMNAMEDKICKSTART
Output :
KICK
START
KICK
START
as you can see it just prints fine and exact words without printing any gibberish values
as you can see in this test case my for loop for (int i = 0; i < s.size() - 3; i++) runs from
0 to < s.size-3
0 to < 31-3
0 to < 28
0 to 27
this for loop will run fine without giving index out of range while checking for substr “KICK”
problem is when we are checking for substr “START”, this will give index out of range error and hence i put this if statement if ((s2.compare(s.substr(i, 5)) == 0) && (i < s.size() - 4))
which will check
if(“START” && i < 31-4)
if(“START” && i < 27)
if(“START” && i = 0 to 26)
so this will not throw any index out of range error as per my understanding
please correct me if i am wrong,
s.size()-3
2-3
-1
for (int i = 0; i < s.size() - 3; i++)
0<-1 false
THIS IS WHAT I THOUGHT
but indeed ide gave me some long gibberish positive number wowww , i have no idea why but yes now my code got accepted what i did is just
int n=s.size() and used n instead of directly using s.size() in for loops
It’s not exactly gibberish: string::size is an unsigned value, typically (on most modern hardware/ software platforms ) a 64-bit unsigned integer with range 0 to 2^{64} which cannot represent negative numbers. Such numbers cause it to “wrap around”.
The value of -1 in a 64-bit integer is thus 2^{64}-1=18446744073709551615.