943,627 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 709
  • C++ RSS
Jul 16th, 2008
0

How to optimize this function(about pattern match,A problem in a Interview)

Expand Post »
Question is :
Write a function
c++ Syntax (Toggle Plain Text)
  1. int f(char* str, long pattern)
to find out how many pattern does the str contain? str is a char array consist of '1' and '0';
for example: str = "11010101110101011110100011001" ,pattern = 110;

My solution is change pattern and str to std::string, then use std::string::find to search.
but I think this solution is not efficient.

1.Could you tell me other efficient solution? thanks.

2. if the second parameter is a array, how to optimize it?

Thank you very much!
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Jul 16th, 2008
1

Re: How to optimize this function(about pattern match,A problem in a Interview)

>but I think this solution is not efficient.
Why? If you're just guessing or using your "programmer's intuition", you're probably wrong. Interviewers (good ones) tend to smile more on people who aren't afraid to say "I'd rather optimize clean, correct code than fix fast code". That's the sign of an experienced programmer.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 16th, 2008
0

Re: How to optimize this function(about pattern match,A problem in a Interview)

Thank you!

I don't know the answer of the second question: if pattern is a array, how to optimize it?
Reputation Points: 14
Solved Threads: 6
Light Poster
littlestone is offline Offline
42 posts
since Mar 2008
Jul 16th, 2008
1

Re: How to optimize this function(about pattern match,A problem in a Interview)

>I don't know the answer of the second question:
>if pattern is a array, how to optimize it?
The answer is the same.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 16th, 2008
0

Re: How to optimize this function(about pattern match,A problem in a Interview)

if the pattern matching is done on a very long sequence of characters (eg. searching for the occurrence of a particular phrase or word in a large book), there are several interesting algorithms. here is a link that explores some of them. http://www.jea.acm.org/ARTICLES/Vol4Nbr2/index.html
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: difficulty in providing delay in gcc compiler
Next Thread in C++ Forum Timeline: Interface in DOS





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC