的标题是太可怕了,我知道,但直到我知道回答我的问题,我想不出一个更好的。如果可以,请编辑。
我在OnlineJudge网站的一个解决(好玩)一个非常简单的问题。问题是这样的:
I was solving (for fun) a very easy problem on one of OnlineJudge sites. The problem is this:
输入:包含一个字符串 小写拉丁字母。的长度 串为至少1且至多100 输出:单号也就是 的最长子串的长度 其发生在至少输入字符串 两次在该字符串(该事件可能会重叠)。
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).
样品输入:贝巴 示例输出: 3
Sample input: ababa Sample output: 3
我的的接受的有以下code:
I got Accepted with the following code:
#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运行时错误 (我不知道是什么输入的是 - 网站规则)具有以下code这不过是稍有不同的第一个。
However, I get Runtime Error on Test 42 (I can't know what input is that - site rules) with the following code which is but slightly different from the first one.
#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());
}
祭祀舞蹈半个多小时后,我放弃了。我想不出有什么不对的第二code(除非是的略少有效的可读性的事实)。难道是我减去一个常量性
从迭代
?或者是因为INT主场迎战为size_t的?在code编译(在他们的网站)与MSVC8.0或9.0。释放模式。有任何想法吗?谢谢你。
After half an hour of ritual dancing, I give up. I can't figure out what's wrong with the second code (except the fact that is's slightly less effective and less readable). Is it that I am substracting a const_iterator
from an iterator
? Or because of int vs. size_t? The code is compiled (on their site) with MSVC8.0 or 9.0. Release mode. Any ideas? Thanks.
在不运行您的code,我觉得长度为1的输入字符串你的第二个解决方案失败。
Without running your code, I think your second solution fails on input strings of length 1.
您 DIF
载体是空的,只要输入字符串长度为1,这会导致 * max_element(dif.begin(),dif.end( ))
失败。
Your dif
vector is empty whenever the input string has length 1, which causes *max_element(dif.begin(), dif.end())
to fail.