• <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

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

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

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


             字符串相似度算法可以使用 Levenshtein Distance算法(中文翻譯:編輯距離算法) 這算法是由俄國(guó)科學(xué)家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++實(shí)現(xiàn)如下
            #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 閱讀(20468) 評(píng)論(15)  編輯 收藏 引用 所屬分類: 算法

            評(píng)論

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

            我也在論壇上看到了。學(xué)習(xí)學(xué)習(xí)  回復(fù)  更多評(píng)論   

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

            在哪里看到的?  回復(fù)  更多評(píng)論   

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

            kind of like DP..  回復(fù)  更多評(píng)論   

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

            請(qǐng)問(wèn):
            把從字符串A轉(zhuǎn)換成字符串B,如果最少執(zhí)行次數(shù)為3,那這3次的執(zhí)行動(dòng)作要如何印出來(lái)?需把程式加在哪裡?
              回復(fù)  更多評(píng)論   

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

            如果不需要得到執(zhí)行動(dòng)作的話你這個(gè)算法空間復(fù)雜度高了(應(yīng)該是O(n))。
            如果需要的話,只要回溯就可以了。  回復(fù)  更多評(píng)論   

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

            @shinjikun
            不知道你是怎么做的 不過(guò) 回溯 速度很慢的   回復(fù)  更多評(píng)論   

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

            我也比較關(guān)心國(guó)家大事 kina 提出的問(wèn)題。
            另外,我還沒(méi)有搞清楚這個(gè)算法和KMP算法的原理和實(shí)現(xiàn)的不同支出,能指點(diǎn)一下嗎?謝謝!
            我會(huì)時(shí)常關(guān)注評(píng)論。  回復(fù)  更多評(píng)論   

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

            暈,輸入法智能聯(lián)想問(wèn)題。原本應(yīng)該是:
            我也比較關(guān)心 kina 提出的問(wèn)題。
            另外,我還沒(méi)有搞清楚這個(gè)算法和KMP算法的原理和實(shí)現(xiàn)的不同之處,能指點(diǎn)一下嗎?謝謝!
            我會(huì)時(shí)常關(guān)注評(píng)論。  回復(fù)  更多評(píng)論   

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

            @test01x
            兩個(gè)算法解決的問(wèn)題是完全不一樣的,kmp解決模式匹配的LD 是解決相似度的 問(wèn)題,  回復(fù)  更多評(píng)論   

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

            dp算法

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

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

            代碼有瑕疵。因?yàn)閙atrix[0][0]沒(méi)有初始化。  回復(fù)  更多評(píng)論   

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

            @splash
            說(shuō)明你沒(méi)看懂 matrix[0][0]就沒(méi)用到  回復(fù)  更多評(píng)論   

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

            請(qǐng)問(wèn)這個(gè)在哪個(gè)環(huán)境里通過(guò)了?

            REDMOND # 139.com謝謝  回復(fù)  更多評(píng)論   

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

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


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            国产精品青草久久久久福利99| 日本欧美久久久久免费播放网| 少妇久久久久久被弄到高潮 | 一97日本道伊人久久综合影院| 亚洲乱码日产精品a级毛片久久| 日本加勒比久久精品| 日韩人妻无码一区二区三区久久| 久久99国产精品久久久 | 久久天堂AV综合合色蜜桃网| 青青热久久综合网伊人| 亚洲欧美伊人久久综合一区二区 | 国产精品九九久久免费视频 | 久久婷婷五月综合97色一本一本 | 国产成人精品综合久久久久| 无码精品久久久天天影视| 色综合久久天天综合| 久久91精品国产91久| 色综合合久久天天综合绕视看 | 久久精品人人槡人妻人人玩AV| 亚洲国产一成久久精品国产成人综合 | 久久婷婷五月综合色奶水99啪| 国内精品久久久久影院网站 | 欧美午夜精品久久久久久浪潮| 久久96国产精品久久久| 伊人久久大香线焦AV综合影院| 久久e热在这里只有国产中文精品99 | 精品久久久久久中文字幕大豆网| 久久精品成人| 久久婷婷五月综合成人D啪| 久久成人18免费网站| 久久免费视频观看| 日产精品久久久久久久性色| av色综合久久天堂av色综合在| 亚洲成av人片不卡无码久久| 岛国搬运www久久| 国产A级毛片久久久精品毛片| 久久这里只精品国产99热| 国产午夜精品理论片久久影视| 77777亚洲午夜久久多喷| 91精品国产高清久久久久久国产嫩草 | 97久久久精品综合88久久|