字符串最长重复子串python_查找字符串中重复字符的最长子字符串

我尽量不在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个测试用例中失败了。我不明白代码有什么问题,所以我无法修复它。有人能帮忙吗?在

谢谢你的阅读!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值