hdu----(2222)Keywords Search(trie树)
Keywords Search
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35683 Accepted Submission(s): 11520
Wiskey also wants to bring this feature to his image retrieval system.
Every
image have a long description, when users type some keywords to find
the image, the system will match the keywords with description of image
and show the image which the most keywords be matched.
To simplify
the problem, giving you a description of image, and some keywords, you
should tell me how many keywords will be match.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
5
she
he
say
shr
her
yasherhs
/*hdu 2222 字典树*/
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
struct Trie
{
struct Trie *next[];
int tail;
};
char str[];
char ss[];
void in_Trie(char *s,Trie *root)
{
Trie *cur=root,*newcur;
for(int i=;s[i]!='\0';i++)
{
if(cur->next[s[i]-'a']==NULL)
{
newcur=new Trie; //(Trie*)malloc(sizeof(sizeof(Trie)));
for(int j=;j<;j++)
newcur->next[j]=NULL;
newcur->tail=;
cur->next[s[i]-'a']=newcur;
}
cur=cur->next[s[i]-'a'];
}
cur->tail++;
}
int query(char *s,Trie *root)
{
int i=,cnt=;
Trie *cur;
for(int j=;s[j]!='\0';j++)
{
cur=root;
for(i=j;s[i]!='\0';i++){
if(cur->next[s[i]-'a']!=NULL){
cur=cur->next[s[i]-'a'];
cnt+=cur->tail;
cur->tail=; //只需求出第一次出现的
}
else break;
}
}
return cnt;
}
void dele(Trie *root)
{
for(int i= ; i< ; i++ )
if(root->next[i]!=NULL)
dele(root->next[i]);
// free(root);
delete root;
}
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int t,i,n;
Trie *root;
scanf("%d",&t);
while(t--)
{
root = new Trie ;
for(int j=;j<;j++)
root->next[j]=NULL;
root->tail=;
scanf("%d",&n);
for(i=;i<n;i++){
scanf("%s",str);
in_Trie(str,root);
}
scanf("%s",ss);
printf("%d\n",query(ss,root));
dele(root);
}
return ;
}
hdu----(2222)Keywords Search(trie树)的更多相关文章
- HDU 2222 Keywords Search(查询关键字)
HDU 2222 Keywords Search(查询关键字) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K ...
- HDU 2222 Keywords Search(AC自动机模版题)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- HDU 2222 Keywords Search (AC自动机)
题意:给你一些模式串,再给你一串匹配串,问你在匹配串中出现了多少种模式串,模式串可以相同 AC自动机:trie树上进行KMP.首先模式串建立trie树,再求得失配指针(类似next数组),其作用就是在 ...
- HDU 2222 Keywords Search(瞎搞)
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- hdu 2222 Keywords Search(AC自动机)
/* 啥也不说了,直接套模板... */ 1 #include<iostream> #include<map> #include<string> #include& ...
- hdu 2222 Keywords Search ac自己主动机
点击打开链接题目链接 Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDU 2222 Keywords Search (AC自动机)(模板题)
<题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...
- HDU 2222 Keywords Search(AC自己主动机模板题)
题意:给出一个字符串和若干个模板,求出在文本串中出现的模板个数. 思路:由于有可能有反复的模板,trie树权值记录每一个模板出现的次数就可以. #include<cstdio> #incl ...
- hdu 2222 Keywords Search 模板题
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
随机推荐
- 图解SQL 2008数据库复制
为了达到数据及时备份,一般采用完整备份+差异备份即可,或者再加上日志备份,本文介绍使用数据库复制技术同步数据: PS:文章以图片为主,图片更能直观的看出操作步骤和配置方法! 1.首先创建一个测试的数据 ...
- 用t4模板和head.js进行css和js的版本控制
head.js 介绍 http://headjs.com/site/api/v1.00.html#load 原文http://www.cnblogs.com/wang2650/p/5102690.h ...
- C语言程序设计现代方法1,2,3章
1:浮点型(float)运算比int慢,并且可能存在舍入误差 如float存储0.1,以后使用可能会变成0.099999999987 2:宏定义只用大写,这是大多数C程序猿遵循的规范! C语言区分大小 ...
- maven实现依赖的“全局排除”
大多数java应用源码构建和依赖管理是使用maven来实现的,maven也是java构建和依赖管理的事实上的标准.我们的应用系统也都是基于maven构建的,maven虽然在依赖管理方面确实很牛叉,但是 ...
- 2013 Multi-University Training Contest 4
HDU-4632 Palindrome subsequence 题意:给定一个字符串,长度最长为1000,问该串有多少个回文子串. 分析:设dp[i][j]表示从 i 到 j 有多少个回文子串,则有动 ...
- ZOJ-3725 Painting Storages 动态规划
题意:给定一个数N,表示有N个位置,要么放置0,要么放置1,问至少存在一个连续的M个1的放置方式有多少? 分析:正面求解可能还要考虑到重复计算带来的影响,该题适应反面求解.设dp[i][j]表示到前 ...
- iOS - OC NSProcessInfo 系统进程信息
前言 @interface NSProcessInfo : NSObject NSProcessInfo 类中包含一些方法,允许你设置或检索正在运行的应用程序(即进程)的各种类型的信息. 1.获取系统 ...
- 使用BAPI_ACC_DOCUMENT_POST,创建会计凭证,用BADI扩展字段(转)
业务需求:和银行做一个接口,要通过银行流水产生会计凭证,会计凭证的事务码是F-02,查到了BAPI方法BAPI_ACC_DOCUMENT_POST.昨天测试发现,有一些参数在BAPI_ACC_DOCU ...
- mysql概要(十四)索引
1.索引是对数据库数据建立目录加快了查询速度.索引分为哈希索引和二叉树索引 (大数据量转移,如果表中带有大量字段索引,进行数据导入时,建议先去掉索引导入数据再统一加入索引,减少索引计算量) 2.索引原 ...
- adb_常用命令
1. adb push 电脑中的文件(包含路径) Android中的绝对路径 2. adb pull Android中的绝对路径文件 电脑中的绝对路径 3. adb install ??.ap ...