ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1423. String Tale

Problem with 8 is...
Posted by Hanzbrow (TNU) KCC 9 Apr 2009 21:09
Why I've got MLE(19 MB). I use KMP and I don't understand why at all! I appeal to you for help...
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main() {
  int  i, j, f=0, n;
  string what_to_find, gde;
  cin>>n>>what_to_find>>gde;
  what_to_find+="#";
  what_to_find+=gde;
  what_to_find+=gde;
  gde="";
  vector<short> next(what_to_find.size());
  next[0]=0;
  for(i=1; i<what_to_find.size(); i++){
    if(what_to_find[i]==what_to_find[next[i-1]])
      next[i]=next[i-1]+1;
    else {
      j = next[i-1];
      while (j>0 && what_to_find[i]!=what_to_find[j]){
      j=next[j-1];
      if(what_to_find[i]==what_to_find[j])
        next[i]=j+1;
      }
    }
    if(next[i]==n) {
      f=1;
      break;
    }
  }

  if(f) cout<<i-2*n;
  else cout<<-1;
  return 0;

}
Re: Problem with 8 is...
Posted by Michael Hranik(VNTU) 24 Oct 2009 00:27
When I was using strings,I had MLE 8. Bit in my AC programm I used arrays of char.
Re: Problem with 8 is...
Posted by Alexey Shmelev [NNSU] 19 Jan 2010 01:12
I get MLE#8 too, using std::string but my AC program with arrays uses only 4.4 MB...
Admins, can you please explain such strange behaviour of the judge system?
Re: Problem with 8 is...
Posted by ASK 3 Mar 2010 21:31
Try to preallocate strings:

int n; cin >> n >> ws;
string str(n,' ');
getline(cin, str);