forked from ShahjalalShohag/code-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefix Automaton.cpp
69 lines (64 loc) · 1.62 KB
/
Prefix Automaton.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define eb emplace_back
#define ll long long
#define nl '\n'
#define deb(x) cerr<<#x" = "<<x<<nl
#define in() ( { int a ; scanf("%d",&a); a; } )
const int N = 1e5 + 9;
const int mod = 1e9 + 7;
vector<int> prefix_function(string &s) {
int n = (int)s.size();
vector<int> pi(n, 0);
for (int i = 1; i < n; i++) {
int j = pi[i-1];
while (j > 0 && s[i] != s[j]) j = pi[j-1];
if (s[i] == s[j]) j++;
pi[i] = j;
}
return pi;
}
int aut[N][26];
void compute_automaton(string s)
{
s += '#';
int n = (int)s.size();
vector<int> pi = prefix_function(s);
for (int i = 0; i < n; i++) {
for (int c = 0; c < 26; c++) {
if (i > 0 && 'a' + c != s[i]) aut[i][c] = aut[pi[i-1]][c];
else aut[i][c] = i + ('a' + c == s[i]);
}
}
}
vector< vector<int> > dp;
int n, m;
string s, p;
int yo(int i, int j)
{
if(i == n) return (j == m);
int &ret = dp[i][j];
if(ret != -1) return ret;
ret = 0;
if(s[i] == '?'){
for(int c=0; c<26; c++){
ret = max(ret, yo(i+1, aut[j][c]));
}
}
else ret = yo(i+1, aut[j][s[i]-'a']);
ret += j == m;
return ret;
}
///change '?'s in string s so that the number of occurrences of p is maximized
int32_t main()
{
char ch[N]; scanf("%s", ch); s = ch;
scanf("%s", ch); p = ch;
compute_automaton(p);
n = s.size(); m = p.size();
dp.resize(n, vector<int>(m+1, -1));
cout<<yo(0, 0)<<nl;
return 0;
}