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.
Related
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?
I need to be able to track the number of exchanges and comparisons in this selection sort algorithm. The algorithm sorts the array just fine. I need to modify it in order to have tracked the number of exchanges.
void insertion_sort(int * theArray, int size)
{
int tmp;
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && theArray[j - 1] > theArray[j]; j--)
{
tmp = theArray[j];
theArray[j] = theArray[j - 1];
theArray[j - 1] = tmp;
}
}
}
Here are the two helper functions
bool inOrder(int i, int j) {
numComparisons++;
return i <= j;
}
void swapElement(int & i, int & j) {
int t = i;
i = j;
j = t;
numSwaps++;
}
Something like that ?
void insertion_sort(int * theArray, int size)
{
int tmp;
int count = 0;
for (int i = 1; i < size; i++)
{
for (int j = i; j > 0 && theArray[j - 1] > theArray[j]; j--)
{
tmp = theArray[j];
theArray[j] = theArray[j - 1];
theArray[j - 1] = tmp;
count++; // increment on each loop/exchanges
}
}
}
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);
This question already has answers here:
Passing a 2D array to a C++ function
(18 answers)
Closed 9 years ago.
Code in C++:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i,int j);
int main()
{
int n;
cin >> n;
int a[n][n];
int b[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = 0;
}
}
b[0][0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; i++)
{
cin >> a[i][j];
}
}
check(0, 0);
if (b[n - 1][n - 1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j)
{
if((i - 1) >= 0 &&
(i - 1) <= (n - 1) &&
(abs(a[i][j] - a[i - 1][j]) >= 10) &&
b[i - 1][j] == 0)
{
b[i - 1][j] = 1;
check(i - 1, j);
}
if((i + 1) >= 0 &&
(i + 1) <= (n-1) &&
(abs(a[i][j] - a[i + 1][j]) >= 10) &&
b[i + 1][j] == 0)
{
b[i + 1][j] = 1;
check(i + 1, j);
}
if((j + 1) >= 0 &&
(j + 1) <= (n - 1) &&
(abs(a[i][j] - a[i][j + 1]) >= 10) &&
b[i][j + 1] == 0)
{
b[i][j + 1] = 1;
check(i, j + 1);
}
if((j - 1) >= 0 &&
(j-1) <= (n - 1) &&
(abs(a[i][j] - a[i][j - 1]) >= 10) &&
b[i][j - 1] == 0)
{
b[i][j - 1] = 1;
check(i, j - 1);
}
}
My code has a function named check inside which integer variable n and arrays a and b. I want them to be made available to this function. If i declare global variable, then I can't use cin outside the main function.
How can I make these variables available to my check function?
Since you do not know the size of the array before you pass it into check, you need to use a pointer to a pointer (and not a 2D array). Here's the modified code (you also had a typo in your nested for-loop where you should have had a j instead of an i):
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
void check(int i, int j, int** a, int** b, int n);
int main() {
int n;
cin >> n;
int** a;
int** b;
a = new int* [n];
b = new int* [n];
for (int i = 0; i < n; i++)
a[i] = new int[n];
for (int i = 0; i < n; i++)
b[i] = new int[n];
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
b[i][j]=0;
}
}
b[0][0]=1;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cin >> a[i][j];
}
}
check(0,0,a,b,n);
if (b[n-1][n-1] == 1)
cout << "yesssss";
else
cout << "no!!!!";
}
void check(int i, int j, int** a, int** b, int n)
{
if((i-1)>=0 && (i-1)<=(n-1) && (abs(a[i][j]-a[i-1][j])>=10) && b[i-1][j]==0)
{
b[i-1][j]=1;
check(i-1,j,a,b,n);
}
if((i+1)>=0 && (i+1)<=(n-1) && (abs(a[i][j]-a[i+1][j])>=10) && b[i+1][j]==0)
{
b[i+1][j]=1;
check(i+1,j,a,b,n);
}
if((j+1)>=0 && (j+1)<=(n-1) && (abs(a[i][j]-a[i][j+1])>=10) && b[i][j+1]==0)
{
b[i][j+1]=1;
check(i,j+1,a,b,n);
}
if((j-1)>=0 && (j-1)<=(n-1) && (abs(a[i][j]-a[i][j-1])>=10) && b[i][j-1]==0)
{
b[i][j-1]=1;
check(i,j-1,a,b,n);
}
}
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.