working on the last problem for A3
This commit is contained in:
1 parent
4d4fcd7c00
commit
ba2d3e9bf6
7 files changed
+304
-18
No files matched your search
+11
-8
@@ -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<size_t> 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--;
|
||||
}
|
||||
|
||||
+71
-10
@@ -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__':
|
||||
|
||||
Executable → Regular
BIN
Binary file not shown.
Reference in new issue
Block a user