From 90facee4e680bdcc79cae5a58edbfe434599ebdd Mon Sep 17 00:00:00 2001 From: Abdurrezzak Efe Date: Fri, 10 Nov 2017 01:36:26 +0200 Subject: [PATCH] Add files via upload --- 19.KMP.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 19.KMP.cpp diff --git a/19.KMP.cpp b/19.KMP.cpp new file mode 100644 index 0000000..0181850 --- /dev/null +++ b/19.KMP.cpp @@ -0,0 +1,56 @@ +/* + * This program takes a text and a pattern and searches the pattern inside the text + * using Knuth- Morris- Pratt Algorithm + * + * Coded by: Abdurrezak Efe + * + * */ +#include + +using namespace std; + +int fail[10000]; //assuming pattern has a length less than or equal to 10000 + +void failure(string s) +{ + int i=1; + int j=0; + while(i0) + j = fail[j-1]; + else + fail[i] = 0, i++; + } +} + +int kmp(string text, string pattern) +{ + int i=0; + int j=0; + while(i0) + j = fail[j-1]; + else + i++; + } + return -1; + +} + +int main() +{ + string text, pattern; + cin >> text >> pattern; + + cout << kmp(text, pattern) << endl; +}