hihocoder 1014 Trie树
2016-09-23 21:55
344 查看
#include <iostream>
#include <cstdio>
#include<limits>
#include <map>
#include <cstring>
#include <string>
using namespace std;
const int N =100005;
struct tree
{
int num;
tree *next[26];
tree()
{
num=0;
for(int i=0; i<26; i++)
{
next[i]=NULL;
}
}
} t;
void In(char str[])
{
tree *p = &t;
int cnt;
for(int i = 0; str[i]; i++)
{
cnt = str[i]-'a';
if(p->next[cnt]==NULL)
{
p->next[cnt] = new tree();
}
p->next[cnt]->num++;
p=p->next[cnt];
}
}
int Find(char str[])
{
tree *p = &t;
int cnt, i;
for(i=0; str[i]; i++)
{
cnt = str[i] - 'a';
if(p->next[cnt]==NULL)
{
return 0;
}
else
{
p = p->next[cnt];
}
}
return p->num;
}
int main()
{
int n, scan;
char str
;
scanf("%d", &scan);
while(scan--)
{
scanf("%s", str);
In(str);
}
scanf("%d", &n);
while(n--)
{
scanf("%s", str);
printf("%d\n", Find(str));
}
return 0;
}
#include <cstdio>
#include<limits>
#include <map>
#include <cstring>
#include <string>
using namespace std;
const int N =100005;
struct tree
{
int num;
tree *next[26];
tree()
{
num=0;
for(int i=0; i<26; i++)
{
next[i]=NULL;
}
}
} t;
void In(char str[])
{
tree *p = &t;
int cnt;
for(int i = 0; str[i]; i++)
{
cnt = str[i]-'a';
if(p->next[cnt]==NULL)
{
p->next[cnt] = new tree();
}
p->next[cnt]->num++;
p=p->next[cnt];
}
}
int Find(char str[])
{
tree *p = &t;
int cnt, i;
for(i=0; str[i]; i++)
{
cnt = str[i] - 'a';
if(p->next[cnt]==NULL)
{
return 0;
}
else
{
p = p->next[cnt];
}
}
return p->num;
}
int main()
{
int n, scan;
char str
;
scanf("%d", &scan);
while(scan--)
{
scanf("%s", str);
In(str);
}
scanf("%d", &n);
while(n--)
{
scanf("%s", str);
printf("%d\n", Find(str));
}
return 0;
}
相关文章推荐
- hihocoder 1014----Trie树
- 【hihocoder】1014 Trie树
- hihocoder 1014 Trie树
- Hihocoder 1014 Trie树
- hihocoder 1014 Trie树
- 【Trie树】 Hihocoder 1014
- hihocoder 1014 : Trie树
- c++字典树 Trie树(hihocoder1014)
- Hihocoder--Trie树--1014
- 【Tire 求字典出现的前缀个数】hihocoder 1014 Trie树
- 【hihocoder】1014 : Trie树
- hihoCoder 1014 Trie树
- [hihoCoder1014]Trie树[前缀树][前缀树的建立]
- hihocoder_1014: Trie树(Trie树模板题)
- hihoCoder 1014 Trie树 字典树模板
- hihocoder-#1014 : Trie树
- hihoCoder题目AC #1014 : Trie树 Java
- hihoCoder 1014 Trie树
- hihoCoder 1014 Trie树
- hihoCoder1014 Trie树