{"id":1687,"date":"2011-04-13T01:00:49","date_gmt":"2011-04-13T01:00:49","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=1687"},"modified":"2011-04-13T01:00:49","modified_gmt":"2011-04-13T01:00:49","slug":"%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%bc%bc%e5%ba%a6%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2011\/04\/13\/%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%9b%b8%e4%bc%bc%e5%ba%a6%e7%ae%97%e6%b3%95\/","title":{"rendered":"\u5b57\u7b26\u4e32\u76f8\u4f3c\u5ea6\u7b97\u6cd5"},"content":{"rendered":"<pre escaped=\"true\" lang=\"c#\">\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\n\r\nnamespace ConsoleApplication38\r\n{\r\n    public class LevenshteinDistance\r\n    {\r\n\r\n        private static LevenshteinDistance _instance=null;\r\n        public static LevenshteinDistance Instance\r\n        {\r\n            get\r\n            {\r\n                if (_instance == null)\r\n                {\r\n                    return new LevenshteinDistance();\r\n                }\r\n                return _instance;\r\n            }\r\n        }\r\n    \r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u53d6\u6700\u5c0f\u7684\u4e00\u4f4d\u6570\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"first\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"second\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"third\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public int LowerOfThree(int first, int second, int third)\r\n        {\r\n            int min = first;\r\n            if (second &lt; min)\r\n                min = second;\r\n            if (third &lt; min)\r\n                min = third;\r\n            return min;\r\n        }\r\n\r\n        public int Levenshtein_Distance(string str1, string str2)\r\n        {\r\n            int[,] Matrix;\r\n            int n=str1.Length;\r\n            int m=str2.Length;\r\n\r\n            int temp = 0;\r\n            char ch1;\r\n            char ch2;\r\n            int i = 0;\r\n            int j = 0;\r\n            if (n ==0)\r\n            {\r\n                return m;\r\n            }\r\n            if (m == 0)\r\n            {\r\n\r\n                return n;\r\n            }\r\n            Matrix=new int[n+1,m+1];\r\n\r\n            for (i = 0; i &lt;= n; i++)\r\n            {\r\n                \/\/\u521d\u59cb\u5316\u7b2c\u4e00\u5217\r\n                Matrix[i,0] = i;\r\n            }\r\n\r\n            for (j = 0; j &lt;= m; j++)\r\n            {\r\n                \/\/\u521d\u59cb\u5316\u7b2c\u4e00\u884c\r\n                Matrix[0, j] = j;\r\n            }\r\n\r\n            for (i = 1; i &lt;= n; i++)\r\n            {\r\n                ch1 = str1[i-1];\r\n                for (j = 1; j &lt;= m; j++)\r\n                {\r\n                    ch2 = str2[j-1];\r\n                    if (ch1.Equals(ch2))\r\n                    {\r\n                        temp = 0;\r\n                    }\r\n                    else\r\n                    {\r\n                        temp = 1;\r\n                    }\r\n                    Matrix[i,j] = LowerOfThree(Matrix[i - 1,j] + 1, Matrix[i,j - 1] + 1, Matrix[i - 1,j - 1] + temp);\r\n\r\n\r\n                }\r\n            }\r\n\r\n            for (i = 0; i &lt;= n; i++)\r\n            {\r\n                for (j = 0; j &lt;= m; j++)\r\n                {\r\n                    Console.Write(\" {0} \", Matrix[i, j]);\r\n                }\r\n                Console.WriteLine(\"\");\r\n            }\r\n            return Matrix[n, m];\r\n\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u8ba1\u7b97\u5b57\u7b26\u4e32\u76f8\u4f3c\u5ea6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"str1\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;param name=\"str2\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public decimal LevenshteinDistancePercent(string str1,string str2)\r\n        {\r\n            int maxLenth = str1.Length &gt; str2.Length ? str1.Length : str2.Length;\r\n            int val = Levenshtein_Distance(str1, str2);\r\n            return 1 - (decimal)val \/ maxLenth;\r\n        }\r\n    }\r\n\r\n    class Program\r\n    {\r\n\r\n\r\n        static void Main(string[] args)\r\n        {\r\n            string str1 = \"\u4f60\u597d\u8482\u8482\";\r\n            string str2=\"\u4f60\u597d\u8482\u82ac\";\r\n            Console.WriteLine(\"\u5b57\u7b26\u4e321 {0}\", str1);\r\n\r\n            Console.WriteLine(\"\u5b57\u7b26\u4e322 {0}\", str2);\r\n\r\n            Console.WriteLine(\"\u76f8\u4f3c\u5ea6 {0} %\", LevenshteinDistance.Instance.LevenshteinDistancePercent(str1, str2)*100);\r\n            Console.ReadLine();\r\n        }\r\n    }\r\n\r\n\r\n\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Collections.Generic; using S [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-1687","post","type-post","status-publish","format-standard","hentry","category-14"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=1687"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/1687\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=1687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=1687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=1687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}