且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算方格的汉明距离和重量

更新时间:2023-02-27 10:46:23

SQLite没有内置函数可以直接帮助解决这个问题.

SQLite has no built-in functions that could directly help with this.

在SQLite 3.8.3或更高版本中,您可以使用递归公用表表达式进行计算手动进行比赛:

In SQLite 3.8.3 or later, you could use a recursive common table expression to compute the matches by hand:

CREATE TABLE t(x);
INSERT INTO t VALUES ('1011'), ('1000'), ('1100'), ('0011');

WITH compare(matches, rest, pattern, original) AS (
  SELECT 0, x, '1011', x FROM t
  UNION ALL
  SELECT matches + (substr(rest, 1, 1) = '1' AND substr(pattern, 1, 1) = '1'),
         substr(rest, 2),
         substr(pattern, 2),
         original
  FROM compare
  WHERE rest != '')
SELECT matches, original
FROM compare
WHERE rest = ''
ORDER BY matches DESC;

3|1011
2|0011
1|1000
1|1100