字符串
kmp,trie,AC自动机,SA,SAM,Manacher
数学
同余,中国剩余定理
字符串
kmp
板 P3375 【模板】KMP
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define For (i , a , b ) for (int i= (a);i<= (b);i++ )
#define Ror (i , a , b ) for (int i= (a);i>= (b);i-- )
const int N = 1 e 6 + 10 ;
char s[N], s2[N];
int p2[N];
int len1, len2;
void kmp2 ()
{
int j = 0 ;
For (i, 2 , len1)
{
while (s2[j+ 1 ]!= s2[i]&& j) j = p2[j];
if (s2[j+ 1 ]== s2[i]) ++ j;
p2[i] = j;
}
}
void get ()
{
int j = 0 ;
For (i, 1 , len1)
{
while (s2[j+ 1 ]!= s[i]&& j) j = p2[j];
if (s2[j+ 1 ]== s[i]) ++ j;
if (j== len2)
{
cout << i- len2+ 1 << endl;
j = p2[j];
}
}
}
int main ()
{
scanf (" %s " , s+ 1 );
len1 = strlen (s+ 1 );
scanf (" %s " , s2+ 1 );
len2 = strlen (s2+ 1 );
kmp2 ();
get ();
For (i, 1 , len2) cout << p2[i] << ' ' ;
return 0 ;
}
Trie
板 P8306 【模板】字典树
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N = 3 e 6 + 10 ;
int T, n, q, tot, ans;
int d[N], a[N][80 ];
string ss;
void insert (string s )
{
int u = 0 ;
for (int i= 0 ;i< s.size ();i++ )
{
int ch = s[i]- 'A' + 17 ;
if (! a[u][ch]) a[u][ch] = ++ tot;
u = a[u][ch];
++ d[u];
}
}
void dfs (int u )
{
int t = u;
ans+= d[t];
for (int i= 0 ;i<= 75 ;i++ )
if (a[t][i])
dfs (a[t][i]);
}
void check (string s )
{
ans = 0 ;
int u = 0 ;
for (int i= 0 ;i< s.size ();i++ )
{
int ch = s[i]- 'A' + 17 ;
if (! a[u][ch]) return ;
u = a[u][ch];
}
ans = d[u];
}
int main ()
{
cin >> T;
while (T-- )
{
cin >> n >> q;
for (int i= 0 ;i<= tot;i++ )
for (int j= 0 ;j<= 75 ;j++ ) a[i][j] = d[i] = 0 ;
tot = 0 ;
getline (cin, ss);
for (int i= 1 ;i<= n;i++ )
{
getline (cin, ss);
insert (ss);
}
for (int i= 1 ;i<= q;i++ )
{
getline (cin, ss);
check (ss);
cout << ans << endl;
}
}
return 0 ;
}
AC自动机(一个文本串建trie和多个匹配串去向上靠)
板 P5357 【模板】AC 自动机
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N = 2 e 6 + 10 ;
const int Nm = 2 e 5 + 10 ;
int n, len, cnt;
int a[Nm][30 ];
int nxt[N], sum[N], ans[N], in[N];
char s[N];
vector<int> d[N];
queue<int> q;
void insert (int x )
{
int j = 0 ;
for (int i= 1 ;i<= len;i++ )
{
int ch = s[i]- 'a' + 1 ;
if (! a[j][ch]) a[j][ch] = ++ cnt;
j = a[j][ch];
}
d[j].push_back (x);
}
void get_fail ()
{
for (int i= 1 ;i<= 26 ;i++ ) if (a[0 ][i]) q.push (a[0 ][i]);
while (! q.empty ())
{
int x = q.front ();
q.pop ();
for (int i= 1 ;i<= 26 ;i++ )
{
if (a[x][i])
{
nxt[a[x][i]] = a[nxt[x]][i];
++ in[nxt[a[x][i]]];
q.push (a[x][i]);
}
else a[x][i] = a[nxt[x]][i];
}
}
}
void AC ()
{
int j = 0 ;
for (int i= 1 ;i<= len;i++ )
{
int ch = s[i]- 'a' + 1 ;
while (! a[j][ch]&& j) j = nxt[j];
j = a[j][ch];
if (j) ++ sum[j];
}
}
void bfs ()
{
for (int i= 1 ;i<= cnt;i++ ) if (! in[i]) q.push (i);
while (! q.empty ())
{
int x = q.front ();q.pop ();
for (int i= 0 ;i<int (d[x].size ());i++ ) ans[d[x][i]] = sum[x];
sum[nxt[x]]+= sum[x];
-- in[nxt[x]];
if (! in[nxt[x]]) q.push (nxt[x]);
}
}
int main ()
{
cin >> n;
for (int i= 1 ;i<= n;i++ )
{
cin >> (s+ 1 );
len = strlen (s+ 1 );
insert (i);
}
get_fail ();
cin >> (s+ 1 );
len = strlen (s+ 1 );
AC ();
bfs ();
for (int i= 1 ;i<= n;i++ ) cout << ans[i] << endl;
return 0 ;
}
SA
板P3809 【模板】后缀排序
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N = 1 e 6 + 10 ;
int n, m;
char s[N];
int x[N], y[N], c[N], sa[N];
void get_sa ()
{
for (int i= 1 ;i<= n;i++ ) ++ c[x[i] = s[i]];
for (int i= 2 ;i<= m;i++ ) c[i]+= c[i- 1 ];
for (int i= n;i>= 1 ;i-- ) sa[c[x[i]]-- ] = i;
for (int k= 1 ;k<= n;k<<= 1 )
{
int num = 0 ;
for (int i= n- k+ 1 ;i<= n;i++ ) y[++ num] = i;
for (int i= 1 ;i<= n;i++ ) if (sa[i]> k) y[++ num] = sa[i]- k;
for (int i= 1 ;i<= m;i++ ) c[i] = 0 ;
for (int i= 1 ;i<= n;i++ ) ++ c[x[y[i]]];
for (int i= 2 ;i<= m;i++ ) c[i]+= c[i- 1 ];
for (int i= n;i>= 1 ;i-- ) sa[c[x[y[i]]]-- ] = y[i];
swap (x, y);
x[sa[1 ]] = num = 1 ;
for (int i= 2 ;i<= n;i++ ) x[sa[i]] = (y[sa[i]]== y[sa[i- 1 ]]&& y[sa[i]+ k]== y[sa[i- 1 ]+ k])? num:++ num;
m = num;
if (num>= n) break ;
}
}
int main ()
{
cin >> s+ 1 ;
n = strlen (s+ 1 );
m = 127 ;
get_sa ();
for (int i= 1 ;i<= n;i++ ) cout << sa[i] << ' ' ;
return 0 ;
}
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N = 1 e 6 + 10 ;
string s;
int n, k = 1 ;
int rk[N], sa[N], tmp[N];
bool cmp (int i , int j )
{
if (rk[i]!= rk[j]) return rk[i]< rk[j];
int ti = i+ k> n?- 1 : rk[i+ k];
int tj = j+ k> n?- 1 : rk[j+ k];
return ti< tj;
}
void get_sa ()
{
for (int i= 0 ;i<= n;i++ )
{
rk[i] = s[i]- 'a' + 1 ;
sa[i] = i;
}
for (;k<= n;(k<<= 1 ))
{
sort (sa, sa+ n, cmp);
tmp[sa[0 ]] = 0 ;
for (int i= 0 ;i< n;i++ ) tmp[sa[i+ 1 ]] = tmp[sa[i]]+ (cmp (sa[i], sa[i+ 1 ])? 1 : 0 );
for (int i= 0 ;i< n;i++ ) rk[i] = tmp[i];
}
}
int main ()
{
getline (cin, s);
n = s.size ();
get_sa ();
for (int i= 0 ;i< n;i++ ) cout << sa[i]+ 1 << ' ' ;
return 0 ;
}
SAM
板 P3804 【模板】后缀自动机(SAM)
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
#define For (i , a , b ) for (int i= (a);i<= (b);i++ )
#define Ror (i , a , b ) for (int i= (a);i>= (b);i-- )
const int N = 2 e 6 + 10 ;
int last = 1 , cnt = 1 , ccnt;
long long Ans;
int len[N], fa[N], head[N], tot[N], a[N][30 ];
char c[N];
struct Node {
int to, next;
}g[N<< 1 ];
void add (int x , int y )
{
g[++ ccnt].to = y;
g[ccnt].next = head[x];
head[x] = ccnt;
}
void insert (int ch )
{
int p = last, u = last = ++ cnt;len[u] = len[p]+ 1 ;
tot[u] = 1 ;
for (;! a[p][ch]&& p;p= fa[p]) a[p][ch] = u;
if (! p) return fa[u] = 1 , void ();
int q = a[p][ch];
if (len[q]== len[p]+ 1 ) return fa[u] = q, void ();
int uu = ++ cnt;len[cnt] = len[p]+ 1 ;
memcpy (a[uu], a[q], sizeof (a[q]));
fa[uu] = fa[q];fa[q] = fa[u] = uu;
for (;a[p][ch]== q&& p;p= fa[p]) a[p][ch] = uu;
}
void find_endpos (int x )
{
for (int i= head[x];i;i= g[i].next)
{
find_endpos (g[i].to);
tot[x]+= tot[g[i].to];
}
if (x!= 1 && tot[x]!= 1 ) Ans = max (Ans, 1 ll* tot[x]* len[x]);
}
int main ()
{
cin >> c+ 1 ;
int lenth = strlen (c+ 1 );
For (i, 1 , lenth)
{
insert (int (c[i]- 'a' + 1 ));
}
For (i, 2 , cnt) add (fa[i], i);
find_endpos (1 );
cout << Ans << endl;
return 0 ;
}
! len[u](或当前匹配到的字符个数now)-len[fa]可以代表当前任意点的子串个数
!每个点的endpos个数可以通过dfs计算,一开始建SAM时u点的size=1,uu点的size=0,建好后dfs每个点的size+=各个子节点(每个节点作为fa的子)的size,最终的size即为各个点的endpos个数
Manacher
板 P3805 【模板】manacher
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
const int N = 1 e 7 + 1 e 6 + 10 ;
int n, cnt, R, C, ans = 1 ;
int p[N<< 1 ];
char s[N], S[N<< 1 ];
void init ()
{
S[++ cnt] = '&' ;
for (int i= 1 ;i<= n;i++ ) S[++ cnt] = '#' , S[++ cnt] = s[i];
S[++ cnt] = '#' ;
S[++ cnt] = '$' ;
}
void manacher ()
{
for (int i= 1 ;i<= cnt;i++ )
{
if (i<= R) p[i] = min (p[(C<< 1 )- i], R- i);
else p[i] = 1 ;
while (S[i- p[i]]== S[i+ p[i]]) ++ p[i];
ans = max (ans, p[i]- 1 );
if (p[i]+ i> R)
{
R = p[i]+ i;
C = i;
}
}
}
int main ()
{
cin >> s+ 1 ;
n = strlen (s+ 1 );
init ();
manacher ();
cout << ans << endl;
return 0 ;
}
数学
同余
板 P1082 同余方程
C++ 复制 #include <bits/stdc++.h>
using namespace std ;
typedef long long LL;
int ex_gcd (LL a , LL b , LL & x , LL & y )
{
if (! b){x = 1 , y = 0 ;return a;}
int g = ex_gcd (b, a% b, x, y);
int tmp = x;
x = y;
y = tmp - a/ b* y;
return g;
}
LL q, t, a, p, k, x, y, mod, ans = 1 ;
int main ()
{
a = read (), p = read ();
int g = ex_gcd (a, p, x, y);
write ((x% p+ p)% p);
puts ("" );
return 0 ;
}
中国剩余定理