题意:返回字典中所有以测试串为前缀的字符串总数。

解题关键:trie模板题,由AC自动机的板子稍加改造而来。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=;
const int MAXN=;
struct Trie{//数组形式
int Next[MAXN][N],Fail[MAXN],End[MAXN],root,tot;//大小为所以匹配字符串的总和
int newnode(){//结构体内部用
for(int i=;i<N;i++) Next[tot][i]=-;
End[tot++]=;
return tot-;
}
void init(){
tot=;
root=newnode();
}
void insert(char buf[]){
int len=strlen(buf);
int now=root;//now是temp指针
for(int i=;i<len;i++){
int k=buf[i]-'a';
if(Next[now][k]==-) Next[now][k]=newnode();//next数组代表的是下一个字符索引
now=Next[now][k];
End[now]++;
}
}
int fnd(char buf[]){
int len=strlen(buf);
int now=root;//now是temp指针
for(int i=;i<len;i++){
int k=buf[i]-'a';
if(Next[now][k]==-) return ;//next数组代表的是下一个字符索引
now=Next[now][k];
}
return End[now];
}
};
Trie ac;
char buf[];
int main(){
ac.init();
while(gets(buf)){
if(buf[]==NULL)break;//gets读入的回车会自动转化为NULL
ac.insert(buf);
}
while(gets(buf)){
printf("%d\n",ac.fnd(buf));
}
return ;
}

[hdu1251]统计难题(trie模板题)的更多相关文章

  1. hdu1251 字典树trie 模板题

    //字典树模板题.题意:给一个库,每次查询,求以之为前缀的单词数量. #include<iostream> #include<string> #include<vecto ...

  2. HDU1251 统计难题 Trie树

    题目很水,但毕竟是自己第一道的Trie,所以还是发一下吧.Trie的更多的应用慢慢学,AC自动机什么的也慢慢学.... #include<iostream> #include<cst ...

  3. HDU1251 统计难题 trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 找前缀数量 裸模板 #include<cstdio> #include<cstr ...

  4. HDU1251统计难题---Trie Tree

    map巧过 #include <stdio.h> #include <string.h> #include <map> #include <string> ...

  5. HDU1251 统计难题 【trie树】

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  6. HDU1251 统计难题(Trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. HDU1251 统计难题 (字典树模板)题解

    思路:模板题,贴个模板 代码: #include<cstdio> #include<cstring> #include<cstdlib> #include<q ...

  8. hdu1251统计难题(trie)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

随机推荐

  1. Adobe Reader 的直接下载地址

    页面:https://get.adobe.com/reader/direct/ 选择操作系统.选择语言.选择版本,然后点击立即下载. https://ardownload2.adobe.com/pub ...

  2. hdu 3401 单调队列优化+dp

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

  3. MySQL 索引知识整理(创建高性能的索引)

    前言: 索引优化应该是对查询性能优化的最有效的手段了.索引能够轻易将查询性能提高几个数量级. // 固态硬盘驱动器有和机械硬盘启动器,有着完全不同的性能特性: 然而即使是固态硬盘,索引的原则依然成立, ...

  4. Nodejs调试技术总结

    调试技术与开发技术构成了软件开发的基石.目前Nodejs作为新型的Web Server开发栈倍受开发者关注.总的来说Nodejs的应用程序主要有两部分:JavaScript编写的js模块和C语言编译的 ...

  5. LeetCode OJ:Count and Say(数数)

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  6. 不能解决,复选框在request对象获取的信息后显示在用户信息里面为中文的选项名

    因为方框里面value 不用中文?.? 假如用中文呢?  完全可以!!已经试验 如果不用中文,那么中文可以用对象的参数来获得,即在login.jsp中就要用javabean类属性

  7. nyoj-155-求高精度幂(java大数)

    题目链接 import java.util.*; import java.math.*; public class Main{ public static void main(String[] arg ...

  8. LeetCode Perfect Number

    原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...

  9. C++语言对C的增强(1)——实用性、变量检测、struct类型、C++中所有变量和函数都必须有类型、bool类型、三目运算符

    1.“实用性”增强 C语言中的变量都必须在作用域开始的位置定义,C++中更强调语言的“实用性”,所有的变量都可以在需要使用时再定义. 2.变量检测加强 在C语言中,重复定义多个同名的全局变量是合法的: ...

  10. bzoj 4300 绝世好题——DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4300 考虑 dp[ i ] 能从哪些 j 转移过来,就是那些 a[ j ] & a[ ...