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

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

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

 
0
  #1
Jul 16th, 2008
Question is :
Write a function
  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!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 721
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #2
Jul 16th, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 42
Reputation: littlestone is an unknown quantity at this point 
Solved Threads: 6
littlestone littlestone is offline Offline
Light Poster

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

 
0
  #3
Jul 16th, 2008
Thank you!

I don't know the answer of the second question: if pattern is a array, how to optimize it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,652
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 721
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
1
  #4
Jul 16th, 2008
>I don't know the answer of the second question:
>if pattern is a array, how to optimize it?
The answer is the same.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

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

 
0
  #5
Jul 16th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC