Skip to content

Instantly share code, notes, and snippets.

@juliusgeo
Last active January 11, 2021 18:14
Show Gist options
  • Save juliusgeo/2e0b2af438f3d972c36a3bfddf52f4bc to your computer and use it in GitHub Desktop.
Save juliusgeo/2e0b2af438f3d972c36a3bfddf52f4bc to your computer and use it in GitHub Desktop.
Implementing longest palindromic substring algorithm using pthreads as practice
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
int sum;
struct input {
char* str;
int i;
};
int NUM_THREADS;
void* add(void* ptr){
struct input* input = (struct input*) ptr;
int strLen = strlen(input->str)+1;
if(input->i-1 < 0 || input->i+1 >= strLen){
//printf("Boundary %d %d %d, %s\n", input->i-i-1, input->i, input->i+i+1, input->str);
char* ret = malloc(sizeof(char)*2);
*ret = input->str[input->i];
ret[1] = '\0';
//printf("Palindromic substring: %s\n", ret);
return ret;
}
// printf("%d %d %d\n", input->i-i-1, input->i, input->i+i+1);
int i = 0;
while(input->i-i-1 >= 0 && input->i+i+1 < strLen && input->str[input->i-i-1] == input->str[input->i+i+1]){
i++;
}
int n = 0;
while(input->i-n-1 >= 0 && input->i+n < strLen && input->str[input->i-n-1] == input->str[input->i+n]){
n++;
}
if(n > i){
char* ret_val = malloc((n*2+1)*sizeof(char));
strncpy(ret_val, input->str+input->i-n, n*2);
ret_val[n*2] = '\0';
//printf("Palindromic substring: %s\n", ret_val);
return ret_val;
}
else{
char* ret_val = malloc((i*2+2)*sizeof(char));
strncpy(ret_val, input->str+input->i-i, i*2+1);
ret_val[i*2+1] = '\0';
//printf("Palindromic substring: %s\n", ret_val);
return ret_val;
}
if(i == 0){
char* ret = malloc(sizeof(char)*2);
*ret = input->str[input->i];
ret[1] = '\0';
//printf("Palindromic substring: %s\n", ret);
return ret;
}
return "";
}
int main(){
char* s = "civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth";
int strLen = strlen(s)+1;
NUM_THREADS = 32;
pthread_t* threads = malloc((strLen)*sizeof(pthread_t));
struct input* args = malloc(sizeof(struct input));
int numThreadsSoFar = 0;
void* max_val;
int max_len = 0;
void* cur_val;
while(numThreadsSoFar < strLen-strLen%NUM_THREADS){
for(int i=numThreadsSoFar; i<numThreadsSoFar+NUM_THREADS; i++){
args = malloc(sizeof(struct input));
args->str = s;
args->i = i;
pthread_create(&threads[i], NULL, add, args);
}
for(int i=numThreadsSoFar; i<numThreadsSoFar+NUM_THREADS; i++){
pthread_join(threads[i], &cur_val);
if(strlen(cur_val) > max_len){
max_len = strlen(cur_val);
max_val = cur_val;
}
}
numThreadsSoFar += NUM_THREADS;
}
for(int i=numThreadsSoFar; i<strLen; i++){
args = malloc(sizeof(struct input));
args->str = s;
args->i = i;
pthread_create(&threads[i], NULL, add, args);
}
for(int i=numThreadsSoFar; i<strLen; i++){
pthread_join(threads[i], &cur_val);
if(strlen(cur_val) > max_len){
max_len = strlen(cur_val);
max_val = cur_val;
}
}
printf("Longest palindromic substring: %s\n", (void*)max_val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment