hihocoder 1014 Trie树
2017-10-11 21:05
381 查看
题目链接:Trie树
题目大意:给你n个字符串,m次询问,每次去询问给定的字符串是给定的字符串里面中多少个字符串的前缀
题目思路:字典树直接做就好了
题目大意:给你n个字符串,m次询问,每次去询问给定的字符串是给定的字符串里面中多少个字符串的前缀
题目思路:字典树直接做就好了
#include <map> #include <set> #include <queue> #include <stack> #include <cmath> #include <cstdio> #include <vector> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e6+10; int le; struct node{ int Next[26]; int cnt; void init(){ cnt = 0; memset(Next,-1,sizeof(Next)); } }T[maxn]; void Insert(char *s){ int i = 0,p = 0; while(s[i]){ int x = s[i]-'a'; if(T[p].Next[x] == -1){ T[le].init(); T[p].Next[x] = le++; } p = T[p].Next[x]; T[p].cnt++; i++; } } void query(char *s){ int i = 0,p = 0; while(s[i]){ int x = s[i]-'a'; if(T[p].Next[x] == -1){ puts("0"); return ; } p = T[p].Next[x]; i++; } printf("%d\n",T[p].cnt); } int main(){ int n,m; char str[20]; while(~scanf("%d",&n)){ le = 1; T[0].init(); while(n--){ scanf("%s",str); Insert(str); } scanf("%d",&m); while(m--){ scanf("%s",str); query(str); } } return 0; }
相关文章推荐
- 【Tire 求字典出现的前缀个数】hihocoder 1014 Trie树
- hihoCoder 1014 Trie树
- [hihoCoder1014]Trie树[前缀树][前缀树的建立]
- Hihocoder--Trie树--1014
- hihocoder-#1014 : Trie树
- hihoCoder题目AC #1014 : Trie树 Java
- hihoCoder 1014 Trie树
- hihoCoder 1014 Trie树
- hihocoder 1014 Trie树 trie
- hihoCoder 1014 Trie树(基础字典树)
- 【hihocoder】#1014 : Trie树
- hihocoder 1014 Trie树 字典树模版题
- hihoCoder 1014 : Trie树
- hihocoder_1014: Trie树(Trie树模板题)
- hihoCoder 1014 Trie树 字典树模板
- Trie树 Hihocoder 1014 Trie树
- hihocoder-1014 Trie树(Trie树)
- hihoCoder1014 Trie树
- hihoCoder-1014-Trie树(字典树)
- hihocoder 1014: Trie树(Trie树模板题)