Pagini recente » Cod sursa (job #874376) | Cod sursa (job #2315937) | Cod sursa (job #889930) | Cod sursa (job #1553130) | Cod sursa (job #1863705)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node{
int occurences;
struct node *next[26];
};
struct node *head = NULL;
void Initialize(){
head = (struct node*)malloc(sizeof(struct node));
head -> occurences = 0; int i;
for(i=0; i<26; i++){
head -> next[i] = NULL;
}
}
void Insert(char word[]){
struct node *ptr = head;
int i, j, len = strlen(word), index;
for(i=0; i<len; i++, ptr = ptr -> next[index]){
index = tolower(word[i]) - 'a';
if(ptr -> next[index] == NULL){
ptr -> next[index] = (struct node*)malloc(sizeof(struct node));
(ptr -> next[index]) -> occurences = 0;
for(j=0; j<26; j++){
(ptr -> next[index]) -> next[j] = NULL;
}
}
}ptr -> occurences++;
}
int Search(char word[]){
struct node *ptr = head;
int i, len = strlen(word), index;
for(i=0; i<len; i++, ptr = ptr -> next[index]){
index = tolower(word[i]) - 'a';
if(ptr -> next[index] == NULL) return 0;
}
return ptr -> occurences;
}
int LongestPrefix(char word[]){
struct node *ptr = head;
int i, j, index, prefix = 0, len = strlen(word);
for(i=0; i<len; i++, ptr = ptr -> next[index]){
index = tolower(word[i] - 'a');
if(ptr -> next[index] == NULL) return prefix;
int vertices = 0;
for(j=0; j<26; j++){
vertices += !!((ptr -> next[index]) -> next[j]);
}
if((ptr -> next[index]) -> occurences || vertices >= 1){
prefix = i+1;
}
}
return prefix;
}
void Delete(char word[21]){
struct node *ptr = head, *depth = NULL;
int i, j, k, index, len = strlen(word), vertices = 0;
for(i=0; i<len; i++, ptr = ptr -> next[index]){
index = tolower(word[i] - 'a');
if(ptr -> next[index] == NULL) return;
if(i != len-1 && ptr -> occurences){
depth = ptr;
j = i;
}
vertices = 0;
for(k = 0; i && k < 26; k++){
vertices += !!(ptr -> next[k]);
}if(vertices > 1 && i > j){
j = i;
depth = ptr;
}
}if(ptr -> occurences == 0) return;
vertices = 0;
for(i=0; i<26; i++){
vertices += !!(ptr -> next[i]);
}
if(ptr -> occurences > 1 || ptr -> occurences == 1 && vertices){
ptr -> occurences--;
return;
}
if(depth == NULL){
index = tolower(word[0] - 'a');
ptr = head -> next[index];
head -> next[index] = NULL;
for(i=1; i<len; i++){
index = tolower(word[i] - 'a');
struct node *temp = ptr;
ptr = ptr -> next[index];
free(temp);
}free(ptr);
}else{
k = len - j;
index = tolower(word[j] - 'a');
struct node *temp = depth;
depth = depth -> next[index];
temp -> next[index] = NULL;
for(i=1; i<k; i++){
index = tolower(word[j+i] - 'a');
temp = depth;
depth = depth -> next[index];
free(temp);
}free(depth);
}
}
int main(){
Initialize();
freopen("trie.in", "r", stdin);
freopen("trie.out", "w", stdout);
int task;
char word[21], ch;
while(1){
if(isdigit(ch)){
task = ch;
}else{
scanf("%d", &task);
}
scanf("%s", word);
if(task==0){
Insert(word);
}
if(task==1){
Delete(word);
}
if(task==2){
printf("%d\n", Search(word));
}
if(task==3){
printf("%d\n", LongestPrefix(word));
}
ch = getchar();
if(ch==EOF) break;
}
return 0;
}