地址:点击打开链接
这个题的bug就是G++编译的时候会一直Memory out,用C++交,就行了
还有注意第一个的结束条件,是空格,而不是EOF
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include<malloc.h>
using namespace std;
struct Node{
Node* next[26];
int num ;
Node(){
for(int i =0 ; i < 26; i++)
next[i]=NULL;
num = 0 ;
}
};
Node *p,*root = new Node() ;
void Insert(char target[])
{
p = root ;
for(int i = 0 ; i < strlen(target);i++)
{
int temp = target[i] -'a' ;
if(p->next[temp]==NULL)
{
p->next[temp] = new Node();
}
p = p->next[temp];
p->num++;
}
}
int Search(char target[])
{
p = root ;
for(int i = 0 ; i < strlen(target);i++)
{
int temp = target[i]-'a';
if(p->next[temp]==NULL)
return 0 ;
p=p->next[temp];
}
return p->num ;
}
int main()
{
char temp[11] ;
while(gets(temp)&&temp[0])
{
Insert(temp);
}
char question[11];
while (~scanf("%s",&question))
{
printf("%d\n",Search(question));
}
return 0 ;
}
837

被折叠的 条评论
为什么被折叠?



