統計難題
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 29855 Accepted Submission(s): 11649
注意:本題只有一組測試數據,處理到文件結束.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int num;
struct node *next[30];
node()
{
memset(next,NULL,sizeof(next));
num=0;
}
};
void insert(node *&root,char s[])
{
int i,k;
node *p;
if(!root) root=new node();
p=root;
for(i=0; s[i]; i++)
{
if(p->next[s[i]-'a']==NULL)
p->next[s[i]-'a']=new node();
p=p->next[s[i]-'a'];
p->num++;
}
}
int suan(node *&root,char s[])
{
int i,k;
node *p;
p=root;
for( i=0;s[i];i++)
{
if(p->next[s[i]-'a']==NULL)
break;
p=p->next[s[i]-'a'];
}
if(s[i])
return 0;
else return p->num;
}
int main()
{
char s[11];
node *root = new node();
while(gets(s),*s)
{
insert(root,s);
}
while(gets(s))
{
printf("%d\n",suan(root,s));
}
}