nxt

AC自动机处理多串匹配——cf1202E

浪尽此生 提交于 2019-11-29 13:36:07
si+sj中间有一个切割点,我们在t上枚举这个切割点i,即以t[i]作为最后一个字符时求有多少si可以匹配,以t[i+1]作为第一个字符时有多少sj可以匹配 那么对s串正着建一个ac自动机,反着建一个自动机,然后t正反各匹配一次,用sum[]数组记录t[i]作为最后一个字符可以匹配的串数量 注意:求sum数组时,暴力跳fail显然会t,考虑到跳fail是为了统计匹配串的后缀,那么我们在build时,就可以在处理fail指针时就可以把那个fail的end加到now的end上去,这样就避免了暴力跳fail #include<bits/stdc++.h> using namespace std; #define N 200005 struct Trie{ int nxt[N][26],fail[N],end[N]; int root,L; int newnode(){ memset(nxt[L],-1,sizeof nxt[L]); end[L]=0; return L++; } void init(){ L++; root=newnode(); } void insert(char buf[]){ int len=strlen(buf+1); int now=root; for(int i=1;i<=len;i++){ if(nxt[now][buf[i]-'a']==-1) nxt

树上启发式合并与dsu on tree

限于喜欢 提交于 2019-11-29 11:24:38
题目链接:https://codeforc.es/contest/600/problem/E 题目大意:给一颗以1为根的树,共n个点。点有颜色,颜色从1-n编号。问每颗子树中出现次数最多的颜色 的编号之和。 启发式合并做法: #include<bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/hash_policy.hpp> #define ll long long #define rep(i, a, b) for(int i = (a); i <= (b); i++) #define pb push_back using namespace std; using namespace __gnu_pbds; const int N = 1e5+1000; vector<int>nxt[N]; int siz[N],son[N]; void split(int u,int f) { son[u] = 0; siz[u] = 1; for(auto v:nxt[u]) { if(v==f)continue; split(v,u); siz[u] += siz[v]; if(siz[son[u]]<siz[v]) son[u] = v; } } //---------- ll val[N]

Separate String

北慕城南 提交于 2019-11-29 02:18:52
Separate String 时间限制: 1 Sec 内存限制: 128 MB 提交: 50 解决: 16 题目描述 You are given a string t and a set S of N different strings. You need to separate t such that each part is included in S. For example, the following 4 separation methods satisfy the condition when t=abab and S={a,ab,b}. a,b,a,b a,b,ab ab,a,b ab,ab Your task is to count the number of ways to separate t. Because the result can be large, you should output the remainder divided by 1,000,000,007. 输入 The input consists of a single test case formatted as follows. N s1 : sN t The first line consists of an integer N (1≤N≤100,000) which is the

P4149 [IOI2011]Race 点分治

对着背影说爱祢 提交于 2019-11-29 01:37:55
思路: 点分治 提交:5次 题解: 刚开始用排序+双指针写的,但是调了一晚上,总是有两个点过不了,第二天发现原因是排序时的 \(cmp\) 函数写错了:如果对于路径长度相同的,我们从小往大按边数排序,当双指针出现 \(==k\) 时,即我们应先左移右指针,否则答案可能会变劣(仔细想一想);若反着排序,应该先右移左指针。 #include<bits/stdc++.h> #define R register int using namespace std; namespace Luitaryi { template<class I> inline I g(I& x) { x=0; register I f=1; register char ch; while(!isdigit(ch=getchar())) f=ch=='-'?-1:f; do x=x*10+(ch^48); while(isdigit(ch=getchar())); return x*=f; } const int N=2e5+10,Inf=1e+9; int n,K,cnt,sum,rt,tot,ans=N; bool vis[N]; int vr[N<<1],nxt[N<<1],fir[N],w[N<<1],sz[N],d[N],f[N],b[N],mx[N],mem[N]; inline void add(int

POJ2778 AC自动机 + 快速矩阵幂

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 00:45:53
http://poj.org/problem?id=2778 做法:利用AC自动机建矩阵之后进行N次矩阵乘 关于AC自动机配快速矩阵幂的理解: 1.题目限制10个字符串长度最多为10,那么建出的AC自动机的结点数至多为100 2.任意合法字符串必定通过nxt指针在AC自动机的结点之间转移 3.那么我们只要求出每次结点之间转移的数量,建立一个矩阵,就可以通过快速矩阵幂优化了 4.对于不合法的结点(病毒),将特定的转移次数设定为0即可。 5.注意不合法的结点除了插入的时候字典树上不合法的结点之外,所有fail指针指向不合法结点的及其后缀都是不合法结点,因为fail指针指向结点是该节点的后缀 #include <map> #include <set> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <bitset> #include <cstdio> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> #include <functional> using

[笔记]点分治

一笑奈何 提交于 2019-11-28 22:47:25
基本思路:点分治,是一种针对 可带权树上简单路径统计问题 的算法。对于一个节点,只解决经过这棵子树的根节点的路径,对于子节点问题下推子树。 //当初的主要问题是vis[]在干什么qwq,终于知道了 #include<iostream> #include<cstdio> #include<algorithm> #define R register int using namespace std; #define ull unsigned long long #define ll long long #define pause (for(R i=1;i<=10000000000;++i)) #define In freopen("NOIPAK++.in","r",stdin) #define Out freopen("out.out","w",stdout) namespace Fread { static char B[1<<15],*S=B,*D=B; #ifndef JACK #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) #endif inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch

洛谷P5410 拓展KMP 模板题

流过昼夜 提交于 2019-11-28 19:31:36
洛谷P5410 拓展KMP 模板题 KMP算法大家应该都知道,拓展KMP顾名思义,就是在KMP算法上面的扩展和加难。 拓展KMP的经典题型就是:给你两个串,让你求一个串的后缀子串与另一个串的最长公共前缀LCP的长度(用ex数组存下) 具体理解可以参照刘雅琼前辈的扩展KMP的PPT https://wenku.baidu.com/view/64ac5384b9d528ea81c779ed.html 上代码 # pragma GCC optimize("O2") # include <bits/stdc++.h> # define ll long long using namespace std ; const int maxn = 2e6 + 7 ; char str [ maxn ] , tr [ maxn ] ; int l , li , nxt [ maxn ] , ex [ maxn ] ; //nxt实际上就是自己对自己的ex数组,ex就代表每一个后缀串对模板串的最长公共前缀(LCP)的长度 void ekmpgetnxt ( ) { int a = 0 , p = 0 ; //p代表最长匹配的长度,a代表开始匹配的位置 nxt [ 0 ] = l ; for ( int i = 1 ; i < l ; i ++ ) { if ( i >= p || i + nxt [ i

Bluetooth-connection between Android and Lego Mindstorm NXT

无人久伴 提交于 2019-11-28 17:15:24
问题 Does anybody know, how to build a bluetooth connection between Android and LEGO-Mindstorm-NXT? The connection between two NXTs works fine. But the other Connection-type likes not so easy. I am working with the LeJOS Firmware 0.85 and the Android SDK Tools (2.2 Froyo). 回答1: So i've solved it and will show all how does it works, because i've seen that a lot of people have problems with that. The class includes 4 functions: Bluetooth enable if not enabled before -> enableBT() Connect to 2 NXTs -

HDU5343 MZL's Circle Zhou(SAM+记忆化搜索)

馋奶兔 提交于 2019-11-28 16:37:56
Problem Description MZL's Circle Zhou is good at solving some counting problems. One day, he comes up with a counting problem: You are given two strings a , b which consist of only lowercase English letters. You can subtract a substring x (maybe empty) from string a and a substring y (also maybe empty) from string b , and then connect them as x + y with x at the front and y at the back. In this way, a series of new strings can be obtained. The question is how many different new strings can be obtained in this way. Two strings are different, if and only if they have different lengths or there

暑假D21

橙三吉。 提交于 2019-11-28 11:13:06
Jigsaw 九条可怜有n盒拼图从1开始编号,当一盒拼图的块数无法组成 任何 r块*c块的矩形图案时就认为它需要返厂补块。 可怜想知道编号在[l,r]内的拼图,如果选择k个一定需要补块的返厂,那么拼图块数最多的最少是多少。 有时候可怜会发现自己数错了,并将第x盒拼图块数更新成y。 对于100%的数据,1<=n,m,k<=2e5,所有拼图块数在任何时候都是[4,1e6]的整数。保证答案存在 输入格式 第一行两个整数 n,k,m ,m表示可怜询问和修改的数量。 接下来一行 n个正整数,第 i个数表示第 i盒拼图初始时的块数。 接下来 m行 ,每行 ,每3个数 opt,l,r 。为了表明你在即时回答可怜的询问,真 实的 opt,l,r 为输入的 opt,l,r 分别异或( XOR )lastans ,其中 lastans 表示上 一次询问的答案,若之前没有操作则 lastans=0 。 若 opt=1 ,则表示这是一次询问操作的区间为 [l,r] 。 若 opt=2 ,则表示这是一次修改操作把第 l盒拼图的块数修改为 r。 题解 为什么这道题要把输入格式专门弄出来呢,因为这里面有很多东西。 他是强制在线。 先找思路,对于题中的返厂可以推出只要是质数就需要,要是返厂的最大的最小直接贪心,从小的开始选,第k小就是答案。 那么就是求区间第k小,可以用主席树+值域线段树解决。但是他又要修改