|
|
вернуться в форумProblem with 8 is... 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... When I was using strings,I had MLE 8. Bit in my AC programm I used arrays of char. Re: Problem with 8 is... 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... Послано ASK 3 мар 2010 21:31 Try to preallocate strings: int n; cin >> n >> ws; string str(n,' '); getline(cin, str); |
|
|