Created
September 9, 2012 19:18
-
-
Save zxmarcos/3686638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| int subseq(std::string str, std::string pattern) | |
| { | |
| size_t lastpos = 0; | |
| size_t occur = 0; | |
| if (!str.size() || !pattern.size()) { | |
| cout << "Sem ocorrencias" << endl; | |
| return 0; | |
| } | |
| while (1) { | |
| lastpos = str.find(pattern[0], lastpos); | |
| if (lastpos == string::npos) | |
| break; | |
| lastpos++; | |
| bool isok = true; | |
| for (size_t i = 1, lastsub = lastpos; i < pattern.size(); i++) { | |
| lastsub = str.find(pattern[i], lastpos); | |
| if (lastsub == string::npos) { | |
| isok = false; | |
| } | |
| lastsub++; | |
| } | |
| if (isok) | |
| ++occur; | |
| } | |
| cout << "Ocorrencias " << occur << endl; | |
| return occur; | |
| } | |
| int main() | |
| { | |
| subseq("3141592653", "123"); | |
| subseq("31415926531323", "123"); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment