Skip to content

Instantly share code, notes, and snippets.

@zxmarcos
Created September 9, 2012 19:18
Show Gist options
  • Select an option

  • Save zxmarcos/3686638 to your computer and use it in GitHub Desktop.

Select an option

Save zxmarcos/3686638 to your computer and use it in GitHub Desktop.
#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