diff --git a/LEXISORT/main.py b/LEXISORT/main.py new file mode 100644 index 0000000..c34f9c3 --- /dev/null +++ b/LEXISORT/main.py @@ -0,0 +1,69 @@ +def merge(l: list, start: int, end: int, end2: int) -> None: + + n1 = end - start + 1 + n2 = end2 - end + + left = [0] * (n1) + right = [0] * (n2) + + for i in range(n1): + left[i] = l[start + i] + + for i in range(n2): + right[i] = l[end + 1 + i] + + i = 0 + j = 0 + k = start + + while i < n1 and j < n2: + if left[i] <= right[j]: + l[k] = left[i] + i += 1 + else: + l[k] = right[j] + j += 1 + k += 1 + + while i < n1: + l[k] = left[i] + j += 1 + k += 1 + + while j < n2: + l[k] = right[j] + j += 1 + k += 1 + +def merge_sort(l: list, left: int, right: int) -> None: + if left < right: + + m = left + (right - 1) // 2 + + merge_sort(l, left, m) + merge_sort(l, m + 1, right) + merge(l, left, m, right) + + +def main(): + lines: list[str] = [] + test_cases = int(input()) + while (test_cases != 0): + n = int(input()) + + while (n != 0): + + lines.append(input()) + + n -= 1 + + merge_sort(lines, 0, len(lines) - 1) + for line in lines: + print(line) + + lines = [] + + test_cases -= 1 + +if __name__ == "__main__": + main() diff --git a/MAIN8_C/MAIN8_C.cpp b/MAIN8_C/MAIN8_C.cpp old mode 100755 new mode 100644 diff --git a/MAIN8_C/a.out b/MAIN8_C/a.out new file mode 100644 index 0000000..58725c6 Binary files /dev/null and b/MAIN8_C/a.out differ diff --git a/NAJPF/NAJPF.cpp b/NAJPF/NAJPF.cpp index bcfaede..14c4ff1 100644 --- a/NAJPF/NAJPF.cpp +++ b/NAJPF/NAJPF.cpp @@ -13,7 +13,7 @@ int main() { while (test_cases != 0) { size_t pos = 0; - int i = 1; + int i = 0; int count = 0; std::vector positions; @@ -22,12 +22,10 @@ int main() { std::string test_case = test_case2; std::string token = token2; - while ((pos = test_case.find(token)) != std::string::npos) { + while ((pos = test_case.find(token, i)) != std::string::npos) { + positions.push_back(pos + 1); - positions.push_back(pos + i); - - i += pos + 1; - test_case.erase(0, pos + 1); + i = pos + 1; count++; } @@ -36,11 +34,16 @@ int main() { } else { std::printf("%d\n", count); for (size_t pos : positions) { - std::printf("%d ", pos); + if (pos != positions.back()) { + std::printf("%d ", pos); + } else { + std::printf("%d", pos); + } } } - std::printf("\n\n"); + if (test_cases != 1) + std::printf("\n"); test_cases--; } diff --git a/NAJPF/NAJPF.py b/NAJPF/NAJPF.py index 7c25750..14a3e70 100644 --- a/NAJPF/NAJPF.py +++ b/NAJPF/NAJPF.py @@ -1,20 +1,81 @@ +def kmp_table(S: str, W: str) -> list[int]: + T = [0] * len(S) + + pos = 1 + cnd = 0 + + while pos < len(W): + if W[pos] == W[cnd]: + cnd += 1 + T[pos] = cnd + pos += 1 + else: + if cnd != 0: + cnd = T[cnd - 1] + else: + T[pos] = 0 + pos += 1 + + return T + +# Returns a list of positions (indexes) +def kmp(S: str, W: str) -> list[int]: + + if W not in S: + return [] + + j = 0 + k = 0 + T = kmp_table(S, W) + P = [] # Positions List + + while j < len(S): + if W[k] == S[j]: + j += 1 + k += 1 + if k == len(W): + P.append(j - k) + k = T[k - 1] + else: + if k != 0: + k = T[k - 1] + else: + j += 1 + + return P + +def find_search(S: str, W: str) -> list[int]: + pos = 0 + i = 0 + count = 0 + positions = [] + while (pos := S.find(W, i)) != -1: + i = pos + 1 + positions.append(pos + 1) + count += 1 + + return positions + def main(): test_cases = int(input()) + while (test_cases != 0): test_case, token = input().split() + positions = kmp(test_case, token) - if token in test_case: - - i = 1 - while token in test_case: - print(f'{test_case.find(token) + i} ',end='') - test_case = test_case.replace(token, '', 1) - i += len(token) - - else: + if len(positions) == 0: print("Not Found", end='') + else: + print(len(positions)) + for pos in positions: + if pos != positions[-1]: + print(f'{pos + 1} ', end='') + else: + print(f'{pos + 1}', end='') + + if test_cases != 1: + print('\n') - print('\n') test_cases -= 1 if __name__ == '__main__': diff --git a/NAJPF/main b/NAJPF/main old mode 100755 new mode 100644 index 05d9d22..a19cca7 Binary files a/NAJPF/main and b/NAJPF/main differ diff --git a/STRMATCH.cpp b/STRMATCH.cpp new file mode 100644 index 0000000..aaa5dbd --- /dev/null +++ b/STRMATCH.cpp @@ -0,0 +1,153 @@ +#include +#include +#include +#include +#include + +const int MAXS = 500000; +const int MAXC = 26; + + +int out[MAXS]; +int f[MAXS]; +int g[MAXS][MAXC]; + +int build_string_matcher(std::string arr[], int k) { + std::memset(out, 0, sizeof out); + std::memset(g, -1, sizeof g); + + int states = 1; + + for (int i = 0; i < k; ++i) + { + const std::string &word = arr[i]; + int currentState = 0; + + for (int j = 0; j < word.size(); ++j) + { + int ch = word[j] - 'a'; + + + if (g[currentState][ch] == -1) + g[currentState][ch] = states++; + + currentState = g[currentState][ch]; + } + + out[currentState] |= (1 << i); + } + + for (int ch = 0; ch < MAXC; ++ch) + if (g[0][ch] == -1) + g[0][ch] = 0; + + std::memset(f, -1, sizeof f); + + std::queue q; + + for (int ch = 0; ch < MAXC; ++ch) + { + if (g[0][ch] != 0) + { + f[g[0][ch]] = 0; + q.push(g[0][ch]); + } + } + + while (q.size()) + { + int state = q.front(); + q.pop(); + + for (int ch = 0; ch <= MAXC; ++ch) + { + if (g[state][ch] != -1) + { + int failure = f[state]; + + while (g[failure][ch] == -1) + failure = f[failure]; + + failure = g[failure][ch]; + f[g[state][ch]] = failure; + + out[g[state][ch]] |= out[failure]; + + q.push(g[state][ch]); + } + } + } + + return states; +} + +int find_next_state(int currentState, char nextInput) +{ + int answer = currentState; + int ch = nextInput - 'a'; + + + while (g[answer][ch] == -1) + answer = f[answer]; + + return g[answer][ch]; +} + +void search_words(std::string queries[], int k, std::string text) { + build_string_matcher(queries, k); + + int current_state = 0; + + int counts[k]; + + std::memset(counts, 0, sizeof counts); + + for (int i = 0; i < text.size(); i++) { + current_state = find_next_state(current_state, text[i]); + + if (out[current_state] == 0) + continue; + + for (int j = 0; j < k; j++) { + if (out[current_state] & (1 << j)) { + counts[j]++; + } + } + } + + for (int i = 0; i < k; i++) { + std::printf("%d\n", counts[i]); + } +} + + +int main() { + int N, Q; + std::string S, query; + + std::scanf("%d %d", &N, &Q); + + char c_s[N]; + std::fscanf(stdin, "%s", c_s); + S = c_s; + + std::string queries[Q]; + int i = 0; + int q_copy = Q; + char c_query[3000]; + while(q_copy != 0) { + + std::fscanf(stdin, "%s", c_query); + query = c_query; + queries[i] = query; + + i++; + q_copy--; + } + + search_words(queries, Q, S); + + return 0; +} + +