This is my first problem in CodeChef. I am getting Runtime(SIGBART) error on the following code. I tested the input cases given in the question and got the same output. But, still I am getting an error. Can anyone explain why I am getting the error and how can I rectify it ?
Here is my solution https://www.codechef.com/viewsolution/20237993
There are multiple reasons behind your code failing!
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;
void show(vector<int> v) {
for (int num:v)
cout << num << " ";
}
int main() {
string alphabets = "abcdefghijklmnopqrstuvwxyz";
int t;
cin >> t;
vector<string> test_strings;
for (int i = 0; i < t; i++) {
string tes;
cin >> tes;
test_strings.push_back(tes);
}
for (int i = 0; i < t; i++) {
string test = test_strings.at(i);
vector<int> counts;
for (char c:alphabets) {
int c_count = count(test.begin(), test.end(), c);
if (c_count > 0)
counts.push_back(c_count);
}
sort(counts.begin(), counts.end());
for (int i = 0; i < counts.size() - 2; i++)
if (counts.at(i + 2) == counts.at(i) + counts.at(i + 1)) {
if (i == counts.size() - 3)
cout << "Dynamic\n";
} else
cout << "Not\n";
}
return 0;
}
Note that the constraints state that the length of the input string is greater than 1.
For this input: 1 a
Your code at this line: counts.at(i + 2) == counts.at(i) + counts.at(i + 1) would throw an out of range exception.
(terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 2) >= this->size() (which is 1))
Also, you the local variable ‘i’ declared in the nested for loop hides the previous declaration of i in the outer for loop.
If a string is of length 1 or 2, can we call it ‘Dynamic’ ?
Well, it’s mentioned:
Note that if the number of distinct characters in the string is less than 3, i.e. if |C|<3, then the string is always dynamic.
So according to me, your code is valid for |C| > 3 else just print dynamic!
Accept the answer if it helped!