mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-22 07:01:46 +00:00
Compare commits
2 Commits
f41c1341a0
...
78bac3ae8a
| Author | SHA1 | Date | |
|---|---|---|---|
| 78bac3ae8a | |||
| 8406b59ba4 |
88
src/8/20/P2312.cpp
Normal file
88
src/8/20/P2312.cpp
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
#include <cctype>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using i128 = __int128_t;
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
static ll n, m;
|
||||||
|
static const ll mod = 1e9+7;
|
||||||
|
static std::vector<std::vector<ll>> a;
|
||||||
|
|
||||||
|
static inline constexpr ll strp(const std::string &s, ll p) {
|
||||||
|
ll num = 0;
|
||||||
|
int idx = 0;
|
||||||
|
ll nag = 1;
|
||||||
|
if (s[0] == '-') {
|
||||||
|
nag = -1;
|
||||||
|
idx = 1;
|
||||||
|
}
|
||||||
|
for (size_t i = idx; i < s.length(); ++i) {
|
||||||
|
num = (num * 10 + (s[i] - '0')) % p;
|
||||||
|
}
|
||||||
|
num = (num * nag) % p;
|
||||||
|
if (num < 0) {
|
||||||
|
num += p;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
std::ios_base::sync_with_stdio(false);
|
||||||
|
std::cin.tie(NULL);
|
||||||
|
|
||||||
|
std::cin >> n >> m;
|
||||||
|
a.resize(n + 1, std::vector<ll>(1));
|
||||||
|
for (int i = 0; i <= n; ++i) {
|
||||||
|
std::string tmpstr;
|
||||||
|
std::cin >> tmpstr;
|
||||||
|
for (size_t k = 0; k < 1; ++k) {
|
||||||
|
a[i][k] = strp(tmpstr,mod);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<ll> sol;
|
||||||
|
sol.reserve(100);
|
||||||
|
|
||||||
|
for (ll i = 1; i <= m; ++i) {
|
||||||
|
bool isok = true;
|
||||||
|
for (size_t k = 0; k < 1; ++k) {
|
||||||
|
|
||||||
|
const ll p = mod;
|
||||||
|
const auto &cur = a;
|
||||||
|
|
||||||
|
ll curx = 1;
|
||||||
|
i128 sum = 0;
|
||||||
|
|
||||||
|
for (int j = 0; j <= n; ++j) {
|
||||||
|
|
||||||
|
sum += (i128)cur[j][k] * curx;
|
||||||
|
curx = (curx * i) % p;
|
||||||
|
|
||||||
|
if ((j & 15) == 15) {
|
||||||
|
sum %= p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sum %= p;
|
||||||
|
|
||||||
|
if (sum != 0) {
|
||||||
|
isok = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isok) {
|
||||||
|
sol.push_back(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << sol.size() << '\n';
|
||||||
|
for (ll ans : sol) {
|
||||||
|
std::cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
72
src/8/21/kmp.cpp
Normal file
72
src/8/21/kmp.cpp
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::vector<int> compute_next(const std::string &p) {
|
||||||
|
int m = p.length();
|
||||||
|
std::vector<int> next(m, 0);
|
||||||
|
|
||||||
|
for (int i = 1, j = 0; i < m; ++i) {
|
||||||
|
|
||||||
|
while (j > 0 && p[i] != p[j]) {
|
||||||
|
j = next[j - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p[i] == p[j]) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
|
||||||
|
next[i] = j;
|
||||||
|
}
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<int> kmp_search(const std::string &text, const std::string &p) {
|
||||||
|
int n = text.length();
|
||||||
|
int m = p.length();
|
||||||
|
|
||||||
|
if (m == 0)
|
||||||
|
return {};
|
||||||
|
if (n < m)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
std::vector<int> next = compute_next(p);
|
||||||
|
std::vector<int> res;
|
||||||
|
|
||||||
|
for (int i = 0, j = 0; i < n; ++i) {
|
||||||
|
while (j > 0 && text[i] != p[j]) {
|
||||||
|
j = next[j - 1];
|
||||||
|
}
|
||||||
|
if (text[i] == p[j]) {
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j == m) {
|
||||||
|
res.push_back(i - m + 1);
|
||||||
|
j = next[j - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
std::string text = "ABABDABACDABABCABAB";
|
||||||
|
std::string p = "ABABCABAB";
|
||||||
|
|
||||||
|
std::cout << "Text: " << text << std::endl;
|
||||||
|
std::cout << "p: " << p << std::endl;
|
||||||
|
|
||||||
|
std::vector<int> ans1 = kmp_search(text, p);
|
||||||
|
|
||||||
|
if (ans1.empty()) {
|
||||||
|
std::cout << "p not found " << std::endl;
|
||||||
|
} else {
|
||||||
|
std::cout << "p found at ";
|
||||||
|
for (size_t i = 0; i < ans1.size(); ++i) {
|
||||||
|
std::cout << ans1[i] << (i == ans1.size() - 1 ? "" : ", ");
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user