• <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記錄開發新知道

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

            image

            晚上在跟同事聊到字符串匹配的時候,同事提到了矩陣,覺著是個很牛的東西,它的結果如果出現連續的斜線的話,則說明有匹配了,這可以用于做快速搜索,而且也可以搜索最大公共字符串。據說有個很牛的算法專門用來做“最大公共字符串”的,但我還沒去找,先將這個矩陣輸出試試。

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

            通過矩陣,我們就很容易得出最大匹配結果。至于如何分析,暫時還沒有做考慮,有空會進行分析。

            // max_match.cpp : 定義控制臺應用程序的入口點。
            //
            
            #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 閱讀(818) 評論(1)  編輯 收藏 引用

            評論

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

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

            特殊功能
             
            亚洲精品综合久久| 亚洲综合精品香蕉久久网97| 久久久这里只有精品加勒比| 国色天香久久久久久久小说| AV狠狠色丁香婷婷综合久久| A狠狠久久蜜臀婷色中文网| 国产精品亚洲综合专区片高清久久久| 亚洲国产成人久久综合野外| 无码人妻久久一区二区三区免费丨 | 人妻精品久久久久中文字幕69 | 无码AV波多野结衣久久| 精品久久久久久无码国产 | 丁香久久婷婷国产午夜视频| 区久久AAA片69亚洲| 精品久久久久久无码免费| 久久亚洲AV成人无码电影| 亚洲国产精品一区二区三区久久| 精品熟女少妇a∨免费久久| 中文字幕精品久久久久人妻| 国产精品久久久久久久久| 亚洲狠狠婷婷综合久久蜜芽| 亚洲欧美日韩精品久久亚洲区| 97久久香蕉国产线看观看| 亚洲国产小视频精品久久久三级| 久久免费视频网站| 成人免费网站久久久| 久久综合精品国产二区无码| 久久久久久国产精品免费免费| 亚洲va久久久久| 久久久久亚洲AV综合波多野结衣| 久久99精品国产麻豆不卡| 久久香蕉综合色一综合色88| 久久久久人妻一区精品性色av| 久久婷婷五月综合97色直播| 理论片午午伦夜理片久久 | 久久丝袜精品中文字幕| 国产精品99久久久久久宅男| 国产精品久久99| 色综合久久天天综合| 久久无码AV中文出轨人妻| 久久人人爽人人爽人人片AV麻豆|