From eb72d788ef735628f186819771fa0c4e7111cddd Mon Sep 17 00:00:00 2001 From: Bruno Freitas Tissei <bft15@inf.ufpr.br> Date: Thu, 6 Jun 2019 21:17:07 -0300 Subject: [PATCH] Add MCAIRO and UVA10679_kmp Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br> --- algorithms/string/kmp.cpp | 2 +- algorithms/structure/bit2d.cpp | 2 + contests/Cadernaveis/MCAIRO.cpp | 74 +++++++++++++++++++++++++++ contests/Cadernaveis/UVA10679_kmp.cpp | 74 +++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 contests/Cadernaveis/MCAIRO.cpp create mode 100644 contests/Cadernaveis/UVA10679_kmp.cpp diff --git a/algorithms/string/kmp.cpp b/algorithms/string/kmp.cpp index dfc537c..241b64a 100644 --- a/algorithms/string/kmp.cpp +++ b/algorithms/string/kmp.cpp @@ -5,7 +5,7 @@ /// - search: O(n) /// Space: O(n + m) /// -/// Status: Tested (URI2350) +/// Status: Tested (URI2350,UVA10679) struct KMP { string patt; diff --git a/algorithms/structure/bit2d.cpp b/algorithms/structure/bit2d.cpp index fe8beb6..8a71df5 100644 --- a/algorithms/structure/bit2d.cpp +++ b/algorithms/structure/bit2d.cpp @@ -4,6 +4,8 @@ /// - update: O(log^2 n) /// - query: O(log^2 n) /// Space: O(n^2) +/// +/// Status: Tested (MCAIRO) struct BIT2D { int N, M; diff --git a/contests/Cadernaveis/MCAIRO.cpp b/contests/Cadernaveis/MCAIRO.cpp new file mode 100644 index 0000000..c65600e --- /dev/null +++ b/contests/Cadernaveis/MCAIRO.cpp @@ -0,0 +1,74 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +struct BIT2D { + int N, M; + vector<vector<int>> tree; + + BIT2D(int N, int M) : + N(N), M(M), tree(N, vector<int>(M)) + { init(); } + + void init() { + for (auto &i : tree) + fill(all(i), 0); + } + + int query(int idx, int idy) { + int sum = 0; + for (; idx > 0; idx -= (idx & -idx)) + for (int m = idy; m > 0; m -= (m & -m)) + sum = max(sum, tree[idx][m]); + return sum; + } + + void update(int idx, int idy, int val) { + for (; idx < N; idx += (idx & -idx)) + for (int m = idy; m < M; m += (m & -m)) + tree[idx][m] = max(tree[idx][m], val); + } +}; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int t; cin >> t; + for (int cas = 1; cas <= t; ++cas) { + BIT2D bit(1001, 1001); + + int n; cin >> n; + vector<int> x(n), y(n); + for (int i = 0; i < n; ++i) + cin >> x[i] >> y[i]; + + int ans = 0; + for (int i = 0; i < n; ++i) { + int val = 1 + bit.query(x[i], y[i]); + bit.update(x[i], y[i], val); + ans = max(ans, val); + } + + cout << ans << ende; + } + + return 0; +} diff --git a/contests/Cadernaveis/UVA10679_kmp.cpp b/contests/Cadernaveis/UVA10679_kmp.cpp new file mode 100644 index 0000000..380a1ac --- /dev/null +++ b/contests/Cadernaveis/UVA10679_kmp.cpp @@ -0,0 +1,74 @@ +#include <bits/stdc++.h> + +#define EPS 1e-6 +#define MOD 1000000007 +#define inf 0x3f3f3f3f +#define llinf 0x3f3f3f3f3f3f3f3f + +#define fi first +#define se second +#define pb push_back +#define ende '\n' + +#define all(x) (x).begin(), (x).end() +#define rall(x) (x).rbegin(), (x).rend() +#define mset(x, y) memset(&x, (y), sizeof(x)) + +using namespace std; + +using ll = long long; +using ii = pair<int,int>; + +struct KMP { + string patt; + vector<int> table; + + KMP(string patt) : + patt(patt), table(patt.size()+1) + { preprocess(); } + + void preprocess() { + fill(all(table), -1); + + for (int i = 0, j = -1; i < patt.size(); ++i) { + while (j >= 0 && patt[i] != patt[j]) + j = table[j]; + table[i + 1] = ++j; + } + } + + bool search(const string &txt) { + bool found = false; + + for (int i = 0, j = 0; i < txt.size(); ++i) { + while (j >= 0 && txt[i] != patt[j]) + j = table[j]; + j++; + + if (j == patt.size()) { + found = true; + j = table[j]; + } + } + + return found; + } +}; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int t; cin >> t; + for (int cas = 1; cas <= t; ++cas) { + string s; cin >> s; + int q; cin >> q; + for (int i = 0; i < q; ++i) { + string t; cin >> t; + KMP kmp(t); + cout << ((kmp.search(s)) ? "y" : "n") << ende; + } + } + + return 0; +} -- GitLab