题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1251
node *head=(node*)malloc(sizeof(node));
for(int i=0; i<26; i++)
{
head->next[i] = NULL;
head->sum = 0;
}
可以改成node *head=new node();
要用c++提交才对,不然G++就内存超限了-_-
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;struct node
{
int sum;
node *next[];
};void BuildLibTree(node *head, char s[])
{
node *p = head,*q; for(int i=; s[i]; i++)
{
int k = s[i] - 'a';
if(p->next[k] == NULL)
{
q=(node*)malloc(sizeof(node));
for(int j=; j<; j++)
{
q->next[j] = NULL;
q->sum = ;
}
p->next[k] = q;
}
p = p->next[k];
p->sum++;
}
}
int Query(node *head, char s[])
{
node *p = head;
for(int i=; s[i]; i++)
{
int k = s[i]-'a';
if(p->next[k] == NULL)
return ;
p = p->next[k];
}
return p->sum;
}
int main()
{
char s[];
node *head=(node*)malloc(sizeof(node));
for(int i=; i<; i++)
{
head->next[i] = NULL;
head->sum = ;
}
while(gets(s),s[])
BuildLibTree(head, s); while(scanf("%s",s)!=EOF)
printf("%d\n", Query(head, s));
return ;
}