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

            MyMSDN

            MyMSDN記錄開(kāi)發(fā)新知道

            公共字符串匹配矩陣(max_match)

            image

            晚上在跟同事聊到字符串匹配的時(shí)候,同事提到了矩陣,覺(jué)著是個(gè)很牛的東西,它的結(jié)果如果出現(xiàn)連續(xù)的斜線的話,則說(shuō)明有匹配了,這可以用于做快速搜索,而且也可以搜索最大公共字符串。據(jù)說(shuō)有個(gè)很牛的算法專門用來(lái)做“最大公共字符串”的,但我還沒(méi)去找,先將這個(gè)矩陣輸出試試。

            本程序提供兩種輸出方式,以參數(shù)-w和-wo進(jìn)行區(qū)分,分別用于顯示人眼識(shí)別的信息,以及可供后續(xù)程序進(jìn)行處理的純凈信息。本程序同時(shí)提供通用的幫助查看方式,參數(shù)為-h或/?等。

            通過(guò)矩陣,我們就很容易得出最大匹配結(jié)果。至于如何分析,暫時(shí)還沒(méi)有做考慮,有空會(huì)進(jìn)行分析。

            // max_match.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
            //
            
            #include <stdlib.h>
            #include <stdio.h>
            #include <string.h>
            #define BOOL int
            #define TRUE 1
            #define FALSE 0
            typedef char element_t;
            #define ele_space ' '
            #define ele_break '\n'
            #define ele_placeholder '*'
            #define ele_end '\0'
            
            element_t *malloc_space(element_t* str1, element_t* str2, BOOL with_illuminate);
            element_t *max_match_matrix(element_t* str1, element_t* str2, BOOL with_illuminate, element_t *matrix);
            /*display the help informations*/
            void man_this(void);
            /*
            purpose:
                Generate a matrix for max match with str1 & str2.
                e.g:
                    str1 = "abcdefghijk";
                    str2 =    "define";
                The str2 with the same substring "def" in str1.
                Show the relationship between the two string by matrix.
                You can use the pipe for dealing with the result.
            invoke:
                max_match {options} str1 str2
            options:
                @-w(default): Display the result infomations for view.
                @    --with_illuminate: The same function as option -w;
                @-wo: Don't display the result infomations for view.
                @    --without_illuminate: The same function as option -wo;
                @-h: Display the help info for users.
                @    --help: The same function as option -h;
                @    /h: The same function as option -h;
                @    /help: The same function as option -h;
                @    /?: The same function as option -h;
                @    -?: The same function as option -h;
            */
            int main(int argc, char* argv[])
            {
                char *str1, *str2;
                BOOL with_illuminate = TRUE;
                if(argc == 3)    
                {
                    str1 = argv[1];
                    str2 = argv[2];
                }
                else if(argc == 4)
                {
                    if(strcmp(argv[1], "-w") == 0 || strcmp(argv[1], "--with_illuminate") == 0)
                        with_illuminate = TRUE;
                    else if(strcmp(argv[1], "-wo") == 0 || strcmp(argv[1], "--without_illuminate") == 0)
                        with_illuminate = FALSE;
                    else
                    {
                        printf("ERROR: error paramaters in!\n");
                        return -2;
                    }
            
                    str1 = argv[2];
                    str2 = argv[3];
                }
                else if(argc == 2)
                {
                    if(strcmp(argv[1], "-h") == 0
                        || strcmp(argv[1], "/h") == 0
                        || strcmp(argv[1], "--help") == 0
                        || strcmp(argv[1], "/help") == 0
                        || strcmp(argv[1], "/?") == 0
                        || strcmp(argv[1], "-?") == 0)
                    {
                        man_this();
                        return 0;
                    }
                }
                else
                {
                    printf("ERROR: No enough paramaters!\n");
                    return -1;
                }
                
                if(with_illuminate)
                {
                    printf("str1:\t|%s\n", str1);
                    printf("str2:\t|%s\n", str2);
                    printf("\nresult:\n");
                }
            
                element_t *matrix = malloc_space(str1, str2, with_illuminate);
                printf("%s", max_match_matrix(str1, str2, with_illuminate, matrix));
                free(matrix);
                return 0;
            }
            
            element_t *max_match_matrix(element_t* str1, element_t* str2, BOOL with_illuminate, element_t *matrix)
            {
                int curr = with_illuminate ? 0 : -1;
                if(with_illuminate)
                {
                    matrix[curr] = ele_space;
                    int i = -1;
                    while(str1[++i] != ele_end)
                    {
                        matrix[++curr] = str1[i];
                    }
                    matrix[++curr] = ele_break;
                }
                int j = -1;
                while(str2[++j] != ele_end)
                {
                    if(with_illuminate)    matrix[++curr] = str2[j];
                    int z = -1;
                    while(str1[++z] != ele_end)
                    {
                        if(str1[z] == str2[j])
                            matrix[++curr] = ele_placeholder;
                        else
                            matrix[++curr] = ele_space;
                    }
                    matrix[++curr] = ele_break;
                }
            
                matrix[++curr] = ele_end;
                return matrix;
            }
            
            element_t *malloc_space(element_t* str1, element_t* str2, BOOL with_illuminate)
            {
                int len1 = strlen(str1);
                int len2 = strlen(str2);
                int len = 0;
                if(with_illuminate)
                    len = (len1 + 2) * (len2 + 1) + 1;
                else
                    len = (len1 + 1) * len2 + 1;
            
                element_t* result;
                if((result = (element_t*)malloc((size_t)(len * sizeof(element_t)))) == NULL)
                {
                    printf("ERROR: No enough space!");
                    return NULL;
                }
                return result;
            }
            
            void man_this(void)
            {
                printf("%s", "purpose:\n");
                printf("%s", "    Generate a matrix for max match with str1 & str2.\n");
                printf("%s", "    e.g:\n");
                printf("%s", "        str1 = \"abcdefghijk\";\n");
                printf("%s", "        str2 =    \"define\";\n");
                printf("%s", "    The str2 with the same substring \"def\" in str1.\n");
                printf("%s", "    Show the relationship between the two string by matrix.\n");
                printf("%s", "    You can use the pipe for dealing with the result.\n");
                printf("%s", "invoke:\n");
                printf("%s", "    max_match {options} str1 str2\n");
                printf("%s", "options:\n");
                printf("%s", "    @-w(default): Display the result infomations for view.\n");
                printf("%s", "    @    --with_illuminate: The same function as option -w;\n");
                printf("%s", "    @-wo: Don't display the result infomations for view.\n");
                printf("%s", "    @    --without_illuminate: The same function as option -wo;\n");
                printf("%s", "    @-h: Display the help info for users.\n");
                printf("%s", "    @    --help: The same function as option -h;\n");
                printf("%s", "    @    /h: The same function as option -h;\n");
                printf("%s", "    @    /help: The same function as option -h;\n");
                printf("%s", "    @    /?: The same function as option -h;\n");
                printf("%s", "    @    -?: The same function as option -h;\n");
            }
            
            

            posted on 2009-08-07 00:47 volnet 閱讀(819) 評(píng)論(1)  編輯 收藏 引用

            評(píng)論

            # re: 公共字符串匹配矩陣(max_match) 2009-08-07 01:18 volnet

            孤陋寡聞了,原來(lái)這種東西滿大街都是,呵呵:
            http://blog.sina.com.cn/s/blog_455b20c10100929m.html
            http://andylin02.javaeye.com/blog/437166
            關(guān)鍵字:字符串相似度,最大公共字符串,LCS算法等  回復(fù)  更多評(píng)論   


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


            特殊功能
             
            性做久久久久久久| 国产精品美女久久久久av爽| www亚洲欲色成人久久精品| 国产精品久久久久蜜芽| 久久嫩草影院免费看夜色| 岛国搬运www久久| 精品国产综合区久久久久久| 国产精品久久久久aaaa| 亚洲色欲久久久综合网东京热| 狠狠色狠狠色综合久久| 一本久久知道综合久久| 久久亚洲日韩精品一区二区三区| 亚洲中文字幕无码一久久区| 色88久久久久高潮综合影院| 漂亮人妻被黑人久久精品| 精品久久久久久亚洲| 久久精品成人免费观看97| 久久综合一区二区无码| 久久99九九国产免费看小说| 麻豆一区二区99久久久久| 精品午夜久久福利大片| 久久青青草原精品国产软件| 久久精品人妻中文系列| 国产精品久久久久久久| 久久久精品久久久久久 | 亚洲香蕉网久久综合影视| 久久精品无码一区二区无码 | 欧美精品国产综合久久| 99精品久久精品一区二区| 久久久久人妻一区二区三区vr| 国内精品久久久久久久影视麻豆| 亚洲国产日韩欧美综合久久| 亚洲AV日韩精品久久久久久| 2020最新久久久视精品爱| 久久婷婷色香五月综合激情| 久久久噜噜噜www成人网| 久久精品亚洲精品国产欧美| 97热久久免费频精品99| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久精品午夜一区二区福利| 久久久久一级精品亚洲国产成人综合AV区|