コード例 #1
0
ファイル: str.c プロジェクト: snowcrystall/ds
//TODO  求最长重复字串,最长公共字串
int main()
{
	char *s1 = "asdfgsdfhasjasdkasd\0";
	printf("%s\n",s1);
	char *s2 = "asdkasd\0";
	printf("%s\n",s2);
	printf("%d\n",strstr_kmp(s1,s2,0));
	return 0;
}
コード例 #2
0
ファイル: main.c プロジェクト: joaomsa/StringSearch
int main(int argc, char *argv[])
{
    int needleLen, haystackLen;
    char *needle, *haystack, *ptr;
    FILE *input;
    if (argc < 3){
        fprintf(stderr, "Missing paramerter\n");
        abort();
    }
    input = fopen(argv[1], "r");

    while (1){
        if (fscanf(input, "%i %i", &haystackLen, &needleLen) != 2){
            fprintf(stderr, "Missing number of notes\n");
            abort();
        }

        if (needleLen == 0 || haystackLen == 0)
            break;

        CHKMLC(needle = malloc(sizeof(char) * needleLen));
        CHKMLC(haystack = malloc(sizeof(char) * haystackLen));

        music_read(input, haystackLen, haystack);
        music_read(input, needleLen, needle);

        music_pitch_diff(&haystackLen, haystack);
        music_pitch_diff(&needleLen, needle);

        switch(argv[2][0] - '0'){
            case 1: ptr = strstr_bf(haystack, needle);
                    break;
            case 2: ptr = strstr_kmp(haystack, needle);
                    break;
            case 3: ptr = strstr_bmh(haystack, needle);
                    break;
            case 4: ptr = strstr_bitap(haystack, needle);
                    break;
            default: ptr = strstr(haystack, needle);
        }

        if (ptr != NULL)
            printf("S %li\n", ptr - haystack);
        else
            printf("N\n");

        free(needle);
        free(haystack);
    }
    return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: strstr_KMP.c プロジェクト: gs0622/algo
int main(int argc, char *argv[])
{
    char *s;
    if (argc<3) return 0;

    s = strstr(argv[1], argv[2]); /*argv[1]=string, argv[2]=pattern*/
    if (s)
        printf("1: pattern \"%s\" is found in string \"%s\" offset %ld\n", argv[2], argv[1], s-argv[1]);
    else 
        printf("1: pattern \"%s\" is not found in string \"%s\"\n", argv[2], argv[1]);

    s = strstr_kmp(argv[1], argv[2]); /*argv[1]=string, argv[2]=pattern*/
    if (s)
        printf("2: pattern \"%s\" is found in string \"%s\" offset %ld\n", argv[2], argv[1], s-argv[1]);
    else 
        printf("2: pattern \"%s\" is not found in string \"%s\"\n", argv[2], argv[1]);

    return 0;
}