Exemplo n.º 1
0
// search keywords in array Keys in the text T
// Keys: 	the 2D char array holding keywords
// T:		the pointer to the text array
// num_states: 	the number of states in the finite state machine, 
// 		this parameter need not to be exact, you can input the maximum number of states
// num_keys: 	the number of keys in the keyword set
// alpb:	the size of the alphabet. 
void multi_pattern_search(char *T, char **Keys, int num_states, int num_keys, int alpb){
	int **G = construct_trie_and_L(Keys, num_keys, num_states, alpb);
	int *F = construct_fsm_and_L(Keys,G, alpb, num_keys);
	int j = 0;
	int i = 1;
	int n = strlen(T) - 1; // characters in T are indexed from 1
	for(; i <= n; i++){
		while(j >= 0 && G[j][id(T[i])] == -1){
			j = F[j];
		}
		if(j == -1) j = 0;
		else {
			j = G[j][id(T[i])];
			if(strlen(L[j]) > 0){
				printf("the keyword found at state %d is: ", j);
				print_char_arr(L[j]);
				printf("\n");
				int keylen = strlen(L[j])-1; // ignore the first empty character
				printf("the last pos of the text containing the keywords is:  %d\n", i);
			}
			if(is_leaf(j, G, alpb)){
				j = F[j];
			}
		}
	}
}
Exemplo n.º 2
0
void bizz_buss(FILE * file) {
    char num[MAX_NUMBER_LENGTH] = {};


    bool was_number = false;
    bool was_space = true;

    int digit_sum = 0;
    int digit_last = -1;
    int digit_count = 0;


    int ch;
    while (EOF != (ch = getc(file))) {

        if (isdigit(ch) && (was_space || was_number)) {

            int digit = ch - '0';

            was_number = true;
            digit_sum += digit;
            digit_last = digit;

            if (digit_count >= MAX_NUMBER_LENGTH) {
                fprintf(stderr, "ERROR: a number has more than %d digits", MAX_NUMBER_LENGTH);
                exit(ERR_NUM_TOO_BIG);
            }
            num[digit_count] = (char) ch;
            digit_count += 1;

        } else if (was_number) {
            if (isspace(ch) || EOF == ch) {
                bool mod_3 = is_mod_3(digit_sum);
                bool mod_5 = is_mod_5(digit_last);

                if (mod_3 && mod_5) {
                    printf("bizzbuss");
                } else if (mod_3) {
                    printf("bizz");
                } else if (mod_5) {
                    printf("buss");
                } else {
                    print_char_arr(num, digit_count);
                }

            } else {
                print_char_arr(num, digit_count);
            }

            was_number = false;
            digit_sum = 0;
            digit_last = -1;
            digit_count = 0;

            printf("%c", ch);

        } else {
            printf("%c", ch);
        }

        was_space = isspace(ch);
    }
}