below is my c++ code
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int trials = 0;
cin >> trials;
int c = 1;
while (trials--) {
string grid[51];
int r = 0;
cin >> r;
int n = r;
int m = 0;
for (int i = 0; i < r; i++) {
string s;
cin >> s;
m = s.size();
grid[i] = s;
}
if (n < 3 || m < 3) {
cout << "Case #" << c++ << ": " << 0 << endl;
continue;
}
int rowCount[51][51] = {0};
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
rowCount[i + 1][j + 1] = rowCount[i + 1][j];
if (grid[i].at(j) == '0')
rowCount[i + 1][j + 1] += 1;
}
}
int columnCount[51][51] = {0};
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
columnCount[i + 1][j + 1] = columnCount[i][j + 1];
if (grid[i].at(j) == '0')
columnCount[i + 1][j + 1] += 1;
}
}
int table[51][51][51][51] = {0};
for (int h = 3; h <= n; h++) {
for (int w = 3; w <= m; w++) {
for (int i = 1; i + h - 1 <= n; i++) {
for (int j = 1; j + w - 1 <= m; j++) {
int i1 = i + h - 1;
int j1 = j + w - 1;
int vals[] = {table[i][j][i1][j1],
table[i + 1][j][i1][j1],
table[i][j + 1][i1][j1],
table[i][j][i1 - 1][j1],
table[i][j][i1][j1 - 1]};
table[i][j][i1][j1] = *max_element(vals, vals + 5);
// if (grid[i - 1].substr(j - 1, j1).find('.') == string::npos ||
// grid[i1 - 1].substr(j - 1, j1).find('.') == string::npos) {
// continue;
// }
if (rowCount[i][j1] - rowCount[i][j - 1] != j1 - j + 1 ||
rowCount[i1][j1] - rowCount[i1][j - 1] != j1 - j + 1) {
continue;
}
if (columnCount[i1][j] - columnCount[i - 1][j] != i1 - i + 1 ||
columnCount[i1][j1] - columnCount[i - 1][j1] != i1 - i + 1) {
continue;
}
if (table[i + 1][j + 1][i1 - 1][j1 - 1] + 1 > table[i][j][i1][j1]) {
table[i][j][i1][j1] = table[i + 1][j + 1][i1 - 1][j1 - 1] + 1;
}
}
}
}
}
cout << "Case #" << c++ << ": " << table[1][1][n][m] << endl;
}
return 0;
}
On running this code in Clion IDE (on my windows 11 system) I get the following error
Process finished with exit code -1073741571 (0xC00000FD)
This is basically a code for solving a competitive programming question. As soon as I execute the code I get the error. It doesn't even let me input trials.
I am not sure what the reason is for this. I believe it is to do with the memory available for execution. I have tried to change the memory settings for the CLion IDE but I still get the same error. The code above is long but you can just copy/paste it into your own environment and maybe have a look? It works alright on ideone (online compiler). Could anyone help with this?
Related
I get a problem
"free(): invalid pointer
Process finished with exit code 6"
when I am trying to delete dr[1].
Please say why there is the error in current input (str1, str2)?
'''
#include <iostream>
using namespace std;
int64_t myMin(int64_t first, int64_t second) {
return first > second ? first : second;
}
int main() {
string str1, str2;
str1 = "ABRA", str2 = "CADABRA";
int64_t n1 = static_cast<int64_t>(str1.length()), n2 = static_cast<int64_t>(str2.length());
int64_t **dp = new int64_t *[n2 + 1];
for (int i = 0; i <= n2; ++i) {
dp[i] = new int64_t[n1 + 1];
dp[i][0] = 0;
dp[0][i] = 0;
}
for (int64_t i = 1; i <= n2; ++i) {
for (int j = 1; j <= n1; ++j) {
if (str2[i - 1] == str1[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = myMin(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[n2][n1];
cout << '\n' << n2 << "\t" << n1 << '\n';
for (int i = n2; i >= 0; --i) {
// There is no problem with uncommented below line. Why?
// if (i != 1)
delete[] dp[i];
}
delete[] dp;
return 0;
}
'''
I use CLion framework.
dp[0][i] = 0;
When n2 > n1, this will write over the array dp[0] as i grows.
Adding a bound-check will fix this problem:
if (i <= n1)
dp[0][i] = 0;
In addition, I'd strongly recommend to learn how to debug program.
I am trying to make an infix calculator for which I am currently trying to convert numbers entered in a character array to double.
here's my code:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
char exp[500];
const int SIZE = 100;
char temp[SIZE];
char op;
int strLen = 0, k, l, num = 0, fnum = 0;
double number = 0;
cin.getline(exp, 500,'\n');
int i = 0, j = 0, fpoint=0;
cout << exp;
for (i = 0, j = 0; exp[j] != 0; i++)
{
if (i % 2 == 0)
{
for (int m = 0; exp[m] != ','; m++) //stopped working
temp[m] = exp[m];
cout << temp;
for (k = 0; k < SIZE && temp[k] != 0; k++)
{
strLen = k;
if (temp[k] == '.')
fpoint = k + 1;
}
cout << fpoint<<endl;
cout << "strLen" << strLen;
for (k = 0; k <= fpoint; k++)
{
num = num + ((temp[fpoint - k] - '0') * pow(10, k));
}
for (k = fpoint + 1, l = 0; k <= strLen; k++, l++)
{
fnum = fnum + ((temp[strLen - l] - '0') * pow(10, l));
}
number = num + (fnum / pow(10, strLen - fpoint + 1));
cout << number;
j = j + strLen + 1;
}
else
{
char op = temp[j];
cout << op;
}
}
system("pause");
return 0;
}
sample input
2.5*3
It stops working and gives segmentation fault as an error on the marked position.
This line for (int m = 0; exp[m] != ','; m++) //stopped working will always fail if there are no , characters since exp[m] != ',' will always be equal to true and so will reach beyond the end of the array of exp which triggers the "segmentation fault".
So the numbers pattern going like this :
Input : 2
Output :
0 0 0 1 0 0 0
0 0 2 0 12 0 0
0 3 0 0 0 11 0
4 0 0 0 0 0 10
0 5 0 0 0 9 0
0 0 6 0 8 0 0
0 0 0 7 0 0 0
I have solved it using usual method, but when I tried with array, the output is really messed up. Any suggestion how to make this number pattern with array?
This is my code that creates this number pattern :
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) cout << i << " ";
else if (i > mid && j == mid-n+i) cout << i << " ";
else cout << "0 ";
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) cout << (2*n-i) << " ";
else if (i < mid && j == mid+i-1) cout << (2*n-i) << " ";
else cout << "0 ";
}
cout << endl;
}
Thanks in advance.
I think this code(working) might help you.
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main() {
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
int arr[n][n];
memset(arr, 0, sizeof(arr));
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) arr[i - 1][j - 1] = i;
else if (i > mid && j == mid-n+i) arr[i - 1][j - 1] = i;
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) arr[i - 1][j - 1] = 2 * n - i;
else if (i < mid && j == mid+i-1) arr[i - 1][j - 1] = 2 * n - i;
}
}
}
Supplementing baymaxx, if you desire dynamic memory allocation:
#include <iostream>
#include <stdio.h>
#include <string.h>
int input, n, mid, i, j;
cin >> input;
n = (2*input)+3;
mid = (n+1)/2;
// create array of the specified size.
int** arr = (int**) malloc (n * sizeof(int**));
for (int p = 0; p < n; ++p) {
arr[p] = (int*) malloc(n * sizeof(int));
for (int q = 0; q < n; ++q) {
arr[p][q] = 0;
}
}
// fill array (algorithm attr. baymaxx)
for(i = 1; i <= n; i++) {
for (j = 1; j <= mid; j++) {
if (i <= mid && j == mid-i+1) {
arr[i - 1][j - 1] = i;
}
else if (i > mid && j == mid-n+i) {
arr[i - 1][j - 1] = i;
}
}
for (j = mid+1; j <= n; j++) {
if (i >= mid && j == n+mid-i) {
arr[i - 1][j - 1] = 2 * n - i;
}
else if (i < mid && j == mid+i-1) {
arr[i - 1][j - 1] = 2 * n - i;
}
}
}
// print array
for (int p = 0; p < n; ++p) {
for (int q = 0; q < n; ++q) {
int x = arr[p][q];
std::cout << x << " ";
}
std::cout << endl;
}
// delete array
for (int i = 1; i < n; ++i) {
free(arr[i]);
}
free(arr);
I have written this code using dynamic programming approach and I think the logic is good but the code is not displaying the result. The code is as follows:
#include <iostream>
using namespace std;
void LCS(int input1[], int input2[], int n, int m) {
int L[n + 1][m + 1]; /*This matrix stores the length of common subsequences*/
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= m; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (input1[i - 1] == input2[j - 1])
L[i][j] = 1 + L[i - 1][j - 1];
else
L[i][j] = max(L[i - 1][j], L[i][j - 1]);
}
}
int index = L[n][m];
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[index - 1] = input1[i - 1];
i--;
j--;
index--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
}
int main() {
int n, m;
cin >> n >> m;
int input1[n], input2[m]; /*two arrays from which longest subsequnce is to be found*/
for (int i = 0; i < n; i++)
cin >> input1[i];
for (int i = 0; i < m; i++)
cin >> input2[i];
LCS(input1, input2, n, m);
return 0;
}
The code terminates without showing any result!
I even switched to a different IDE but its the same. What is wrong with this?
You are modifying the index variable. Create a copy of it and modify that. Here I used temp.
int index = L[n][m];
int temp = index;
int lcs[index];
int i = n, j = m;
while (i > 0 && j > 0) {
if (input1[i - 1] == input2[j - 1]) {
lcs[temp - 1] = input1[i - 1];
i--;
j--;
temp--;
} else if (L[i - 1][j] > L[i][j - 1])
i--;
else
j--;
}
for (int i = 0; i < index; i++){
cout << lcs[i];
}
In your version index is decremented to zero when you want to print the result so nothing will be printed.
I am trying to solve this SPOJ problem. The question asks to find the shortest path for each black(1) pixel.
Since it is a unweighted graph I used BFS.
for input:
3 3
010
000
000
it's giving:
323
434
343
instead of:
101
212
323
This is my code
#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
typedef pair < int, int >ii;
int R, C, i, j;
queue < ii > myQueue;
int visit[100][100];
int dist[100][100];
void bfs(ii s)
{
int i, j;
int count = 0;
ii node;
memset(visit, 0, sizeof(visit));
memset(dist, 0, sizeof(dist));
myQueue.push(s);
dist[node.first][node.second] = 0;
while (!myQueue.empty()) {
node = myQueue.front();
myQueue.pop();
if (visit[node.first][node.second])
continue;
visit[node.first][node.second] = 1;
//cout << node.first << " " << node.second << "\n";
i = node.first;
j = node.second;
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
if (j + 1 < R && j + 1 >= 0) {
myQueue.push(make_pair(i, j + 1));
if(dist[i][j+1] == 0)
dist[i][j + 1] = dist[i][j] + 1;
}
if (i - 1 < C && i - 1 >= 0) {
myQueue.push(make_pair(i - 1, j));
if(dist[i-1][j] == 0)
dist[i - 1][j] = dist[i][j] + 1;
}
if (i + 1 < C && i + 1 >= 0) {
myQueue.push(make_pair(i + 1, j));
if(dist[i+1][j] == 0)
dist[i + 1][j] = dist[i][j] + 1;
}
}
}
int main()
{
char input[100][100];
scanf("%d %d", &R, &C);
for (i = 0; i < R; i++)
scanf("%s", &input[i]);
int GRID[R][C];
for (i = 0; i < R; i++)
for (j = 0; j < C; j++)
GRID[i][j] = input[i][j] - '0';
for (i = 0; i < R; i++)
for (j = 0; j < C; j++) {
if (GRID[i][j] == 1)
bfs(make_pair(i, j));
}
for (i = 0; i < R; i++) {
for (j = 0; j < C; j++) {
printf("%d", dist[i][j]);
}
printf("\n");
}
}
ideone
Try this:
if (j - 1 < R && j - 1 >= 0) {
myQueue.push(make_pair(i, j - 1));
if(dist[i][j - 1] == 0)
dist[i][j - 1] = dist[i][j] + 1;
}
do this for all dist[][].
You have doubled result may be because you run your BFS twice between paired vertices.
But I'm not sure.