Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2932    Accepted Submission(s): 1116

Problem Description

When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results.
Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
 
Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and
all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
 
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
 
Sample Input
20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
 
Sample Output
0
20
11
11
2
 
Source
 



题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846



题目大意:p个字符串,q个关键词,问有多少个字符串包括关键词



题目分析:对于每一个字符串,我们依照其后缀建立字典树,建树时须要加1个id。表示这个分支来自第几个字符串,不然会反复计数。比方例子的第三组查询

#include <cstdio>
#include <cstring>
char s[25];
int id; struct node
{
node *next[26];
int cnt;
int id;
node()
{
memset(next, NULL, sizeof(next));
cnt = 0;
id = -1;
}
}; void Insert(node *p, char *s, int index, int id)
{
for(int i = index; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
p -> next[idx] = new node();
p = p -> next[idx];
if(p -> id != id)
{
p -> id = id;
p -> cnt ++;
}
}
} int Search(node *p, char *s)
{
for(int i = 0; s[i] != '\0'; i++)
{
int idx = s[i] - 'a';
if(p -> next[idx] == NULL)
return 0;
p = p -> next[idx];
}
return p -> cnt;
} int main()
{
int p, q;
id = 0;
node *root = new node();
scanf("%d", &p);
for(int i = 0; i < p; i++)
{
scanf("%s", s);
int len = strlen(s);
for(int j = 0; j < len; j++)
Insert(root, s, j, i);
}
scanf("%d", &q);
for(int i = 0; i < q; i++)
{
scanf("%s", s);
printf("%d\n", Search(root, s));
}
}

HDU 2846 Repository (字典树 后缀建树)的更多相关文章

  1. HDU 2846 Repository(字典树,标记)

    题目 字典树,注意初始化的位置~!!位置放错,永远也到不了终点了org.... 我是用数组模拟的字典树,这就要注意内存开多少了,,要开的不大不小刚刚好真的不容易啊.... 我用了val来标记是否是同一 ...

  2. hdu 2846 Repository (字典树)

    RepositoryTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  3. hdu 2846(字典树)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  4. HDU 2846 Repository(字典树,每个子串建树,*s的使用)

    Repository Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  5. HDU 2846 Repository(字典树)

    字典树较为复杂的应用,我们在建立字典树的过程中需要把所有的前缀都加进去,还需要加一个id,判断它原先是属于哪个串的.有人说是AC自动机的简化,但是AC自动机我还没有做过. #include<io ...

  6. hdu 2846 Repository

    http://acm.hdu.edu.cn/showproblem.php?pid=2846 Repository Time Limit: 2000/1000 MS (Java/Others)     ...

  7. POJ3630/HDU-1671 Phone List,字典树静态建树!

    Phone List POJ动态建树TLE了~~~ 题意:拨打某个电话时可能会因为和其他电话号码的前几位重复而导致错误,现在给出一张电话单,求是否有某个电话是其他电话的前缀.是则输出NO,否则输出YE ...

  8. hdu 1979 DFS + 字典树剪枝

    http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...

  9. HDU 1671 (字典树统计是否有前缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...

随机推荐

  1. POJ TOYS(叉积的妙用+二分答案)

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16042   Accepted: 7688 Description ...

  2. NodeJS学习笔记 (27)实用工具模块-util(ok)

    debuglog(section) 很有用的调试方法.可以通过 util.debuglog(name) 来创建一个调试fn,这个fn的特点是,只有在运行程序时候,声明环境变量NODE_DEBUG=na ...

  3. ArchLinux 设置时间同步和硬件时间同步错误 No usable clock interface found

    笔记本不知道怎么了,总是时间对不上 硬件时间也设置不了,只能时间同步了 手动时间同步 ntpdate即可,ntp服务器在这里用这两个就好了 cn.ntp.org.cn 或 edu.ntp.org.cn ...

  4. python、js 时间日期模块time

    python 参考链接:https://www.runoob.com/python/python-date-time.html 时间戳 >>> print(time.time())# ...

  5. JDBC连接SQL Server 2005 报错Connection refused: connect

    com.microsoft.sqlserver.jdbc.SQLServerException: 通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败.错误:“Connect ...

  6. 【转】CentOS下firefox安装flash说明

    http://www.cnblogs.com/lamper/archive/2013/01/16/2862254.htm CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装 ...

  7. BestCoder Round #11 (Div. 2)

    太菜,仅仅能去Div2.(都做不完 ORZ... 各自是 HDU: 5054pid=5054"> Alice and Bob 5055Bob and math problem 5056 ...

  8. WITH common_table_expression (Transact-SQL)

    https://docs.microsoft.com/en-us/sql/t-sql/queries/with-common-table-expression-transact-sql Specifi ...

  9. Task Scheduler

    https://technet.microsoft.com/en-us/library/cc748993(v=ws.11).aspx#BKMK_winui If Task Scheduler is n ...

  10. XML结构,写到TreeView树上

    http://blog.csdn.net/ztzi321/article/details/44077563