0

私の課題は次のとおりです。

文字列マッチングでは、ある文字列 (パターン) の別の文字列 (テキスト) のインスタンスを探します。障害 (不一致) の数が最も少ないインスタンスを探しています。

たとえば、パターンが「camb」でテキストが「caammbd」の場合、「positions」を比較します。 1. 「camb」と「caam」を比較すると、2 つの不一致 (m と a、b と m) が示されます。2. "camb" と "aamm" を比較すると、2 つの不一致 (m と a、b と m) が示されます。等々....

私のプログラムは、ユーザーから 2 つの文字列を読み取り、最小の数字を見つけようとし、その「位置」が返されます。

int misPattern(char str[], char pattern[]){
    int count = 0, maxCount = 0, worstPattern = 0, i = 0, j = 0, z = 0,
    patternNum = 1; 
    /*in order to make minimal tests, we must have each string's length*/
    int testsMade = 0;
    int numOfTests = (strlen(str, MAX_LEN + 1)) - (strlen(pattern, MAX_LEN + 1));
    while (str[i] != '\0' && testsMade<=numOfTests){
        z = i; count = 0;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                count++;
            j++;
            z++;
        }
        j = 0;
        i++;
        if (count > maxCount){
            maxCount= count;
            worstPattern = patternNum;
        }
        patternNum++;
        testsMade++;
    }
    printf("%d\n", count);
    return worstPattern;
}

最悪のパターンは、不一致が最も少ない位置を表す必要があります。

ご想像のとおり、ここで何か問題が発生しました。デバッガーから、私の比較がうまくいっていないようです。ダバガーが示すように、文字列はよく読み取られます。

ご協力ありがとうございました。

4

1 に答える 1

0
int misPattern(char str[], char pattern[]){
    int count = 0, minCount = 0, worstPattern = 0, i = 0, j = 0, z = 0, patternNum = 0; 
    /*in order to make minimal tests, we must have each string's length*/
    int testsMade = 0;
    int numOfTests = (strlen(str, MAX_LEN + 1)) - (strlen(pattern, MAX_LEN + 1));

    /*if the pattern is longer
    than the text than return -1, and 
     if the strings are   `equal         also     return -1*/`
    if (strcmp(str,pattern)<=0)
        return -1;

    /*initial value for minCount. we have to make sure minCount is initilized at least once*/
    while (str[i] != '\0' && (minCount == 0) && testsMade<=numOfTests){
        z = i;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                minCount++;
            j++;
            z++;
        }
        testsMade++;
        j = 0;
        i++;
        patternNum++;
    }

        printf("number of first minimal mismatches is %d\n", minCount);
        printf("i is %d j is %d\n", i, j);

    while (str[i] != '\0' && testsMade<=numOfTests){
        z = i; count = 0;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                count++;

            j++;
            z++;
        }
        j = 0;
        i++;
        if (count < minCount){
            minCount= count;
            worstPattern = patternNum;
        }
        patternNum++;
        testsMade++;
    }

    return worstPattern;
}

私の過ちを理解した後、これが私の最後のプログラムです。皆さん、ありがとうございました。

于 2013-11-09T18:39:14.033 に答える