Skip to content

Instantly share code, notes, and snippets.

@inage
Created December 7, 2013 14:52
Show Gist options
  • Select an option

  • Save inage/7843524 to your computer and use it in GitHub Desktop.

Select an option

Save inage/7843524 to your computer and use it in GitHub Desktop.
最長共通部分列問題
## 最長共通部分列問題
## http://rubyfiddle.com/riddles/10ea3
s = "abcd"
t = "becd"
n = s.length
m = t.length
dp = Array.new(n+1,0).map{Array.new(m+1,0)}
n.times{|i|
m.times{|j|
if s[i] == t[j]
dp[i+1][j+1] = dp[i][j]+1
else
dp[i+1][j+1] = [dp[i][j+1],dp[i+1][j]].max
end
}
}
puts dp[n][m]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment