Skip to content

Commit 11ce5cd

Browse files
committed
reupload String Algorithms
1 parent 89c31bb commit 11ce5cd

17 files changed

+1619
-0
lines changed

src/1110.cpp

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
6+
template<typename A, typename B> print_op(pair<A, B>) { return out << "(" << u.first << ", " << u.second << ")"; }
7+
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> print_op(T_container) { out << "{"; string sep; for (const T &x : u) out << sep << x, sep = ", "; return out << "}"; }
8+
template<typename T> void dbg_out(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
9+
template<typename T, typename... Args> void dbg_out(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
10+
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; dbg_out(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
11+
#ifdef LOCAL
12+
#define dbg(...) dbg_out(#__VA_ARGS__, __VA_ARGS__)
13+
#else
14+
#define dbg(...)
15+
#endif
16+
17+
#define ar array
18+
#define ll long long
19+
#define ld long double
20+
#define sz(x) ((int)x.size())
21+
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
22+
#define all(a) (a).begin(), (a).end()
23+
24+
const int MAX_N = 1e5 + 5;
25+
const int MAX_L = 20;
26+
const int MAX_C = 26;
27+
const ll MOD = 1e9 + 7;
28+
const ll INF = 1e9;
29+
const ld EPS = 1e-9;
30+
31+
using ull = unsigned long long;
32+
33+
struct PolyHash {
34+
static const ull MOD = (1ULL << 61) - 1;
35+
static int BASE;
36+
37+
static inline ull add(ull a, ull b) {
38+
a += b;
39+
if (a >= MOD)
40+
a -= MOD;
41+
return a;
42+
}
43+
44+
static inline ull sub(ull a, ull b) {
45+
a -= b;
46+
if (a >= MOD)
47+
a += MOD;
48+
return a;
49+
}
50+
51+
static inline ull mul(ull a, ull b) {
52+
ull l1 = (uint32_t) a, h1 = a >> 32, l2 = (uint32_t) b, h2 = b >> 32;
53+
ull l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
54+
ull ret = (l & MOD) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
55+
ret = (ret & MOD) + (ret >> 61);
56+
ret = (ret & MOD) + (ret >> 61);
57+
return ret - 1;
58+
}
59+
60+
vector<ull> power, pref;
61+
62+
PolyHash(const string &s) : pref(s.length() + 1) {
63+
while ((int) power.size() <= (int) s.length())
64+
power.push_back(power.empty() ? 1 : mul(power.back(), BASE));
65+
66+
for (int i = 0; i < (int) s.length(); i++)
67+
pref[i + 1] = add(mul(pref[i], BASE), s[i]);
68+
}
69+
70+
ull operator () (int pos, int len) const {
71+
return sub(pref[pos + len], mul(pref[pos], power[len]));
72+
}
73+
};
74+
75+
int gen() {
76+
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
77+
mt19937 gen(seed ^ ull(new ull));
78+
uniform_int_distribution<int> dist(258, 2e9 - 1);
79+
int base = dist(gen);
80+
return base % 2 == 0 ? base - 1 : base;
81+
}
82+
83+
int PolyHash::BASE = gen();
84+
85+
void solve(int tc = 0) {
86+
string s; cin >> s;
87+
int n = sz(s);
88+
s += s;
89+
auto hs = PolyHash(s);
90+
91+
// compare 2 substrings s[a...a+n-1] and s[b...b+n-1] in log(n) time complexity
92+
auto cmp = [&](const int &a, const int &b) -> bool {
93+
if (s[a] != s[b]) return s[a] < s[b];
94+
if (hs(a, n) == hs(b, n)) return false;
95+
int lo = 1, hi = n;
96+
while (lo + 1 < hi) {
97+
int mid = (lo + hi) / 2;
98+
if (hs(a, mid) == hs(b, mid)) lo = mid;
99+
else hi = mid;
100+
}
101+
return s[a + hi - 1] < s[b + hi - 1];
102+
};
103+
104+
int mn = 0;
105+
for (int i = 1; i < n; i++) {
106+
if (cmp(i, mn)) {
107+
mn = i;
108+
}
109+
}
110+
cout << s.substr(mn, n) << "\n";
111+
}
112+
113+
signed main() {
114+
ios_base::sync_with_stdio(false); cin.tie(NULL);
115+
int tc = 1;
116+
// cin >> tc;
117+
for (int t = 1; t <= tc; t++) {
118+
// cout << "Case #" << t << ": ";
119+
solve(t);
120+
}
121+
}

src/1111.cpp

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
6+
template<typename A, typename B> print_op(pair<A, B>) { return out << "(" << u.first << ", " << u.second << ")"; }
7+
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> print_op(T_container) { out << "{"; string sep; for (const T &x : u) out << sep << x, sep = ", "; return out << "}"; }
8+
template<typename T> void dbg_out(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
9+
template<typename T, typename... Args> void dbg_out(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
10+
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; dbg_out(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
11+
#ifdef LOCAL
12+
#define dbg(...) dbg_out(#__VA_ARGS__, __VA_ARGS__)
13+
#else
14+
#define dbg(...)
15+
#endif
16+
17+
#define ar array
18+
#define ll long long
19+
#define ld long double
20+
#define sz(x) ((int)x.size())
21+
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
22+
#define all(a) (a).begin(), (a).end()
23+
24+
const int MAX_N = 1e6 + 5;
25+
const int MAX_L = 20;
26+
const int MAX_C = 26;
27+
const ll MOD = 1e9 + 7;
28+
const ll INF = 1e9;
29+
const ld EPS = 1e-9;
30+
31+
int p[2 * MAX_N + 3];
32+
33+
string manacher(const string &s) {
34+
memset(p, 0, sizeof p);
35+
string t = "$";
36+
for (char c : s)
37+
t += '#', t += c;
38+
t += "#@";
39+
40+
int c = 0, r = 0;
41+
for (int i = 1; i < (int) t.size() - 1; i++) {
42+
int mirr = 2 * c - i;
43+
if (i < r)
44+
p[i] = min(p[mirr], r - i);
45+
while (t[i + (p[i] + 1)] == t[i - (p[i] + 1)])
46+
p[i]++;
47+
if (p[i] + i > r) {
48+
c = i;
49+
r = i + p[i];
50+
}
51+
}
52+
53+
int start = 0, mx = 0;
54+
for (int i = 0; i < sz(t); i++) {
55+
if (p[i] > mx) {
56+
if (i % 2) {
57+
start = (i / 2 - 1) - p[i] / 2 + 1;
58+
} else {
59+
start = (i / 2 - 1) - p[i] / 2;
60+
}
61+
mx = p[i];
62+
}
63+
}
64+
65+
return s.substr(start, mx);
66+
}
67+
68+
void solve(int tc = 0) {
69+
string s; cin >> s;
70+
cout << manacher(s) << "\n";
71+
}
72+
73+
signed main() {
74+
ios_base::sync_with_stdio(false); cin.tie(NULL);
75+
int tc = 1;
76+
// cin >> tc;
77+
for (int t = 1; t <= tc; t++) {
78+
// cout << "Case #" << t << ": ";
79+
solve(t);
80+
}
81+
}

src/1112.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
6+
template<typename A, typename B> print_op(pair<A, B>) { return out << "(" << u.first << ", " << u.second << ")"; }
7+
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> print_op(T_container) { out << "{"; string sep; for (const T &x : u) out << sep << x, sep = ", "; return out << "}"; }
8+
template<typename T> void dbg_out(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
9+
template<typename T, typename... Args> void dbg_out(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
10+
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; dbg_out(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
11+
#ifdef LOCAL
12+
#define dbg(...) dbg_out(#__VA_ARGS__, __VA_ARGS__)
13+
#else
14+
#define dbg(...)
15+
#endif
16+
17+
#define ar array
18+
#define ll long long
19+
#define ld long double
20+
#define sz(x) ((int)x.size())
21+
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
22+
#define all(a) (a).begin(), (a).end()
23+
24+
const int MAX_N = 1e5 + 5;
25+
const int MAX_L = 20;
26+
const int MAX_C = 26;
27+
const ll MOD = 1e9 + 7;
28+
const ll INF = 1e9;
29+
const ld EPS = 1e-9;
30+
31+
void add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; }
32+
ll mul(ll a, ll b) { return (ll) a * b % MOD; }
33+
34+
int n, m, pf[MAX_N], nxt[MAX_N][MAX_C];
35+
string s;
36+
37+
void preprocess() {
38+
pf[0] = 0;
39+
for (int i = 1; i < m; i++) {
40+
int j = pf[i - 1];
41+
while (j > 0 && s[i] != s[j])
42+
j = pf[j - 1];
43+
pf[i] = j + (s[i] == s[j]);
44+
}
45+
46+
for (int i = 0; i <= m; i++) {
47+
for (int c = 0; c < MAX_C; c++) {
48+
char cc = (char) ('A' + c);
49+
int j = (i == m ? pf[m - 1] : i);
50+
while (j > 0 && (s[j] != cc))
51+
j = pf[j - 1];
52+
nxt[i][c] = j + (s[j] == cc);
53+
}
54+
}
55+
}
56+
57+
void solve(int tc = 0) {
58+
cin >> n >> s; m = sz(s);
59+
preprocess();
60+
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
61+
dp[0][0] = 1;
62+
for (int i = 0; i < n; i++)
63+
for (int j = 0; j < m; j++)
64+
for (int c = 0; c < MAX_C; c++)
65+
add(dp[i + 1][nxt[j][c]], dp[i][j]);
66+
int ans = 0, pw = 1;
67+
for (int i = n; i >= 0; i--) {
68+
add(ans, mul(dp[i][m], pw));
69+
pw = mul(pw, 26);
70+
}
71+
cout << ans << "\n";
72+
}
73+
74+
signed main() {
75+
ios_base::sync_with_stdio(false); cin.tie(NULL);
76+
int tc = 1;
77+
// cin >> tc;
78+
for (int t = 1; t <= tc; t++) {
79+
// cout << "Case #" << t << ": ";
80+
solve(t);
81+
}
82+
}

src/1731.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define print_op(...) ostream& operator<<(ostream& out, const __VA_ARGS__& u)
6+
template<typename A, typename B> print_op(pair<A, B>) { return out << "(" << u.first << ", " << u.second << ")"; }
7+
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> print_op(T_container) { out << "{"; string sep; for (const T &x : u) out << sep << x, sep = ", "; return out << "}"; }
8+
template<typename T> void dbg_out(string s, T x) {cerr << "\033[1;35m" << s << "\033[0;32m = \033[33m" << x << "\033[0m\n";}
9+
template<typename T, typename... Args> void dbg_out(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
10+
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << "\033[1;35m" << s.substr(0, i) << "\033[0;32m = \033[33m" << x << "\033[31m | "; dbg_out(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
11+
#ifdef LOCAL
12+
#define dbg(...) dbg_out(#__VA_ARGS__, __VA_ARGS__)
13+
#else
14+
#define dbg(...)
15+
#endif
16+
17+
#define ar array
18+
#define ll long long
19+
#define ld long double
20+
#define sz(x) ((int)x.size())
21+
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
22+
#define all(a) (a).begin(), (a).end()
23+
24+
const int MAX_N = 1e5 + 5;
25+
const int MAX_L = 20;
26+
const int MAX_C = 26;
27+
const ll MOD = 1e9 + 7;
28+
const ll INF = 1e9;
29+
const ld EPS = 1e-9;
30+
31+
struct Node {
32+
Node *ch[MAX_C];
33+
int cnt;
34+
Node(): cnt(0), ch() {}
35+
Node *init(int x) {
36+
if (!ch[x]) ch[x] = new Node();
37+
return ch[x];
38+
}
39+
};
40+
41+
void add(struct Node* tr, const string &s) {
42+
Node* u = tr;
43+
for (char c : s) {
44+
u = u->init(c - 'a');
45+
}
46+
u->cnt++;
47+
}
48+
49+
void solve(int tc = 0) {
50+
string s; cin >> s;
51+
int n = sz(s);
52+
int q; cin >> q;
53+
auto tr = new Node();
54+
while (q--) {
55+
string t; cin >> t;
56+
add(tr, t);
57+
}
58+
vector<int> dp(n + 1);
59+
dp[0] = 1;
60+
for (int i = 0; i < n; i++) {
61+
auto cur = tr;
62+
for (int j = i; j < n; j++) {
63+
if (!cur->ch[s[j] - 'a']) break;
64+
cur = cur->ch[s[j] - 'a'];
65+
if (cur->cnt) dp[j + 1] = (dp[j + 1] + dp[i]) % MOD;
66+
}
67+
}
68+
cout << dp[n] << "\n";
69+
}
70+
71+
signed main() {
72+
ios_base::sync_with_stdio(false); cin.tie(NULL);
73+
int tc = 1;
74+
// cin >> tc;
75+
for (int t = 1; t <= tc; t++) {
76+
// cout << "Case #" << t << ": ";
77+
solve(t);
78+
}
79+
}

0 commit comments

Comments
 (0)