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最大 ...
随机推荐
- 51nod 1073约瑟夫环 递归公式法
约瑟夫环问题的原来描述为,设有编号为1,2,--,n的n(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,再从他的下一个人起重新报数,报到m时停止报数,报m的出圈,- ...
- SwiftUI render WKWebView
SwiftUI render WKWebView // // ContentView.swift // webview-app // // Created by 夏凌晨 on 2020/10/27. ...
- JSON-LD & SEO
JSON-LD & SEO https://json-ld.org/ https://en.wikipedia.org/wiki/JSON-LD Google Search structure ...
- macOS & PostgreSQL
macOS & PostgreSQL macOS 上安装 PostgreSQL 后为什么会自动创建一个系统用户账号 https://get.enterprisedb.com/postgresq ...
- MDN & JavaScript 文档翻译状态
MDN & JavaScript 文档翻译状态 https://developer.mozilla.org/zh-CN/docs/MDN/Doc_status/JavaScript refs ...
- 中文域名 & 原理剖析
中文域名 & 原理剖析 https://zh.wikipedia.org/wiki/中文域名 原理 利用浏览器的对字符编码的转换算法,实现 unicode 字符显示! 国际化域名: 就是一个普 ...
- 新三板 & 挂牌费用
新三板 & 挂牌费用 关于拟申请公司股票在全国中小企业股份转让系统终止挂牌的提示性公告 https://pilu.tianyancha.com/announcement/ef51e981910 ...
- clientHeight & offsetHeight & scrollHeight
clientHeight & offsetHeight & scrollHeight scrollWidth/scrollHeight,offsetWidth/offsetHeight ...
- vue:表单验证时,trigger的值什么时候选blur什么时候选change
对el-input输入框的验证,trigger的值选blur,即失去焦点时进行验证. 下拉框(el-select).日期选择器(el-date-picker).复选框(el-checkbox).单选框 ...
- 导入Excel时,如果有多个投料信息,则循环导入
List<Input> list = new ArrayList<Input>();for (int j = 0; j < 500; ) { String materia ...