我尽量不在codeforces问题上寻求帮助,除非我真的,真的,卡住了,现在正好是。在Your first mission is to find the password of the Martian database. To achieve this, your best secret agents have already discovered the following facts:
The password is a substring of a given string composed of a sequence of non-decreasing digits
The password is as long as possible
The password is always a palindrome
A palindrome is a string that reads the same backwards. racecar, bob, and noon are famous examples.
Given those facts, can you find all possible passwords of the database?
Input
The first line contains n, the length of the input string (1 ≤ n ≤ 105).
The next line contains a string of length n. Every character of this string is a digit.
The digits in the string are in non-decreasing order.
Output
On the first line, print the number of possible passwords, k.
On the next k lines, print the possible passwords in alphabetical order.
我的观察是:非递减字符串中的回文是由重复字符组成的字符串(例如“4444”或“11”)
在字符i上,i的最后一个实例-i+1的第一个实例=重复字符的长度
跟踪最大密码长度,然后过滤掉小于最大密码长度的每个项目,以确保输出的密码为最大长度
基于这些观察,我的解决方案是:n,s = [input() for i in range(2)]#input
maxlength = 0
results = []
for i in s:
length = (s.rfind(i)-s.find(i))+1
if int(i*(length)) not in results and length>=maxlength:
results.append(int(i*(length)))
maxlength = length
#filer everything lower than the max password length out
results = [i for i in results if len(str(i))>=maxlength]
#output
print(len(results))
for y in results:
print(y)
不幸的是,这个解决方案是错误的,并且在第4个测试用例中失败了。我不明白代码有什么问题,所以我无法修复它。有人能帮忙吗?在
谢谢你的阅读!在