kmp&字典树 模板
kmp:
const int maxn=1e5+5;
char s[maxn],p[maxn];
int nex[maxn];
int KmpSearch(char* s, char* p)
{
int i = 0;
int j = 0;
int sLen = strlen(s);
int pLen = strlen(p);
while (i < sLen && j < pLen)
{
//①如果j = -1,或者当前字符匹配成功(即S[i] == P[j]),都令i++,j++
if (j == -1 || s[i] == p[j])
{
i++;
j++;
}
else
{
//②如果j != -1,且当前字符匹配失败(即S[i] != P[j]),则令 i 不变,j = next[j]
//next[j]即为j所对应的next值
j = nex[j];
}
}
if (j == pLen)
return i - j;
else
return -1;
}
void GetNext(char* p,int next[])
{
int pLen = strlen(p);
nex[0] = -1;
int k = -1;
int j = 0;
while (j < pLen - 1)
{
//p[k]表示前缀,p[j]表示后缀
if (k == -1 || p[j] == p[k])
{
next[++j] = ++k;
}
else
{
k = next[k];
}
}
}
//优化过后的next 数组求法
void GetNextval(char* p, int next[])
{
int pLen = strlen(p);
next[0] = -1;
int k = -1;
int j = 0;
while (j < pLen - 1)
{
//p[k]表示前缀,p[j]表示后缀
if (k == -1 || p[j] == p[k])
{
++j;
++k;
//较之前next数组求法,改动在下面4行
if (p[j] != p[k])
next[j] = k; //之前只有这一行
else
//因为不能出现p[j] = p[ next[j ]],所以当出现时需要继续递归,k = next[k] = next[next[k]]
next[j] = next[k];
}
else
{
k = next[k];
}
}
}
字典树:
const int maxn=1e5+5;
int trie[maxn][26];
int sum[maxn];
int exist[maxn];
char s[maxn];
int tot=0;
void insert(char s[]) //插入字符串;
{
int len=strlen(s);
int root=0;
for(int i=0;i<len;i++)
{
int id=s[i]-'a';
if(trie[root][id]==0)
{
trie[root][id]=++tot; //tot新建节点编号;
exist[root]=0;
}
root=trie[root][id];
sum[root]++; //表示有相同该前缀的单词个数;
}
exist[root]=1; //该节点是存入的单词最后一位可以用来查询是否有这个单词;
}
// 查找是否为插入单词
bool find(char s[])
{
int len=strlen(s);
int root=0;
for(int i=0;i<len;i++)
{
int id=s[i]-'a';
if(trie[root][id]==0) //不存在直接退出;
{
return false;
}
root=trie[root][id];
}
if(exist[root])
{
return true;
}
else return false;
}
// 查找前缀为 s出现的次数;
int find_sum(char s[])
{
int len=strlen(s);
int root=0;
for(int i=0;i<len;i++)
{
int id=s[i]-'a';
if(trie[root][id]==0)
{
return 0;
}
root=trie[root][id];
}
return sum[root];
}
kmp&字典树 模板的更多相关文章
- 字典树模板题(统计难题 HDU - 1251)
https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...
- CH 1601 - 前缀统计 - [字典树模板题]
题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...
- hdu1521(字典树模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意: 中文题诶~ 思路: 字典树模板 代码1: 动态内存, 比较好理解一点, 不过速度略慢, ...
- HDU - 1251 字典树模板题
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- 字典树模板 HDU - 1251
题意: 给一些单词,换行键后,查找以后输入的单词作为前缀的话们在之前出现过几次. 思路: 字典树模板----像查字典的顺序一样 #include<string> #include<s ...
- 字典树模板( 指针版 && 数组版 )
模板 : #include<string.h> #include<stdio.h> #include<malloc.h> #include<iostream ...
- P1184 高手之在一起(字典树模板题,hash算法, map)
哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- Xor Sum---hdu4825(01字典树模板)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4825 题意:有n个数m个查找,每个查找有一个数x, 从序列中找到一个数y,使得x异或y最大 ...
随机推荐
- Sentry 中文版
Sentry 中文版 汉化 https://sentry.io/settings/account/details/ 打开用户设置 User settings 语言选择中文 Simplified Chi ...
- GitHub Actions in Action
GitHub Actions in Action https://lab.github.com/githubtraining/github-actions:-hello-world https://g ...
- 使用 js 实现一个简易版的动画库
使用 js 实现一个简易版的动画库 具有挑战性的前端面试题 animation css refs https://www.infoq.cn/article/0NUjpxGrqRX6Ss01BLLE x ...
- shit 牛客网
shit 牛客网 为什么,只可以 log 一次,什么垃圾逻辑呀! https://www.nowcoder.com/test/question/e46437833ddc4c5bb79f7af7a1b7 ...
- webpack & webpack-cli
webpack & webpack-cli Error: Cannot find module 'webpack' Google site: stackoverfow UnhandledPro ...
- learning free programming resources form top university of the world
learning free programming resources form top university of the world Harvard university https://www. ...
- vue动态添加当前事件下的class
html部分<div class="star"> <span v-for="(item,index) in 5" @click="c ...
- 「NGK每日快讯」12.2日NGK公链第29期官方快讯!
- vue最好的ssr服务器渲染框架
vue和angular js.react三大框架非常好用,现在大部分人都使用了这三大框架进行开发. 但是vue这些框架到目前位置,大部分还是用来做管理后台,用来做移动端.而官网网站却很少用他们来开发. ...
- 【SpringMVC】 4.3 拦截器
SpringMVC学习记录 注意:以下内容是学习 北京动力节点 的SpringMVC视频后所记录的笔记.源码以及个人的理解等,记录下来仅供学习 第4章 SpringMVC 核心技术 4.3 拦截器 ...