Skip to content

Instantly share code, notes, and snippets.

@yetimdasturchi
Created March 25, 2024 05:20
Show Gist options
  • Save yetimdasturchi/991a686a5e9391652a37d40868e8904a to your computer and use it in GitHub Desktop.
Save yetimdasturchi/991a686a5e9391652a37d40868e8904a to your computer and use it in GitHub Desktop.
Programming Classics: Implementing the World's Best Algorithms by Oliver. Smilar text implementation.
#include <stdio.h>
#include <string.h>
void similar_str( const char* txt1, int len1, const char* txt2, int len2, int* pos1, int* pos2, int* max, int* count ) {
*max = 0;
*count = 0;
for ( int i = 0; i < len1; i++ ) {
for ( int j = 0; j < len2; j++ ) {
int l = 0;
while ( ( i + l < len1 ) && ( j + l < len2 ) && ( txt1[ i + l ] == txt2[ j + l ] ) ) {
l++;
}
if ( l > *max ) {
*max = l;
*count += 1;
*pos1 = i;
*pos2 = j;
}
}
}
}
int similar_char( const char* txt1, int len1, const char* txt2, int len2 ) {
int sum = 0;
int pos1 = 0;
int pos2 = 0;
int max = 0;
int count = 0;
similar_str( txt1, len1, txt2, len2, &pos1, &pos2, &max, &count );
sum = max;
if ( pos1 && pos2 && count > 1 ) {
char sub1[pos1 + 1];
strncpy( sub1, txt1, pos1 );
sub1[pos1] = '\0';
char sub2[pos2 + 1];
strncpy( sub2, txt2, pos2 );
sub2[pos2] = '\0';
sum += similar_char( sub1, pos1, sub2, pos2 );
}
if ( ( pos1 + max < len1) && ( pos2 + max < len2 ) ) {
char sub1[len1 - pos1 - max + 1];
strncpy(sub1, txt1 + pos1 + max, len1 - pos1 - max);
sub1[len1 - pos1 - max] = '\0';
char sub2[len2 - pos2 - max + 1];
strncpy(sub2, txt2 + pos2 + max, len2 - pos2 - max);
sub2[len2 - pos2 - max] = '\0';
sum += similar_char( sub1, len1 - pos1 - max, sub2, len2 - pos2 - max );
}
return sum;
}
int main() {
const char* txt1 = "hello";
const char* txt2 = "hallo";
printf( "%d\n", similar_char( txt1, strlen(txt1), txt2, strlen(txt2) ) );
return 0;
}
<?php
function similar_str(string $txt1, int $len1, string $txt2, int $len2, &$pos1, &$pos2, &$max, &$count) {
$max = 0;
$count = 0;
for ( $i = 0; $i < $len1; $i++ ) {
for ( $j = 0; $j < $len2; $j++ ) {
$l = 0;
while ( ( $i + $l < $len1 ) && ( $j + $l < $len2 ) && ( $txt1[ $i + $l ] === $txt2[ $j + $l ] ) ) {
$l++;
}
if ( $l > $max ) {
$max = $l;
$count += 1;
$pos1 = $i;
$pos2 = $j;
}
}
}
}
function similar_char( string $txt1, int $len1, string $txt2, int $len2 ): int {
$sum = 0;
$pos1 = 0;
$pos2 = 0;
$max = 0;
$count = 0;
similar_str( $txt1, $len1, $txt2, $len2, $pos1, $pos2, $max, $count );
if ( $sum = $max ) {
if ( $pos1 && $pos2 && $count > 1 ) {
$sum += similar_char(
substr($txt1, 0, $pos1),
$pos1,
substr($txt2, 0, $pos2),
$pos2
);
}
if ( ( $pos1 + $max < $len1 ) && ( $pos2 + $max < $len2 ) ) {
$sum += similar_char(
substr( $txt1, $pos1 + $max ),
$len1 - $pos1 - $max,
substr( $txt2, $pos2 + $max ), $len2 - $pos2 - $max
);
}
}
return $sum;
}
$txt1 = "hello";
$txt2 = "hallo";
echo similar_char( $txt1, strlen( $txt1 ), $txt2, strlen( $txt2 ) );
def similar_str(txt1, len1, txt2, len2):
max_length = 0
count = 0
pos1 = pos2 = 0
for i in range(len1):
for j in range(len2):
l = 0
while (i + l < len1) and (j + l < len2) and (txt1[i + l] == txt2[j + l]):
l += 1
if l > max_length:
max_length = l
count += 1
pos1, pos2 = i, j
return pos1, pos2, max_length, count
def similar_char(txt1, len1, txt2, len2):
sum_ = 0
pos1, pos2, max_length, count = similar_str(txt1, len1, txt2, len2)
if max_length > 0:
sum_ = max_length
if pos1 and pos2 and count > 1:
sum_ += similar_char(txt1[:pos1], pos1, txt2[:pos2], pos2)
if (pos1 + max_length < len1) and (pos2 + max_length < len2):
sum_ += similar_char(txt1[pos1 + max_length:], len1 - pos1 - max_length, txt2[pos2 + max_length:], len2 - pos2 - max_length)
return sum_
txt1 = "hello"
txt2 = "hallo"
print(similar_char(txt1, len(txt1), txt2, len(txt2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment