Created
April 3, 2014 08:19
-
-
Save chaomai/9950463 to your computer and use it in GitHub Desktop.
hardcode
This file contains 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> | |
#include <sstream> | |
int main() | |
{ | |
using namespace std; | |
string txt("nanonawefawefawefnaoawfnanowfawe"); | |
string pat("nano"); | |
stringstream stxt(txt); | |
enum state | |
{ | |
sempty, sn, sna, snan, snano | |
}; | |
state s = sempty; | |
char ch; | |
int index(0); | |
while (!stxt.eof()) | |
{ | |
stxt >> ch; | |
index++; | |
switch (s) | |
{ | |
case sempty: | |
{ | |
if (ch == 'n') | |
{ | |
s = sn; | |
} | |
else | |
{ | |
s = sempty; | |
} | |
break; | |
} | |
case sn: | |
{ | |
if (ch == 'n') | |
{ | |
s = sn; | |
} | |
else if (ch == 'a') | |
{ | |
s = sna; | |
} | |
else | |
{ | |
s = sempty; | |
} | |
break; | |
} | |
case sna: | |
{ | |
if (ch == 'n') | |
{ | |
s = snan; | |
} | |
else | |
{ | |
s = sempty; | |
} | |
break; | |
} | |
case snan: | |
{ | |
if (ch == 'o') | |
{ | |
s = snano; | |
} | |
else if (ch == 'n') | |
{ | |
s = sn; | |
} | |
else if (ch == 'a') | |
{ | |
s = sna; | |
} | |
else | |
{ | |
s = sempty; | |
} | |
break; | |
} | |
case snano: | |
{ | |
s = sempty; | |
break; | |
} | |
} | |
if (s == snano) | |
{ | |
cout << "find at " << index - pat.length() + 1 << endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment