数算!
This commit is contained in:
1 parent
ca6daa582c
commit
e0c09e0c7b
22 files changed
+1344
-105
No files matched your search
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
|
||||
bool is_within(const std::string& str, const std::string& substr) {
|
||||
int i = 0, j = 0;
|
||||
for (; i < str.size() && j < substr.size(); i++) {
|
||||
if (str[i] == substr[j]) { j++; }
|
||||
}
|
||||
return j == substr.size();
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
std::string substr, str;
|
||||
while (std::cin >> substr >> str) {
|
||||
std::cout << (is_within(str, substr) ? "Yes" : "No") << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const std::vector<int>& vector) {
|
||||
for (const auto elem : vector) {
|
||||
os << elem << ' ';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
std::vector<int> prefix_function(const std::string& s) {
|
||||
std::vector<int> pi(s.size());
|
||||
for (int i = 1; i < s.size(); i++) {
|
||||
int j = pi[i - 1];
|
||||
while (true) {
|
||||
if (s[i] == s[j]) {
|
||||
pi[i] = j + 1;
|
||||
break;
|
||||
}
|
||||
if (j == 0) {
|
||||
pi[i] = 0;
|
||||
break;
|
||||
}
|
||||
j = pi[j - 1];
|
||||
}
|
||||
}
|
||||
return pi;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::string s;
|
||||
while (std::cin >> s && s != ".") {
|
||||
auto pi = prefix_function(s);
|
||||
std::clog << pi << std::endl;
|
||||
int period = static_cast<int>(s.size()) - pi.back();
|
||||
if (s.size() % period != 0) {
|
||||
std::cout << 1 << std::endl;
|
||||
} else {
|
||||
std::cout << s.size() / period << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
#include <regex>
|
||||
|
||||
bool matches(std::string s, std::string pattern) {
|
||||
int i = 0, j = 0;
|
||||
bool within_bracket = false;
|
||||
while (true) {
|
||||
if (!within_bracket) {
|
||||
if (pattern[j] == '[') {
|
||||
within_bracket = true;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
if (s[i] != pattern[j]) { return false; }
|
||||
i++, j++;
|
||||
} else {
|
||||
bool matched = false;
|
||||
while (pattern[j] != ']') {
|
||||
matched = s[i] == pattern[j] || matched;
|
||||
within_bracket = false;
|
||||
j++;
|
||||
}
|
||||
if (!matched) { return false; }
|
||||
i++, j++;
|
||||
}
|
||||
if (i == s.size() && j == pattern.size()) { return true; }
|
||||
if (i == s.size() || j == pattern.size()) { return false; }
|
||||
}
|
||||
throw std::logic_error("What the fuck is this piece of WCJ doing?");
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
int n;
|
||||
std::string pattern;
|
||||
std::cin >> n;
|
||||
std::vector<std::string> strs(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cin >> strs[i];
|
||||
}
|
||||
std::cin >> pattern;
|
||||
for (char& c : pattern) {
|
||||
if (std::isupper(c)) { c -= 'A' - 'a'; }
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::string s = strs[i];
|
||||
for (char& c : s) {
|
||||
if (std::isupper(c)) { c -= 'A' - 'a'; }
|
||||
}
|
||||
if (matches(s, pattern)) {
|
||||
std::cout << i + 1 << ' ' << strs[i] << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
std::vector<int> prefix_function(const std::string& s) {
|
||||
std::vector<int> pi(s.size());
|
||||
for (int i = 1; i < s.size(); 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;
|
||||
}
|
||||
|
||||
std::vector<int> find_substring_pos(const std::string& target, const std::string& pattern) {
|
||||
std::vector<int> matched_pos;
|
||||
const std::vector<int> pattern_prefix_values = prefix_function(pattern);
|
||||
int j = 0;
|
||||
for (int i = 0; i < target.size(); i++) {
|
||||
while (j > 0 && target[i] != pattern[j]) {
|
||||
j = pattern_prefix_values[j - 1];
|
||||
}
|
||||
if (target[i] == pattern[j]) { j++; }
|
||||
if (j == pattern.size()) {
|
||||
matched_pos.push_back(i - static_cast<int>(pattern.size()) + 1);
|
||||
j = pattern_prefix_values[j - 1];
|
||||
}
|
||||
}
|
||||
return matched_pos;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::string target, pattern;
|
||||
std::cin >> target >> pattern;
|
||||
for (const auto i : find_substring_pos(target, pattern)) {
|
||||
std::cout << i + 1 << std::endl;
|
||||
}
|
||||
for (const auto val : prefix_function(pattern)) {
|
||||
std::cout << val << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user