您的位置:首页 > 其它

HDU 1251 统计难题

2015-08-15 18:32 357 查看
注意用g++交,否则会超内存

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 26
using namespace std;
struct node
{
int cnt;
node * next[maxn];
node()
{
cnt=0;
memset(next,0,sizeof(next));
}

};
node *p,*root=new node();
void insert(char *s)
{
p=root;
for(int i=0;s[i];i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL) p->next[id]=new node(); //没有就新建节点
p=p->next[id];//当前节点
p->cnt++;//出现次数加一
}
}
int query(char *s)
{
p=root;
for(int i=0;s[i];i++)
{
int id=s[i]-'a';
if(p->next[id]==NULL) return 0;
p=p->next[id];
}
return p->cnt;
}
int main()
{
char s[20];
while(gets(s),*s) insert(s);
while(gets(s)) cout<<query(s)<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: