Skip to content

Commit 0fa6968

Browse files
committed
Random stuff
1 parent dce8a21 commit 0fa6968

File tree

5 files changed

+102
-13
lines changed

5 files changed

+102
-13
lines changed

CCO/CCC 20-searching/one-hash.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
const ll P1 = 9973, P2 = 69420, M1 = 1e9 + 9, M2 = 998244353;
6+
7+
int freq_target[26], freq_curr[26];
8+
string s, t;
9+
10+
int main() {
11+
cin.tie(0)->sync_with_stdio(0);
12+
cin >> s >> t;
13+
if (s.size() > t.size()) return cout << 0, 0;
14+
15+
set<pair<ll, ll>> good;
16+
ll poly1 = 0, poly2 = 0, p_pow1 = 1, p_pow2 = 1;
17+
for (int i = 0; i < s.size(); i++) {
18+
freq_target[s[i] - 'a']++, freq_curr[t[i] - 'a']++;
19+
poly1 = (poly1 * P1 + (t[i] - 'a')) % M1;
20+
poly2 = (poly2 * P2 + (t[i] - 'a')) % M2;
21+
p_pow1 = p_pow1 * P1 % M1;
22+
p_pow2 = p_pow2 * P2 % M2;
23+
}
24+
{
25+
bool match = true;
26+
for (int j = 0; j < 26; j++) match &= freq_curr[j] == freq_target[j];
27+
if (match) good.insert({poly1, poly2});
28+
}
29+
30+
for (int i = s.size(); i < t.size(); i++) {
31+
freq_curr[t[i] - 'a']++;
32+
freq_curr[t[i - s.size()] - 'a']--;
33+
poly1 = (poly1 * P1 - p_pow1 * (t[i - s.size()] - 'a') % M1 + (t[i] - 'a') + M1) % M1;
34+
poly2 = (poly2 * P2 - p_pow2 * (t[i - s.size()] - 'a') % M2 + (t[i] - 'a') + M2) % M2;
35+
36+
bool match = true;
37+
for (int j = 0; j < 26; j++) match &= freq_curr[j] == freq_target[j];
38+
if (match) good.insert({poly1, poly2});
39+
}
40+
41+
cout << good.size() << '\n';
42+
return 0;
43+
}

CCO/CCC 20-searching/two-hash.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
typedef long long ll;
3+
using namespace std;
4+
5+
const ll P1 = 9973, P2 = 69420, M1 = 1e9 + 9, M2 = 998244353;
6+
7+
ll inv(ll base, ll MOD) {
8+
ll ans = 1, expo = MOD - 2;
9+
while (expo) {
10+
if (expo & 1) ans = ans * base % MOD;
11+
expo >>= 1;
12+
base = base * base % MOD;
13+
}
14+
return ans;
15+
}
16+
17+
string n, h;
18+
19+
int main() {
20+
cin.tie(0)->sync_with_stdio(0);
21+
cin >> n >> h;
22+
if (n.size() > h.size()) return cout << 0, 0;
23+
24+
set<pair<ll, ll>> good;
25+
ll n_hsh1 = 1, h_hsh1 = 1, n_hsh2 = 1, h_hsh2 = 1;
26+
ll poly1 = 0, poly2 = 0, p_pow1 = 1, p_pow2 = 1;
27+
for (int i = 0; i < n.size(); i++) {
28+
n_hsh1 = n_hsh1 * (P1 + n[i] - 'a') % M1;
29+
n_hsh2 = n_hsh2 * (P2 + n[i] - 'a') % M2;
30+
h_hsh1 = h_hsh1 * (P1 + h[i] - 'a') % M1;
31+
h_hsh2 = h_hsh2 * (P2 + h[i] - 'a') % M2;
32+
33+
poly1 = (poly1 * P1 + (h[i] - 'a')) % M1;
34+
poly2 = (poly2 * P2 + (h[i] - 'a')) % M2;
35+
p_pow1 = p_pow1 * P1 % M1;
36+
p_pow2 = p_pow2 * P2 % M2;
37+
}
38+
if (n_hsh1 == h_hsh1 && n_hsh2 == h_hsh2) good.insert({poly1, poly2});
39+
40+
for (int i = n.size(); i < h.size(); i++) {
41+
h_hsh1 = h_hsh1 * inv(P1 + h[i - n.size()] - 'a', M1) % M1 * (P1 + h[i] - 'a') % M1;
42+
h_hsh2 = h_hsh2 * inv(P2 + h[i - n.size()] - 'a', M2) % M2 * (P2 + h[i] - 'a') % M2;
43+
44+
poly1 = (poly1 * P1 - p_pow1 * (h[i - n.size()] - 'a') % M1 + (h[i] - 'a') + M1) % M1;
45+
poly2 = (poly2 * P2 - p_pow2 * (h[i - n.size()] - 'a') % M2 + (h[i] - 'a') + M2) % M2;
46+
47+
if (n_hsh1 == h_hsh1 && n_hsh2 == h_hsh2) good.insert({poly1, poly2});
48+
}
49+
50+
cout << good.size() << '\n';
51+
return 0;
52+
}

IOI/IOI 04-empodia.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ again, so we pop it from the stack
2222
*/
2323

2424
#include <bits/stdc++.h>
25-
#define FOR(i, x, y) for (int i = x; i < y; i++)
2625
using namespace std;
2726

2827
int a[2000000], le[2000000], gr[2000000];
@@ -33,12 +32,12 @@ int main() {
3332
int n;
3433
map<int, stack<int>> mp;
3534
cin >> n;
36-
FOR(i, 0, n) {
35+
for (int i = 0; i < n; i++) {
3736
cin >> a[i];
3837
mp[a[i] - i].push(i);
3938
}
4039
stack<int> stck;
41-
FOR(i, 0, n) {
40+
for (int i = 0; i < n; i++) {
4241
while (stck.size() && a[stck.top()] < a[i]) stck.pop();
4342
if (stck.size())
4443
gr[i] = stck.top();

POI/POI 11-lightning_conductor.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#include <bits/stdc++.h>
2-
#pragma GCC optimize("O3")
3-
#define FOR(i, x, y) for (ll i = x; i < y; i++)
4-
#define MOD 1000000007
5-
typedef long long ll;
62
using namespace std;
73

84
int n, h[500000], dp1[500000], dp2[500000];
@@ -14,7 +10,7 @@ double slope(int j, int k) {
1410

1511
void solve(int dp[]) {
1612
deque<int> q;
17-
FOR(i, 0, n) {
13+
for (int i = 0; i < n; i++) {
1814
// Keep the queue monotone
1915
if (!i || h[q.back()] < h[i]) {
2016
while ((q.size() > 0 &&
@@ -33,12 +29,13 @@ int main() {
3329
iostream::sync_with_stdio(false);
3430
cin.tie(0);
3531
cin >> n;
36-
FOR(i, 0, n) cin >> h[i];
32+
for (int i = 0; i < n; i++) cin >> h[i];
3733

3834
solve(dp1);
39-
FOR(i, 0, n / 2) swap(h[i], h[n - 1 - i]);
35+
for (int i = 0; i < n / 2; i++) swap(h[i], h[n - 1 - i]);
4036
solve(dp2);
4137

42-
FOR(i, 0, n) cout << max(dp1[i], dp2[n - 1 - i]) - h[n - 1 - i] << '\n';
38+
for (int i = 0; i < n; i++)
39+
cout << max(dp1[i], dp2[n - 1 - i]) - h[n - 1 - i] << '\n';
4340
return 0;
4441
}

POI/POI 17-sabotage.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include <bits/stdc++.h>
2-
#pragma GCC optimize("O3")
32
#define FOR(i, x, y) for (int i = x; i < y; i++)
4-
#define MOD 1000000007
53
typedef long long ll;
64
using namespace std;
75

0 commit comments

Comments
 (0)