查找字符串中重复字符的最长子字符串 [英] Finding the longest substring of repeating characters in a string

查看:384
本文介绍了查找字符串中重复字符的最长子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(这是此codeforce问题的基础

除非我确实真的卡住了,否则我尽量不寻求代码强制问题的帮助。

I try not to get help with codeforces problems unless i'm really, really, stuck, which happens to be now.


您的第一个任务是找到火星数据库的密码。为此,您的最佳机密人员已经发现了以下事实:

密码是给定字符串的子字符串,该字符串由一系列非递减数字组成
密码长
密码始终为回文
回文是向后读取相同内容的字符串。赛车,鲍勃和中午都是著名的例子。

鉴于这些事实,您可以找到数据库的所有可能的密码吗?

输入
第一行包含n,即输入字符串的长度(1≤n≤105)。

下一行包含长度为n的字符串。该字符串的每个字符都是一个数字。

字符串中的数字按非降序排列。

输出
在第一行中,打印可能的密码数k。

在接下来的k行中,按字母顺序打印可能的密码。
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.

我的观察结果是:


  1. 回文

  1. A palindrome in a non-decreasing string is simply a string of repeating characters (eg. "4444" or "11" )

在字符上,非递减字符串中的字符串只是重复字符的字符串(例如 4444或 11)。 i ,i的最后一个实例-i的第一个实例+1 =重复字符的长度

on character i, the last instance of i - the first instance of i +1 = length of the repeating character

最大密码长度,然后过滤出所有长度小于最大密码长度的项目,以确保输出的密码具有最大长度

Keeping track of the max password length and then filtering out every item that is shorter than the max password length guarantees that the passwords outputted are of max length



根据这些观察,我的解决方案是:

my solution based on these observations is:

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个测试用例上失败。我不了解代码有什么问题,因此无法修复。有人可以帮忙吗?

unfortunately, this solution is wrong, in fact and fails on the 4th test case. I do not understand what is wrong with the code, and so i cannot fix it. Can someone help with this?

感谢您的阅读!

推荐答案

您的程序将失败:

4
0011

它将仅返回 11

问题在于 str(int('00'))等于1。

您可以通过删除 int str 从程序中调用(即,将答案另存为字符串而不是整数)。

You could fix it by removing the int and str calls from your program (i.e. saving the answers as strings instead of ints).

这篇关于查找字符串中重复字符的最长子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆