| | |
How to optimize this function(about pattern match,A problem in a Interview)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 6
Question is :
Write a function 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!
Write a function
c++ Syntax (Toggle Plain Text)
int f(char* str, long pattern)
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!
>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.
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.
New members chased away this month: 4
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
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
![]() |
Other Threads in the C++ Forum
- Previous Thread: difficulty in providing delay in gcc compiler
- Next Thread: Interface in DOS
Views: 529 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






