- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我知道标题很糟糕,但在我知道我的问题的答案之前,我想不出更好的标题。如果可以,请编辑。
我在一个 OnlineJudge 网站上(为了好玩)解决了一个非常简单的问题。问题是这样的:
Input: a single string containing lowercase latin letters. The length of string is at least 1 and at most 100.
Output: a single number which is the length of the longest substring of the input string which occurs at least twice in that string( the occurrences may overlap).
示例输入: ababa
示例输出: 3
我通过以下代码获得了接受:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string s;
std::cin >> s;
int max = 0;
typedef std::string::const_iterator sit;
sit end = s.end();
for(sit it1 = s.begin(); it1 != end; ++it1)
for(sit it2 = it1 + 1; it2 != end; ++it2)
max = std::max(max, std::mismatch(it1, it1 + (end - it2), it2).first - it1);
std::cout << max;
}
但是,我得到测试 42 运行时错误(我不知道那是什么输入 - 站点规则),下面的代码与第一个。
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
string s;
cin >> s;
vector<size_t> dif;
for(string::const_iterator it1 = s.begin(); it1 != s.end(); ++it1)
for(string::const_iterator it2 = it1 + 1; it2 != s.end(); ++it2)
dif.push_back(mismatch(it1, it1 + (s.end() - it2), it2).first - it1);
cout << *max_element(dif.begin(), dif.end());
}
半小时的仪式舞蹈后,我放弃了。我无法弄清楚第二个代码有什么问题(除了它的效率和可读性稍差)。是不是我要从 iterator
中减去 const_iterator
?还是因为 int 与 size_t?代码是用 MSVC8.0 或 9.0 编译的(在他们的网站上)。 Release模式。有任何想法吗?谢谢。
最佳答案
如果不运行您的代码,我认为您的第二个解决方案无法处理长度为 1 的输入字符串。
当输入字符串的长度为 1 时,您的 dif
vector 为空,这会导致 *max_element(dif.begin(), dif.end())
失败。
关于c++ - 至少出现两次的最长子串 : C++ question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6641475/
我需要一个带有条件的正则表达式: 最少 6 个字符,最多 50 个字符 必须包含 1 个字母 必须包含 1 个数字 可能包含特殊字符,例如!@#$%^&*()_+ 目前我有模式:(?!^[0-9]*$
我想检查密码是否至少包含一个字母和一个数字。接受特殊字符但不是必需的... 那将是一个简单的密码检查器。 最佳答案 您可以使用先行断言来检查任何数字和任何字母的存在,如下所示: ^(?=.*[a-zA
我需要找到生产至少三种不同型号电脑的制造商。结果集:制造商、型号数量。 我的 table 是产品(制造商、型号、类型)PC(代码、型号、速度、内存、高清、CD、价格) 我现在已经这样做了 SELECT
在我正在处理的哈希字典中,根据 GDB,数组的大小已从 500 调整到 1000。它在尝试添加时崩溃的数组索引是 799,所以它没有超出范围......我不是确定它为什么会出现段错误,尤其是在这样一条
我需要一个正则表达式来检查字符串的长度是否至少为 10 个字符。这些字符是什么并不重要。 谢谢 最佳答案 您可以使用: .{10,} 自 .默认情况下不匹配换行符,您必须使用合适的修饰符(如果您的正则
我有以下代码: val num1: Int? = someObject.number val num2: Int? = anotherObject?.anotherNumber val numToFa
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我一直在进行查询以获取用户列表及其各自的组。我知道了。 但是有一些限制: 如果用户只是 group5 的成员,则不应出现在列表中 如果是 group5 和其他组的成员,它应该出现在列表中 我可以获得不
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我需要忽略任何一个空格,并且应该匹配至少大于一个空格的空格... "MARY HAD A LITTLE LAMB" 我希望 "MARY", "HAD A LITTLE", "
如何在仅数字验证中允许空格(至少 8 位数字)?当允许空格时,输入电话号码会更容易。例如0400 123 456、9699 1234。 到目前为止,这是我的代码,我只进行了最少 8 位数字验证: jQ
我想将以下内容重写为 HTML 模式: if (/\S/.test(myString)) { // string is not empty and not just whitespace }
我的想法是使用 php 或 js 创建大量的 div。 (我从昨天开始写js。) 所以我自己给定的任务是使用 php 或 js 或两者生成一个 div 网格。到目前为止的想法是有一个通用的 div 作
显示字符串中至少有 10 个数字字符的正则表达式。 可以多于 10 个,但不能少于 10 个。在随机位置可以有任意数量的其他字符,将数字分隔开。 示例数据: (123) 456-7890 123-45
我目前在 Firefox 中使用边框动画时遇到问题,从无到 50px。 Chrome 非常好,但 Firefox 不是,我想同样的问题也会出现在 Opera 中。 这里有一个 fiddle 来向您展示
作为 ruby 挑战训练的一部分,我被要求创建一个方法 substrings(string),它将接受一个字符串,并返回一个包含所有子字符串的数组。 即 substrings("cat") #=>
我目前有这段代码,它检查数组中的所有元素是否相同。如果是这种情况,则返回 true def all_equal(lst): """ >>> all_equal([1,1,1,1,1,1,1])
当我注意到 Libre Office 电子表格显示远低于 2^53 的数字的错误值时,我正在做一些计算以计划改进我的主要筛选器的实现,这是 FoxPro 和许多其他精确整数计算的限制内部使用 C do
我有一个复选框,我想检查用户是否填写了它后面的文本框。必须至少包含 5 个字符才能选中复选框。 pro)) echo 'checked' ?>> pro?>">Please supply your I
我正在为我的食谱设计一个数据库。我在我的设计中创建了多个表格:书籍、作者、食谱、成分,对于所有这些项目,我想将媒体(图像或视频)链接到所有这些表格中的项目。 我在想这样的设计: media_id, r
我是一名优秀的程序员,十分优秀!