Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 合并两个字符串以删除重复的子字符串_C - Fatal编程技术网

C 合并两个字符串以删除重复的子字符串

C 合并两个字符串以删除重复的子字符串,c,C,我有两个字符串,我想合并,删除重复的子字符串。请注意,每两个连续数字构成一个子串。考虑字符串STR1和STR2: str1=“#1#1.2.3#1#6.7.8” str2=“#1#6.7.8#1#5.6” 我希望生成一个组合字符串,如下所示: comboStr=“#1#1.2.3#1#6.7.8#1#5.6”(即,我删除了重复的#1#6.7.8) 我已经编写了一个小函数来实现这一点: char *combine (char *nodehashkey ,char *ngbrhashkey) {

我有两个字符串,我想合并,删除重复的子字符串。请注意,每两个连续数字构成一个子串。考虑字符串STR1和STR2:

str1=“#1#1.2.3#1#6.7.8” str2=“#1#6.7.8#1#5.6”

我希望生成一个组合字符串,如下所示:

comboStr=“#1#1.2.3#1#6.7.8#1#5.6”(即,我删除了重复的#1#6.7.8)

我已经编写了一个小函数来实现这一点:

char *combine (char *nodehashkey ,char *ngbrhashkey)
{
        char *suffix, *combo_hashkey;
        char prefix[5], token[15];
        short qid;

    short len = strlen(nodehashkey);

    combo_hashkey = (char*) malloc(sizeof(char) * (len+1));
    strcpy(combo_hashkey, nodehashkey);

    short offset = len;

    sscanf(nodehashkey, "#%hd#", &qid);

    sprintf(prefix, "#%hd#", qid);
    printf("prefix: %s\n", prefix);

    suffix = strtok(ngbrhashkey, prefix);

    while (suffix != NULL)
    {
            strcpy(token, prefix);
            strcpy(token + strlen(prefix), suffix);
            int token_len = strlen(token);

            if(strstr(nodehashkey, token) == NULL)
            {
                    if(!(combo_hashkey = (char*) realloc (
                    combo_hashkey, sizeof(char) * (offset+token_len+1))))
                    printf("malloc failed!");

                    strncpy(combo_hashkey + offset, token, token_len+1);
                    offset += token_len;
                    combo_hashkey[offset] = '\0';
            }

            suffix = strtok(NULL, prefix);
    }

    return combo_hashkey;
}

为了测试它,我尝试了以下方法。虽然前两个combine调用生成了正确的组合字符串,但第三个调用没有。它不是生产1.6 1.2.4 1.3.5,而是生产1.6 1.2.4 1.3.5

}

我再次跟踪了该函数,但我无法确定额外的1.6是从哪里来的。

\include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *combine (const char *nodehashkey, const char *ngbrhashkey){
    int i, dup, len, pos_count = 0;
    const char *p1, *p2, *pos[64];
    char *combo_hashkey, *p3;

    if(!nodehashkey || !ngbrhashkey)
        return NULL;

    //store key position
    pos[0] = p1 = nodehashkey;
    while(*p1){
        sscanf(p1, "#%*d#%*[.0123456789]%n", &len);
        pos[++pos_count] = (p1 += len);
    }
    len = p1 - nodehashkey;
    p2 = ngbrhashkey;
    p3 = combo_hashkey = malloc(len + strlen(p2) + 1);
    memcpy(p3, nodehashkey, len);
    p3 += len;
    while(*p2){
        sscanf(p2, "#%*d#%*[.0123456789]%n", &len);
        for(dup=i=0;i<pos_count;++i){
            if(pos[i+1]-pos[i] == len && strncmp(pos[i], p2, len)==0){
                dup = 1;
                break;
            }
        }
        if(!dup){
            memcpy(p3, p2, len);
            p3 += len;
        }
        p2 += len;
    }
    *p3 = '\0';
    return combo_hashkey;
}

int main(){
    char *str1, *str2, *str3;
    str1 = combine("#1#.1.2.3#1#.6.7.8", "#1#.6.7.8#1#.5.6");
    printf("%s\n", str1);//#1#.1.2.3#1#.6.7.8#1#.5.6
    free(str1);

    str2 = combine("#1#.1.6", "#1#.2.4");
    str3 = combine("#1#.1.6", "#1#.3.5");
    printf("str2:%s\n", str2);
    printf("str3:%s\n", str3);

    char *weird = combine(str2, str3);

    printf("weird: %s\n", weird);//weird: #1#.1.6#1#.2.4#1#.3.5
    free(str2);free(str3);free(weird);
    return 0;
}
#包括 #包括 char*combine(常量char*nodehashkey,常量char*ngbrhashkey){ int i、dup、len、pos_计数=0; 常量字符*p1,*p2,*pos[64]; char*combo_hashkey,*p3; 如果(!nodehashkey | |!ngbrhashkey) 返回NULL; //存储键位置 pos[0]=p1=nodehashkey; 而(*p1){ sscanf(p1,“#%*d#%*[.0123456789]%n”,&len); pos[++pos_count]=(p1+=len); } len=p1-节点hashkey; p2=ngbrhashkey; p3=combo_hashkey=malloc(len+strlen(p2)+1); memcpy(p3,nodehashkey,len); p3+=len; 而(*p2){ sscanf(p2,“#%*d#%*[.0123456789]%n”,&len);
对于(dup=i=0;iSo)您想要两个多集之间的并集吗?@MadScienceDreams是的,您可以这么说。在我看来,问题是您试图按顺序检查键。多集有序的最大并集实际上不起作用,因为您看到的问题是:您不知道下一个键是否是重复的(应该删除)“正确”的做法是将每个多集转换为一组键计数对,对帐户求和,并扩展结果集,但这不会以正确的顺序返回内容(很可能会输出“#1#1.6#2.4#3.5”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *combine (const char *nodehashkey, const char *ngbrhashkey){
    int i, dup, len, pos_count = 0;
    const char *p1, *p2, *pos[64];
    char *combo_hashkey, *p3;

    if(!nodehashkey || !ngbrhashkey)
        return NULL;

    //store key position
    pos[0] = p1 = nodehashkey;
    while(*p1){
        sscanf(p1, "#%*d#%*[.0123456789]%n", &len);
        pos[++pos_count] = (p1 += len);
    }
    len = p1 - nodehashkey;
    p2 = ngbrhashkey;
    p3 = combo_hashkey = malloc(len + strlen(p2) + 1);
    memcpy(p3, nodehashkey, len);
    p3 += len;
    while(*p2){
        sscanf(p2, "#%*d#%*[.0123456789]%n", &len);
        for(dup=i=0;i<pos_count;++i){
            if(pos[i+1]-pos[i] == len && strncmp(pos[i], p2, len)==0){
                dup = 1;
                break;
            }
        }
        if(!dup){
            memcpy(p3, p2, len);
            p3 += len;
        }
        p2 += len;
    }
    *p3 = '\0';
    return combo_hashkey;
}

int main(){
    char *str1, *str2, *str3;
    str1 = combine("#1#.1.2.3#1#.6.7.8", "#1#.6.7.8#1#.5.6");
    printf("%s\n", str1);//#1#.1.2.3#1#.6.7.8#1#.5.6
    free(str1);

    str2 = combine("#1#.1.6", "#1#.2.4");
    str3 = combine("#1#.1.6", "#1#.3.5");
    printf("str2:%s\n", str2);
    printf("str3:%s\n", str3);

    char *weird = combine(str2, str3);

    printf("weird: %s\n", weird);//weird: #1#.1.6#1#.2.4#1#.3.5
    free(str2);free(str3);free(weird);
    return 0;
}