Apa tu sei laureato in Fisica Quantistica (tesi sulla teoria delle stringhe?! )
Per me è troppo complicato da capire, figurati a "stendere" del codice
Devo ancora capire bene (e simulare meglio!) quello di VIC io !!!!!!!!!
\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct state state;
struct state {
int word;
int index;
int count;
};
typedef struct state_array state_array;
struct state_array {
size_t capacity;
size_t size;
state states[];
};
static inline state_array *new_state_array(size_t capacity) {
size_t size = sizeof(state_array) + capacity*sizeof(state);
state_array *ret = malloc(size);
if (!ret) { return NULL; }
ret->capacity = capacity;
ret->size = 0;
return ret;
}
static inline state_array *grow(state_array **arr, size_t new_capacity)
{
if (!arr) { return new_state_array(new_capacity); }
if (*arr && (*arr)->capacity >= new_capacity) { return *arr; }
state_array *ret = new_state_array(new_capacity);
if (!ret) { return NULL; }
if (!memcpy(ret->states, (*arr)->states, (*arr)->size*sizeof(state))) {
free(ret);
return NULL;
}
free(*arr);
*arr = ret;
return ret;
}
static inline state_array *push_back(state_array *arr, state s)
{
if (!arr) { return arr; }
if (arr->capacity == arr->size) {
if (!grow(&arr, arr->capacity*2)) { return NULL; }
}
arr->states[arr->size++] = s;
return arr;
}
int combinations(const int dictionary_size, const char * dictionary[dictionary_size], const char *text)
{
state_array *states = NULL, *states_next = NULL;
states = new_state_array(dictionary_size);
if (!states) { return -1; }
push_back(states, (state){ -1, -1, 1 });
states_next = new_state_array(dictionary_size);
if (!states) { free(states); return -1; }
push_back(states_next, (state){-1, -1, 0});
int i = 0;
while (text[i]) {
if (states->states[0].count > 0) {
for (int d = 0; d < dictionary_size; ++d) {
if (!dictionary[d][0]) { // size 0 strings in dictionary
states_next->states[0].count += states->states[0].count;
}
if (text[i] == dictionary[d][0]) {
if (!dictionary[d][1]) { // size 1 strings in dictionary
states_next->states[0].count += states->states[0].count;
} else {
push_back(states_next, (state){ d, 0, states->states[0].count });
}
}
}
}
for (size_t j = 1; j < states->size; ++j) {
const char *word = dictionary[ states->states[j].word ];
if (text[i] == word[ states->states[j].index + 1 ]) {
if (!word[ states->states[j].index + 2 ]) {
states_next->states[0].count += states->states[j].count;
} else {
push_back(states_next, (state){ states->states[j].word, states->states[j].index+1, states->states[j].count });
}
}
}
state_array *tmp = states;
states = states_next;
states_next = tmp;
states_next->size = 1;
states_next->states[0] = (state){-1, -1, 0};
++i;
}
int ret = states->states[0].count;
free(states);
free(states_next);
return ret;
}
int main(void)
{
const char * dictionary[] = {
"AB", "BA", "ABB", "BAB"
};
const size_t dictionary_size = sizeof(dictionary)/sizeof(const char *);
const char *str1 = "ABBBABABAB";
printf("Stringa: %s. Modi: %d.\n", str1, combinations(dictionary_size, dictionary, str1));
const char *str2 = "ABBAAAB";
printf("Stringa: %s. Modi: %d.\n", str2, combinations(dictionary_size, dictionary, str2));
return 0;
}
Visitano il forum: Nessuno e 1 ospite