Esempio n. 1
0
int main()
{
  
  char str_hash[MAX_LEN];
  int n;
  scanf ("%d", &n);
  getchar();

  int i;
  for (i = 0; i < n; i++)
    gets (words[i]);

  //  for (i = 0; i < n; i++)
  //    quick_sort (words[i], 0, strlen (words[i]) - 1);


  struct trie_node* root = create_trie_node ();
  
  for (i = 0; i < n; i++)
    {
      strcpy (str_hash, words[i]);
      quick_sort (str_hash, 0, strlen (str_hash) - 1);
      trie_insert (root, str_hash, i);
    }

  int q;
  scanf ("%d", &q);
  getchar();
  for (i = 0; i < q; i++)
    {
      char query[MAX_LEN];
      gets (query);
      strcpy (str_hash, query);
      quick_sort (str_hash, 0, strlen (str_hash) - 1);
      struct trie_node* node = trie_search (root, str_hash);
      if (node == NULL)
        {
          answer[i] = 0;
        }
      else
        {
          //          printf ("p->size: %d\n", node->size);
          answer[i] = node->size;
          int j;
          for (j = 0; j < node->size; j++)
            {
              //              printf ("ori: %s\n", words[node->index[j]]);
              //              printf ("current: %s\n", query);
              if (strcmp (words[node->index[j]], query) == 0)
                answer[i] -= 1;
            }
        }
    }

  for (i = 0; i < q; i++)
    printf ("%d\n", answer[i]);
  return 0;
}
Esempio n. 2
0
File: trie.c Progetto: GZShi/Trie-C
int trie_add_record(trie* root, const char* str, int times)
{
	trie *p = root->children;

	while(p)
	{
		if(p->value == str[0])
		{
			if(str[0] != '\0')
				return trie_add_record(p, str + 1, times);
			p->times += times;
			return p->times;
		} 
		else if(p->next == NULL)
		{
			if(!(p->next = create_trie_node()))
			{
				return ADD_FAILED;
			}
			p = p->next;
			p->value = str[0];
			if(str[0] != '\0')
				return trie_add_record(p, str + 1, times);
			p->times += times;
			return p->times;
		}
		p = p->next;
	}

	if(!(root->children = create_trie_node()))
	{
		return ADD_FAILED;
	}
	root->children->value = str[0];
	if(str[0] != '\0')
		return trie_add_record(root->children, str + 1, times);
	root->children->times += times;
	return root->children->times;
}
Esempio n. 3
0
gchar* create_pinyin_trie(const gchar* data)
{
    int i = 0;
    int err_flag = 0;
    pinyin_trie* root = NULL;
    gchar* pinyin = NULL;
    gchar* str_md5 = NULL;
    gchar** data_split = NULL;

    if ( !data ) {
        g_warning ("args error in create pinyin trie!\n");
        return NULL;
    }

    root = create_trie_node (' ');
    if ( !root ) {
        g_warning ("create trie failed!\n");
        return NULL;
    }

    fprintf (stdout, "\ntrie data: %s\n\n", data);
    str_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5,
              data, -1);
    g_print ("str md5: %s\n", str_md5);

    data_split = g_strsplit (data, ";", -1);
    for (; data_split[i] != NULL; i++ ) {
        /*fprintf (stdout, "word: %s\n", data_split[i]);*/
        pinyin = get_pinyin (data_split[i]);
        if ( !pinyin ) {
            err_flag = 1;
            break;
        }

        insert_pinyin (pinyin, i, root);
        g_free (pinyin);
    }

    if ( err_flag ) {
        g_warning ("get pinyin failed!\n");
        g_strfreev (data_split);
        return NULL;
    }

    g_hash_table_insert (trie_table, g_strdup (str_md5), root);
    g_hash_table_insert (data_table, g_strdup (str_md5), data_split);
    return str_md5;
}
Esempio n. 4
0
void trie_insert (struct trie_node* root, char word[], int index)
{
  struct trie_node* node = root;
  int i = 0;
  while (word[i] != '\0')
    {
      if (node->children[word[i] - 'A'] == NULL)
        {
          node->children[word[i] - 'A'] = create_trie_node ();
        }
      node = node->children[word[i] - 'A'];
      i++;
    }
  node->index[node->size] = index;
  node->size += 1;
}
Esempio n. 5
0
void add_string(struct trie_node *root, char *s) {
    char *p = s;
    struct trie_node *parent = root;
    while(*p) {
        struct trie_node *node = parent->child;
        while(node != NULL && node->c != *p) {
            node = node->brother;
        }
        if(node == NULL) {
            node = create_trie_node(*p);
            node->brother = parent->child;
            parent->child = node;
        }
        else {
            node->count++;
        }
        parent = node;
        p++;
    }
}
Esempio n. 6
0
void insert_char (char ch, int pos, pinyin_trie* node)
{
    int i = 0;
    int len = 0;
    int k = ch_to_num (ch);
    int* value = NULL;

    if ( !node->next[k] ) {
        node->next[k] = create_trie_node (ch);
        node->next[k]->flag = 1;
    }
    node->next[k]->ret_pos.cnt += 1;
    len = node->next[k]->ret_pos.cnt;
    /*g_print ("$$$$$ value size: %d\n", len);*/

    value = g_new0 (int, len);
    memcpy (value, node->next[k]->ret_pos.pos, (len - 1) * sizeof (int));
    value[len - 1] = pos;

    g_free (node->next[k]->ret_pos.pos);
    node->next[k]->ret_pos.pos = value;
    value = NULL;
}