What is a shell command to find the longest common

2019-04-28 07:35发布

What is a shell command to find the longest common substring of two strings in unix? like: foo 'abcdefghi' 'abjklmdefnop' prints: def

2条回答
姐就是有狂的资本
2楼-- · 2019-04-28 07:43

This is known as the longest common subsequence problem and there are some great algorithms for it. Check out the dynamic programming solution (If you google it, you'll find a ton of implementations). If you really want to understand this at an algorithmic level, check out this MIT lecture,

http://videolectures.net/mit6046jf05_leiserson_lec15/

查看更多
何必那么认真
3楼-- · 2019-04-28 07:51

I am not sure if there is a single command that does the job for you but the following bash script should do it.

#!/bin/bash

word1="$1"
word2="$2"
if [ ${#word1} -lt ${#word2} ]
then
        word1="$2"
        word2="$1"
fi
for ((i=${#word2}; i>0; i--)); do
        for ((j=0; j<=${#word2}-i; j++)); do
                if [[ $word1 =~ ${word2:j:i} ]]
                then
                        echo ${word2:j:i}
                        exit
                fi
        done
done

save the above as a file substr.sh do chmod +x substr.sh

pranithk @ ~
09:24:32 :) $ ./substr.sh 'abcdefghi' 'abcdeghi'
abcde

pranithk @ ~
09:24:33 :) $ ./substr.sh 'abcdefghi' 'abjklmdefnop'
def
查看更多
登录 后发表回答