对于这种情况,我使用trie数据结构来存储文件中的每个字符串。但是,我无法找到每个字符串的位置和行号。
示例:
的test.txt
Today is sunday.
stack.
function.
data types,stack.
stack.
如果我想要搜索一个单词堆栈。输出将是:
stack[(2,1),(5,1),(4,3)].
以下是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALPHABET_SIZE (26)
struct trie_node
{
int value;
char string[10];
struct trie_node *children[ALPHABET_SIZE];
};
struct trie
{
struct trie_node *root;
int count;
};
struct trie_node *getNode(void)
{
struct trie_node *pNode = NULL;
pNode = (struct trie_node *)malloc(sizeof(struct trie_node));
if( pNode )
{
int i;
pNode->value = 0;
for(i = 0; i < ALPHABET_SIZE; i++)
pNode->children[i] = NULL;
}
return pNode;
}
void initialize(struct trie *pTrie)
{
pTrie->root = getNode();
pTrie->count = 0;
}
void insert(struct trie *pTrie, char p[])
{
int level;
int length = strlen(p);
int index;
struct trie_node *temp;
pTrie->count++;
temp = pTrie->root;
for( level = 0; level < length; level++ )
{
index = (p[level] - (int)'a');
if( !temp->children[index] )
{
temp->children[index] = getNode();
}
temp = temp->children[index];
}
strcpy(temp->string,p);
temp->value = pTrie->count;
}
int main()
{
FILE *f = fopen("test.txt", "r");
char buf[1000], *p;
struct trie tnode;
int i;
initialize(&tnode);
while(fgets(buf, 1000, f)!=NULL)
{
p = strtok(buf, " ,.':|;\n\t|");
while(p!=NULL)
{
insert(&tnode, p);
p = strtok(NULL, " ,.':;|\n\t");
}
}
return 0;
}