working on the last problem for A3
This commit is contained in:
parent
4d4fcd7c00
commit
ba2d3e9bf6
69
LEXISORT/main.py
Normal file
69
LEXISORT/main.py
Normal file
@ -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()
|
||||||
0
MAIN8_C/MAIN8_C.cpp
Executable file → Normal file
0
MAIN8_C/MAIN8_C.cpp
Executable file → Normal file
BIN
MAIN8_C/a.out
Normal file
BIN
MAIN8_C/a.out
Normal file
Binary file not shown.
@ -13,7 +13,7 @@ int main() {
|
|||||||
while (test_cases != 0) {
|
while (test_cases != 0) {
|
||||||
|
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
int i = 1;
|
int i = 0;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
std::vector<size_t> positions;
|
std::vector<size_t> positions;
|
||||||
|
|
||||||
@ -22,12 +22,10 @@ int main() {
|
|||||||
std::string test_case = test_case2;
|
std::string test_case = test_case2;
|
||||||
std::string token = token2;
|
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;
|
||||||
|
|
||||||
i += pos + 1;
|
|
||||||
test_case.erase(0, pos + 1);
|
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,11 +34,16 @@ int main() {
|
|||||||
} else {
|
} else {
|
||||||
std::printf("%d\n", count);
|
std::printf("%d\n", count);
|
||||||
for (size_t pos : positions) {
|
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--;
|
test_cases--;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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():
|
def main():
|
||||||
test_cases = int(input())
|
test_cases = int(input())
|
||||||
|
|
||||||
while (test_cases != 0):
|
while (test_cases != 0):
|
||||||
test_case, token = input().split()
|
test_case, token = input().split()
|
||||||
|
positions = kmp(test_case, token)
|
||||||
|
|
||||||
if token in test_case:
|
if len(positions) == 0:
|
||||||
|
|
||||||
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:
|
|
||||||
print("Not Found", end='')
|
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
|
test_cases -= 1
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
BIN
NAJPF/main
Executable file → Normal file
BIN
NAJPF/main
Executable file → Normal file
Binary file not shown.
153
STRMATCH.cpp
Normal file
153
STRMATCH.cpp
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
#include <queue>
|
||||||
|
#include <math.h>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
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<int> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user