• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            阿牛CPP

            “中國就有這么一群奇怪的人, 本身是最底階層, 利益每天都在被損害,卻具有統治階級的意識. 在動物世界里找這么弱智的東西都幾乎不可能?!?——林語堂

            字符串相似度算法( Levenshtein Distance算法)

            昨天論壇看到的,簡單寫了一下
            題目: 一個字符串可以通過增加一個字符,刪除一個字符,替換一個字符得到另外一個字符串,假設,我們把從字符串A轉換成字符串B,前面3種操作所執行的最少次數稱為AB相似度
            如  abc adc  度為 1
                  ababababa babababab 度為 2
                  abcd acdb 度為2


             字符串相似度算法可以使用 Levenshtein Distance算法(中文翻譯:編輯距離算法) 這算法是由俄國科學家Levenshtein提出的。其步驟

            Step Description
            1 Set n to be the length of s.
            Set m to be the length of t.
            If n = 0, return m and exit.
            If m = 0, return n and exit.
            Construct a matrix containing 0..m rows and 0..n columns.
            2 Initialize the first row to 0..n.
            Initialize the first column to 0..m.
            3 Examine each character of s (i from 1 to n).
            4 Examine each character of t (j from 1 to m).
            5 If s[i] equals t[j], the cost is 0.
            If s[i] doesn't equal t[j], the cost is 1.
            6 Set cell d[i,j] of the matrix equal to the minimum of:
            a. The cell immediately above plus 1: d[i-1,j] + 1.
            b. The cell immediately to the left plus 1: d[i,j-1] + 1.
            c. The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.
            7 After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].

            C++實現如下
            #include <iostream>
            #include 
            <vector>
            #include 
            <string>
            using namespace std;

            //算法
            int ldistance(const string source,const string target)
            {
                
            //step 1

                
            int n=source.length();
                
            int m=target.length();
                
            if (m==0return n;
                
            if (n==0return m;
                
            //Construct a matrix
                typedef vector< vector<int> >  Tmatrix;
                Tmatrix matrix(n
            +1);
                
            for(int i=0; i<=n; i++)  matrix[i].resize(m+1);

                
            //step 2 Initialize

                
            for(int i=1;i<=n;i++) matrix[i][0]=i;
                
            for(int i=1;i<=m;i++) matrix[0][i]=i;

                 
            //step 3
                 for(int i=1;i<=n;i++)
                 
            {
                    
            const char si=source[i-1];
                    
            //step 4
                    for(int j=1;j<=m;j++)
                    
            {

                        
            const char dj=target[j-1];
                        
            //step 5
                        int cost;
                        
            if(si==dj){
                            cost
            =0;
                        }

                        
            else{
                            cost
            =1;
                        }

                        
            //step 6
                        const int above=matrix[i-1][j]+1;
                        
            const int left=matrix[i][j-1]+1;
                        
            const int diag=matrix[i-1][j-1]+cost;
                        matrix[i][j]
            =min(above,min(left,diag));

                    }

                 }
            //step7
                  return matrix[n][m];
            }

            int main(){
                
            string s;
                
            string d;
                cout
            <<"source=";
                cin
            >>s;
                cout
            <<"diag=";
                cin
            >>d;
                
            int dist=ldistance(s,d);
                cout
            <<"dist="<<dist<<endl;
            }

            #include 
            <iostream>
            #include 
            <vector>
            #include 
            <string>
            using namespace std;

            //算法
            int ldistance(const string source,const string target)
            {
                
            //step 1

                
            int n=source.length();
                
            int m=target.length();
                
            if (m==0return n;
                
            if (n==0return m;
                
            //Construct a matrix
                typedef vector< vector<int> >  Tmatrix;
                Tmatrix matrix(n
            +1);
                
            for(int i=0; i<=n; i++)  matrix[i].resize(m+1);

                
            //step 2 Initialize

                
            for(int i=1;i<=n;i++) matrix[i][0]=i;
                
            for(int i=1;i<=m;i++) matrix[0][i]=i;

                 
            //step 3
                 for(int i=1;i<=n;i++)
                 
            {
                    
            const char si=source[i-1];
                    
            //step 4
                    for(int j=1;j<=m;j++)
                    
            {

                        
            const char dj=target[j-1];
                        
            //step 5
                        int cost;
                        
            if(si==dj){
                            cost
            =0;
                        }

                        
            else{
                            cost
            =1;
                        }

                        
            //step 6
                        const int above=matrix[i-1][j]+1;
                        
            const int left=matrix[i][j-1]+1;
                        
            const int diag=matrix[i-1][j-1]+cost;
                        matrix[i][j]
            =min(above,min(left,diag));

                    }

                 }
            //step7
                  return matrix[n][m];
            }

            int main(){
                
            string s;
                
            string d;
                cout
            <<"source=";
                cin
            >>s;
                cout
            <<"diag=";
                cin
            >>d;
                
            int dist=ldistance(s,d);
                cout
            <<"dist="<<dist<<endl;
            }

            posted on 2008-09-21 03:03 whn 閱讀(20416) 評論(15)  編輯 收藏 引用 所屬分類: 算法

            評論

            # re: 字符串相似度算法( Levenshtein Distance算法) 2008-09-21 10:13 fejay

            我也在論壇上看到了。學習學習  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2008-09-21 22:35 Post

            在哪里看到的?  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2008-09-22 16:30 Vinson

            kind of like DP..  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2008-10-05 13:20 kina

            請問:
            把從字符串A轉換成字符串B,如果最少執行次數為3,那這3次的執行動作要如何印出來?需把程式加在哪裡?
              回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-13 12:39 shinjikun

            如果不需要得到執行動作的話你這個算法空間復雜度高了(應該是O(n))。
            如果需要的話,只要回溯就可以了。  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-17 12:36 whn

            @shinjikun
            不知道你是怎么做的 不過 回溯 速度很慢的   回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-25 01:06 test01x

            我也比較關心國家大事 kina 提出的問題。
            另外,我還沒有搞清楚這個算法和KMP算法的原理和實現的不同支出,能指點一下嗎?謝謝!
            我會時常關注評論。  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-25 01:07 test01x

            暈,輸入法智能聯想問題。原本應該是:
            我也比較關心 kina 提出的問題。
            另外,我還沒有搞清楚這個算法和KMP算法的原理和實現的不同之處,能指點一下嗎?謝謝!
            我會時常關注評論。  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-26 10:19 whncpp

            @test01x
            兩個算法解決的問題是完全不一樣的,kmp解決模式匹配的LD 是解決相似度的 問題,  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2009-05-30 13:50 ray040123

            dp算法

            如果想打印,用一個數組c[i][j] 記錄每步的決策(above,left,diag)
            之后從c[n,m] 倒推,記錄,輸出即可  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2011-06-16 15:46 splash

            代碼有瑕疵。因為matrix[0][0]沒有初始化。  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2012-04-24 11:43 peter4431

            @splash
            說明你沒看懂 matrix[0][0]就沒用到  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法) 2012-06-14 08:48 safds

            請問這個在哪個環境里通過了?

            REDMOND # 139.com謝謝  回復  更多評論   

            # re: 字符串相似度算法( Levenshtein Distance算法)[未登錄] 2016-01-07 11:50 max

            @peter4431
            我只是路過,感謝樓主的分享,但是這個地方確實是用到啦,像掃雷一樣,周邊都是度,可能確實要初始化一下。  回復  更多評論   

            武侠古典久久婷婷狼人伊人| 久久香蕉国产线看观看猫咪?v| 久久天天躁狠狠躁夜夜躁2014| 久久国产劲爆AV内射—百度| 亚洲精品高清国产一线久久| 精品无码久久久久久午夜| 四虎国产精品免费久久5151 | 合区精品久久久中文字幕一区 | 国产精品丝袜久久久久久不卡| 日本精品久久久久影院日本| 色欲av伊人久久大香线蕉影院| 久久99亚洲网美利坚合众国| 久久国产高清一区二区三区| 成人午夜精品无码区久久| 久久www免费人成看国产片| 久久久久久久久久久久中文字幕 | 亚洲日本久久久午夜精品| 久久免费精品视频| 国产成人精品综合久久久久| 国产精品一区二区久久精品无码| 综合久久国产九一剧情麻豆 | 亚洲国产成人久久精品影视| 亚洲精品无码久久一线| 久久人人青草97香蕉| 久久国产午夜精品一区二区三区| 狠狠狠色丁香婷婷综合久久五月| 亚洲国产精品久久久天堂| 偷偷做久久久久网站| 无码人妻久久一区二区三区蜜桃 | 无码精品久久久天天影视| 久久国产色av免费看| 久久精品国产亚洲AV不卡| 亚洲精品乱码久久久久久不卡| 久久国产精品偷99| 狠狠人妻久久久久久综合| 99久久国产综合精品五月天喷水| 狠狠色丁香婷综合久久| 青青国产成人久久91网| 国产精品永久久久久久久久久| 色综合色天天久久婷婷基地| 亚洲欧美日韩精品久久|