(这是基础这个代码强制问题)
我尽量不寻求有关 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
0011
它只会返回11
.
问题是长度str(int('00'))
等于 1。
您可以通过删除来修复它int
and str
来自程序的调用(即将答案保存为字符串而不是整数)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)