D

2026 Spring 第三次上机

public
dsb Mar 26, 2026 Never 122
Clone
Python 全在其中.py 19 lines (18 loc) | 358 Bytes
1
def issub(s,t):
2
i=j=0
3
while i<len(s) and j<len(t):
4
if s[i]==t[j]:
5
i=i+1
6
j=j+1
7
else:
8
j=j+1
9
if i==len(s):
10
return 'Yes'
11
else:
12
return 'No'
13
14
while True:
15
try:
16
s,t=map(str,input().split())
17
print(issub(s,t))
18
except EOFError:
19
break
Python 字符串乘方.py 27 lines (23 loc) | 641 Bytes
1
def make_next(s: str):
2
next_array = [None for i in range(len(s))]
3
i, k = 0, -1
4
next_array[i] = k
5
while i < len(s) - 1:
6
while k >= 0 and s[i] != s[k]:
7
k = next_array[k]
8
k += 1
9
i += 1
10
next_array[i] = k
11
return next_array
12
13
while True:
14
s = input()
15
if s == ".":
16
break
17
18
next_array = make_next(s+"-")
19
length = len(s)
20
pre_suffix_length = next_array[len(s)]
21
22
if length % (length - pre_suffix_length) != 0:
23
print(1)
24
else:
25
print("%d" % (length / (length - pre_suffix_length)))
26
27
next_array = None
Python 堆栈基本操作.py 16 lines (16 loc) | 394 Bytes
This code takes an integer input `n` followed by a sequence of integers. It then simulates a stack using two lists (`remain` and `stack`) and checks if the given sequence can be formed using a stack with push and pop operations. Here is a breakdown of the code: - The integer `n` is taken as input which represents the number of elements in the sequence. - The sequence of integers is taken as input and stored in the list `seq`. - A list `remain` is initialized with numbers from 1 to `n`. - An empty list `stack` is initialized to simulate the stack operations. - An empty list `results` is initialized to store the steps taken (i.e., push and pop operations). The main logic of the code is as follows: 1. For each number `n` in the input sequence: - Iterate over the numbers in `remain` list while `n` is greater than or equal to the first number in the `remain` list. - Pop the first number from `remain` and push it onto the `stack`. - Append the operation (PUSH) to the `results` list. - If the `stack` is empty or the top element of the `stack` is not equal to `n`, print "NO" and exit. - Otherwise, pop the top element from the `stack` and append the operation (POP) to the `results` list. 2. For each step in the `results` list, print the step. Overall, the code checks whether the given sequence can be formed using a stack by performing push and pop operations. If the sequence cannot be formed or the stack operations are invalid, it will print "NO", otherwise, it will print the steps taken to form the sequence.
1
n = int(input())
2
seq = list(map(int, input().split()))
3
remain = [k for k in range(1, n+1)]
4
stack = []
5
results = []
6
for n in seq:
7
while len(remain) > 0 and n >= remain[0]:
8
k = remain.pop(0)
9
stack.append(k)
10
results.append("PUSH %d" % k)
11
if len(stack) == 0 or stack[-1] != n:
12
print("NO")
13
exit()
14
results.append("POP %d" % stack.pop(-1))
15
for step in results:
16
print(step)