Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6125945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:18:33+00:00 2026-05-23T16:18:33+00:00

The title is horrible, I know, but until I know the answer to my

  • 0

The title is horrible, I know, but until I know the answer to my question, I can’t think of a better one. If you can, please edit.

I was solving (for fun) a very easy problem on one of OnlineJudge sites. The problem is this:

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).

Sample input: ababa

Sample output: 3

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;
}

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());
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:18:34+00:00Added an answer on May 23, 2026 at 4:18 pm

    Without running your code, I think your second solution fails on input strings of length 1.

    Your dif vector is empty whenever the input string has length 1, which causes *max_element(dif.begin(), dif.end()) to fail.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Horrible title, I know. I want to have some kind of wordwrap, but obviously
Horrible title I know, horrible question too. I'm working with a bit of software
Horrible title of this question, I know. Sorry. I have four divs similar to
Title edits that reflect a better summary of the question are welcome. I'd like
The title is horrible, i know; I'm terrible at titles on SO here. I'm
I kind of know how preg_replace works but I am horrible with regex. Here
I had a horrible time wording the title of this post, but basically, I
Apologies for the horrible title but I've no idea how to describe this easily.
Well, I apologize for the horrible question title. I am not a SQL or
The title is horrible but that's the best I could do. What I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.