Longest Common Contiguous Substring Length in C - c

So the following is the code I wrote up for the algorithm. I cannot find what is wrong with it. The test cases are supposed to yield 12, 4, and 3 respectively but instead yield 8, 1, and 2 respectively. Did I misunderstand the algorithm structure?
#include <stdio.h>
#define MAX_STRING_LENGTH 100
void clear_memo(int memo[][MAX_STRING_LENGTH]);
// Returns the larger of a and b
int max(int a, int b){
return a ? a > b : b;
}
int lcs_length(char A[], char B[], int i, int j, int memo[][MAX_STRING_LENGTH]){
if(i == 0 && j == 0){
clear_memo(memo);
}
if (memo[i][j] > 0){
return memo[i][j];
}
if (A[i] == '\0' || B[j] == '\0'){
memo[i][j] = 0;
}
else if(A[i] == B[j]){
memo[i][j] = 1 + lcs_length(A, B, i+1, j+1, memo);
}
else{
memo[i][j] = max(lcs_length(A, B, i+1, j, memo), lcs_length(A, B, i, j+1, memo));
}
return memo[i][j];
}
// Makes all the entries zero in the memo array
void clear_memo(int memo[][MAX_STRING_LENGTH]){
for(int i = 0; i < MAX_STRING_LENGTH; i++){
for(int j = 0; j < MAX_STRING_LENGTH; j++){
memo[i][j] = 0;
}
}
}
// Tests the lcs_length() function
int main(){
int memo[MAX_STRING_LENGTH][MAX_STRING_LENGTH];
char a[] = "yo dawg how you doing?";
char b[] = "yo dawg zhzzzozw?";
printf("%d\n", lcs_length(a,b,0,0,memo));
char c[] = "nano";
char d[] = "nematode knowledge";
printf("%d\n", lcs_length(c,d,0,0,memo));
char e[] = "abcd";
char f[] = "abdc";
printf("%d\n", lcs_length(e,f,0,0,memo));
return 0;
}

Your max is wrong. a? a>b:b;
means if a is non zero, return a>b( which returns 1 if a>b and 0 otherwise) and if a is zero, returns b. So a is never returned even if it is greater and b is returned only if a is 0 irrespective of which is greater.
Use
int max(int a, int b){
return a>b?a:b;
}

Related

Char permutation algorithm in C that stores the output in an array

I need to store the permutations of four letters in C
i was trying to use this algorithm but no idea how to store the output in some array
if someone can correct this for me or give another algorithm i would appreciate
#include <stdio.h>
#include <string.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r)
{
int i;
if (l == r)
printf("%s\n", a);
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r);
swap((a + l), (a + i)); // backtrack
}
}
}
int main()
{
char str[] = "AGTC";
int n = strlen(str);
permute(str, 0, n - 1);
return 0;
}
You should note that you will require quite a large size array to store all the permutations. If you have a 4 byte string, this will be a 2D array of 24*5. So this is only practical if you know ahead of time the max size of the string you want to support.
The code below works for max 4 byte strings. For higher size, you need to increase both the dimensions of the 2D array storage. e.g. for 5 byte it will be 120*6
// global
char store[24][5];
void permute(char* a, int l, int r)
{
int i;
static int storeindex;
if (l == r)
{
strcpy(store[storeindex++],a);
}
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r);
swap((a + l), (a + i)); // backtrack
}
}
}
Additional note - The algorithm given above does not print distinct permutations. If the input string has duplicates, this algorithm will print permutations with duplicates. e.g. if input is AAAA output is 24 lines of AAAA
You could do it by using malloc. For this you need to know the number of combinations.
Combination would be factorial of size of string given.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r, char arr[], int n)
{
int i;
static long count = 0;
if (l == r)
{
//printf("%s\n", a);
memcpy(arr+count*n, a, n);
count++;
}
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r, arr, n);
swap((a + l), (a + i)); // backtrack
}
}
}
long factorial(int n)
{
int c = 0;
long fact = 1;
for (c = 1; c <= n; c++)
fact = fact * c;
return fact;
}
int main()
{
char str[] = "AGTC";
int n = strlen(str);
long t_comb = factorial(n);
char *arr = NULL;
char *print = NULL;
arr = (char *)malloc(t_comb * n);
if(arr == NULL)
{
printf("error\n");
}
print = (char *)malloc(n+1);
memset(print, '\0', n+1);
permute(str, 0, n - 1, arr, n);
long itr = 0;
for(itr = 0 ; itr < t_comb ; itr++)
{
memcpy(print, arr+itr*n, n);
printf("%s\n", print);
}
/* After using */
free(print);
free(arr);
return 0;
}

recursive find number in between in C

I want to find the number within a range in an array and must be in a recursive way. The function variables couldn't be modified.
Let's say in the range of 2 and 3
The input is : int a[] = {4, 1, 3, 1, 3, 2};
and the output will be = {3,3,2} , 3 found
Not sure how to code the recursive function in this case. The below I have tried not working.
int within(int a[], int N, int lower, int upper, int result[])
{
if(N == 1 && N <= upper && N>= lower)
return a[0];
return within(&a[1], N-1, lower, upper, result);
}
int main()
{
int a[] = {4, 1, 3, 1, 3, 2};
int result[6] = {0};
int i, nResult;
nResult = within(a, 6, 2, 3, result);
printf("%d data passed the bounds\n", nResult);
for (i = 0; i < nResult; i++){
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
I want to find the number within a range in an array
Let's say in the range of 2 and 3
Normally a for loop or similar would be so much easier here
If it has to be recursive....
// need to have another number - r - number in range
// r starts at zero
//
// normally lower case for variable and capitals for things you #define
// N starts at the number of elements of a less one
//
int within(int a[], int N, int lower, int upper, int r, int result[])
{
if(a[0] <= upper && a[0]>= lower) {
result[r]= a[0];
r++;
}
if(N==0) {
return r;
} else {
r = within(&a[1], N-1, lower, upper, r, result);
return r;
}
}
the function will give a return value of the number of values found within the range.
The code above is recursive, but so much more complicated and fragile than a simple loop... such as the fragment below
for (i=0;i<N;i++) {
if(a[i] <= upper && a[i]>= lower) {
result[r]= a[i];
r++;
}
}
If it has to be recursive wihtout r...
// need to have another number - result[0] - number in range
// result[0] starts at zero
//
// normally lower case for variable and capitals for things you #define
// N starts at the number of elements of a less one
//
int within(int a[], int N, int lower, int upper, int result[])
{
if(a[0] <= upper && a[0]>= lower) {
result[0]++;
result[result[0]]= a[0];
}
if(N==0) {
return result[0];
} else {
result[0] = within(&a[1], N-1, lower, upper, result);
return result[0];
}
}
now result conatins
{number in range, first number in range, second number in range....}
Something like this. If you want to implement a recursive function, try to do it in the way that the recursive call happens at the end.
#include <stdio.h>
int find_in_range(int* out, int const *in, int length, int from, int to)
{
if (length == 0)
{
return 0;
}
int addon;
if (*in >= from && *in <= to)
{
*out = *in;
++out;
addon = 1;
}
else
{
addon = 0;
}
return find_in_range(out, in + 1, length - 1, from, to) + addon;
}
#define N 6
int main()
{
int in[N] = {4, 1, 3, 1, 3, 2};
int out[N] = {0};
int num_found = find_in_range(out, in, N, 2, 3);
for (int i = 0; i < num_found; ++i)
{
printf("%d ", out[i]);
}
printf("\n");
return 0;
}
You can modify the following code as per your requirements. This is just a proof of concept code:
#include <stdio.h>
#include <stdlib.h>
static int result[4];
static int ctr1 = 0;
static int ctr2 = 0;
void recFind(int* arr, int* key){
if(ctr2 == 8)
return;
if(*arr >= key[0] && *arr <= key[1])
result[ctr1++] = *arr;
arr++;
ctr2++;
recFind(arr, key);
}
int main(){
int arr[] = {1,3,3,6,4,6,7,8};
int key[] = {1,4};
recFind(arr, key);
printf(" { ");
for(int i = 0; i < 4; i++){
printf("%d ", result[i]);
}
printf("}\n");
}
As it follows from the description of the assignment the function should provide two values: the number of elements that satisfy the condition and an array that contains the elements themselves.
It is evident that the array should be allocated dynamically. And it is logically consistent when the function itself returns the number of elements while the pointer to the generated array is passed by reference as an argument.
The recursive function can look the following way
#include <stdio.h>
#include <stdlib.h>
size_t get_range( const int a[], size_t n, int lower, int upper, int **out )
{
size_t m;
if ( n )
{
m = get_range( a, n - 1, lower, upper, out );
if ( lower <= a[n-1] && a[n-1] <= upper )
{
int *tmp = realloc( *out, ( m + 1 ) * sizeof( int ) );
if ( tmp )
{
tmp[m] = a[n-1];
*out = tmp;
++m;
}
}
}
else
{
*out = NULL;
m = 0;
}
return m;
}
int main(void)
{
int a[] = { 1, 2, 3, 4, 5, 4, 3, 2, 1 };
const size_t N = sizeof( a ) / sizeof( *a );
int lower = 2, high = 3;
int *out;
size_t n = get_range( a, N, lower, high, &out );
for ( size_t i = 0; i < n; i++ )
{
printf( "%d ", out[i] );
}
putchar( '\n' );
free( out );
return 0;
}
The program output is
2 3 3 2
Below codes will work for you in recursive way. If you don't want to print the numbers just comment out printf statement inside function printfRange. Hope you can understand the logic :-
int within(int *a, int rngH, int rngL, int length)
{
int len = length;
static int i = 0;
static int found = 0;
if(len <=0 )
{
return i;
}
if (*a == rngH)
{
printf("%d,",*a);
i++;
found = 1;
within(++a,rngH, rngL,--len);
}
else if(*a == rngL && found > 0)
{
printf("%d,",*a);
i++;
within(++a,rngH, rngL,--len);
}
else
{
within(++a,rngH, rngL,--len);
}
return i;
}
int main() {
int a[] = {4, 1, 3, 1, 3, 2};
int total = within(a,3,2,6);
printf("\n");
printf("Total :%d\n",total);
return 0;
}

How to free(a)?

I am doing leetocde. First question is Two Sum: https://leetcode.com/problems/two-sum/description/
I submit solution with c code:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
int *a = (int*)malloc(2*sizeof(int));
for(int i = 0;i<numsSize;i++){
for(int j = i+1;(j<numsSize && j != i);j++){
if(nums[i] + nums[j] == target){
a[0] = i;
a[1] = j;
return a;
}
}
}
return NULL;
}
My soluution be accepted.
The complete c code is:
/* my solution to Two Sum */
#include <stdio.h>
#include <stdlib.h>
#define TARGET 9
int* twoSum(int* nums, int numsSize, int target) {
int *a = (int*)malloc(2*sizeof(int));
for(int i = 0;i<numsSize;i++){
for(int j = i+1;(j<numsSize && j != i);j++){
if(nums[i] + nums[j] == target){
a[0] = i;
a[1] = j;
return a;
}
}
}
return NULL;
}
int main()
{
int n,target = TARGET;
int a[]={2, 7, 11, 15};
// int a[]={2,25,10,7,11,15,21,60}; // test data
int *result = NULL;
n = sizeof(a) / sizeof(a[0]);
result = twoSum(a,n,target);
if(result != NULL)
printf("%d %d\n",result[0],result[1]);
else
printf("No elements that satisfy the condition\n");
return 0;
}
/*
* output:
* 0 1
*/
I run command:
gcc two-sum.c -o two-sum
./two-sum
It outputs : 0 1
But I find my code have a bug that in twoSum function "a" is not be free.
How to solve it?
The complement only makes sense when you specify how long your data type is. If it's 4 bits, then for 0xf it's 0x0. If it's 8 bits, then for 0xf it's 0xf0.
Your representation of -6 is going to be either 0xfa, 0xfffa, 0xfffffffa, or longer, depending on how many bytes long your value is.

palindrome checker algorithm

i'm having problems writing this excercise.
this should evaluate if a given array contains a palindrome sequence of numbers, the program builds correctly but doesn't run (console remains black). where am i wrong on this? thanks for all help!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 15
//i'm using const int as exercise demand for it
//should i declare size as int when giving it to function? also if it's been declared?
//i'm a bit confused about that
int palindrome(const int a[], int p, int size);
int main()
{
int a[SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0};
int p = 1; //i'm not using boolean values, but i think should work anyway, right?
p = palindrome(a, p, SIZE);
if (p)
printf("\nseries is palindrome\n");
else
printf("\nseries isn't palindrome\n");
return 0;
}
int palindrome(const int a[], int p, int size)
{
int mid, j;
mid = size / 2;
while (p) {
for (j = 0; j < (SIZE / 2); j++){
if (a[mid + (j + 1)] != a[mid - (j + 1)]) //i think i might be wrong on this, but don't know where i'm in fault
p = 0;
}
}
return p;
}
p.s.
how can i activate debugger "watches" on Code Blocks to look at others function variables? (i put a stop on main function)
You don't need while (p) { loop. It is possible to have infinite loop here (and you have it!), because if you don't change p, this loop never stops.
You mix size and SIZE in the implementation of palindrome() (mid is half of size, but the whole loop is from 0 to SIZE-1).
Also it is better to move int p = 1; in the beginning of implementation of palindrome() (and to remove int p from list of it's parameters).
Just try this:
int palindrome(const int a[], int p, int size)
{
int mid, j;
mid = size / 2;
for (j = 0; j < (size / 2); j++){
if (a[mid + (j + 1)] != a[mid - (j + 1)]);
p = 0;
break;
}
}
return p;
}
here's an alternative without p where palindrome returns 0 or 1
int palindrome(const int a[], int size)
{
int j , k , ret;
for (j = 0 , k = size - 1 ; j < k; j++ , k--)
{
if (a[j)] != a[k])
{
ret = 0;
break;
}
}
if(j >= k)
ret = 1;
return ret;
}
you can call palindrome in the if statement in main like this :
if(palindrome(a , SIZE))
printf("\nseries is palindrome\n");
else
printf("\nseries isn't palindrome\n");

Merge sort of characters in string C

I have this working merge-sort algorithm in C. But it works only for integers. When I tried to change int to char, i'm getting segfault.
Can you please help me, what should I change in this code, so I could use MergeSort like this:
char*str = "test_string";
MergeSort(str, 0, strlen(str)-1);
void Merge(int *array, int left, int mid, int right){
int tempArray[right-left+1];
int pos=0,lpos = left,rpos = mid + 1;
while(lpos <= mid && rpos <= right){
if(array[lpos] <= array[rpos]){
tempArray[pos++] = array[lpos++];
}
else{
tempArray[pos++] = array[rpos++];
}
}
while(lpos <= mid) tempArray[pos++] = array[lpos++];
while(rpos <= right)tempArray[pos++] = array[rpos++];
int iter;
for(iter = 0;iter < pos; iter++){
array[iter+left] = tempArray[iter];
}
return;
}
void MergeSort(int *array, int left, int right){
int mid = (left+right)/2;
if(left<right){
MergeSort(array,left,mid);
MergeSort(array,mid+1,right);
Merge(array,left,mid,right);
}
return;
}
I'm lost. Thanks!
Change your declaration of array from int * to char * in both functions. Make tempArray a char[] instead of an int[]. You are trying to read memory that is 4x (or 8x) out of bounds at the end of the array, hence the seg-fault. Put another way, char is 1 byte (usually) while int is 4 or 8, so you are looking at items of a different size stacked next to each other. Also, do not pass in a const * for your string. Declaring a string as char*str = "test_string"; implies read-only memory on some systems. Use char str[] = "test_string"; instead. If you are not using strictly C, you can use C++ templates to make a function that works for int and char: http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1
#include <stdio.h>
#include<ctype.h>
#include<string.h>
int Run_count=-1;
int main ( int argc , char *argv[] )
{
/* if you dont want to use argv, put the elements in A yourself,
size being the number of string*/
/*L --> left side, R --> right side*/
int i = 0;
int size = argc-1;
char *A[argc-1];
for(i=1;i<=argc;i++){*(A+i-1) = argv[i];}
Caller(A,size);
for(i=0;i<size;i++){
printf("%s\n", A[i]);
}
printf("%d",Run_count);
}
int Caller(char* A[] , int n){
Run_count++;
int sizeL, sizeR ,i;
char *L[n/2+1] , *R[n-n/2+1];
if (n < 2){return 1;}
sizeL = n/2;
sizeR = n - sizeL;
for(i=0;i<sizeL;i++) {L[i] = *(A+i);}
for(i=0;i<n - n/2;i++) {R[i] = *(A+i+n/2);}
Caller( L, sizeL);
Caller( R, sizeR);
merger( L,sizeL, R,sizeR, A);
}
void merger(char* L[], int lengthL , char* R[] , int lengthR , char *A[]){
int i, j, k ,t =0 ;
for(k = 0 , j = 0; k < lengthL && j < lengthR ;t++){
if(compare(*(L+k),*(R+j))){
*(A+t) = *(L+k);
k++;}
else{*(A+t) = *(R+j);j++;}
}
while(k < lengthL ){
*(A+t) = *(L+k);
k++;t++;
}
while(j < lengthR ){
*(A+t) = *(R+j);
j++;t++;}
}
int compare(char *line1 , char *line2 )
{
int i;
for(i = 0;*(line1 + i) != '\0' && *(line2 + i) != '\0' ;){
if(isdigit(*(line1+i)) && isalpha(*(line2+i))){return 0;}
else if(isdigit(*(line2+i)) && isalpha(*(line1+i))){return 1;}
else if(*(line1 + i) > *(line2 + i)){return 0;}
else if(*(line1 + i) == *(line2 + i)){i++;}
else{return 1;}
}
}

Resources