HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Optimizing string match algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
matchstringoptimizingalgorithm

Problem

I was trying to solve this problem. I thought of any possible relation between the number of matches among subsequent substrings, but could not find one. So, the probably naive solution that I drafted was:

#include 
#include 

using namespace std;

int n;
char str[1000000];

int sim(int i, int len)
{
    int j=0;
    for(; i>n;
    for(int i=0; i>str;
        solve();
    }

    return 0;
}


Now submission to online judge at interviewstreet says that I did pass 7/10 testcases but the time limit exceeded for the 8th testcase! It means that my solution isn't even that naive, but it also indicates a need for some optimization. Can you people please suggest any way to optimize it?

Solution

I think what you need is a suffix tree: http://en.wikipedia.org/wiki/Suffix_tree. I don't really remember much about it, except that it exists and seems relevant to the problem at hand.

Style wise, I recommend spaces around operators like < and << and +=

Context

StackExchange Code Review Q#6757, answer score: 5

Revisions (0)

No revisions yet.