一、题目
Input Format
- The first line contains a single string s.
- The second line contains an integer k, the length of each substring.
Consider the following:
A string s of length n
An integer k where k is a factor of n,
We can split s into n/k substrings where each substring t consists of a contiguouse block of k chararcters in s. Then, use each t to create an string u which has all distinct characters.
Example
s = 'AAABCADDE'
k=3
There are three substring of length 3 to consider: 'AAA','BCA' and 'DDE'.
- The first substring is all 'A' characters. so u1 = 'A'.
- The second substring has all distinct characters, so u2 = 'BCA'.
- The third substring has 2 different characters. so u3 = 'DE'.
Note that a subsequence maintains the original order of characters encountered.
The order of characters in each subsequence shown is important.
Prints
Print each subsequence on a new line. There will be n/k of them. No return value is expected.
Sample Input
Sample Output
二、代码
def merge_the_tools(string, k):
for i in range(len(string) // k):
start_index = i * k
end_index = start_index + k
substr = string[start_index:end_index]
result = []
for char in list(substr):
if char not in result:
result.append(char)
print(''.join(result))
if __name__ == '__main__':
string, k = input(), int(input())
merge_the_tools(string, k)
三、解读
这段代码实现将字符串分割成多个子字符串去重打印的任务。