From 0170309aa8ad3f279a57897e35f1b531ca3eb257 Mon Sep 17 00:00:00 2001 From: Tarun anand Date: Tue, 3 Oct 2017 14:38:13 +0530 Subject: [PATCH] Added section for string_algorithms --- String_algo/KMP.cpp | 68 ++++++++++++++++++++++++++++++++++ String_algo/SUFFIX-ARRAY.cpp | 71 ++++++++++++++++++++++++++++++++++++ String_algo/z_algorithm.cpp | 50 +++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 String_algo/KMP.cpp create mode 100644 String_algo/SUFFIX-ARRAY.cpp create mode 100644 String_algo/z_algorithm.cpp diff --git a/String_algo/KMP.cpp b/String_algo/KMP.cpp new file mode 100644 index 00000000..b6620d1a --- /dev/null +++ b/String_algo/KMP.cpp @@ -0,0 +1,68 @@ +/* + Time-Complexity :- O(length of string) + Used for outputing indices where a particular text appears in a given string +*/ +#include +#include +#include +using namespace std; +#define mod 1000000007 +#define all(v) v.begin(),v.end() +#define loop(i,a,b) for(i=(int)a;i<(int)b;i++) +#define revloop(i,a,b) for(i=(int)a;i>=(int)b;i--) +#define stloop(it,v) for(it=v.begin();it!=v.end();++it) +#define ii pair +#define MP make_pair +#define pb push_back +#define ll long long int +#define fill(v,d) memset(v,d,sizeof(v)) +#define INF 1000000005 +#define PI acos(-1.0) +vector b; +string p,t; +int i; +void preprocess(string a) +{ + int i = 1,j = 0; + b[0] = 0; + while(i < (int)a.length()) + { + while(j > 0 && a[i] != a[j]) + { j--; + j= b[j]; + } + if(a[i] == a[j]) + j++; + b[i] = j; + i++; + } +} +void kmp(string a,string t) +{ + int i=0,j=0; + while(i<(int)a.length()) + { + while(j > 0 && a[i] != t[j]) + { j--; + j= b[j]; + } + if(a[i] == t[j]) + j++; + if(j == (int)t.length()) + { cout<>t>>p; + b.resize(p.length()); + preprocess(p); + loop(i,0,b.size()) + cout< +#include +#include +using namespace std; +#define mod 1000000007 +#define all(v) v.begin(),v.end() +#define loop(i,a,b) for(i=(int)a;i<(int)b;i++) +#define revloop(i,a,b) for(i=(int)a;i>=(int)b;i--) +#define stloop(it,v) for(it=v.begin();it!=v.end();++it) +#define ii pair +#define MP make_pair +#define pb push_back +#define ll long long int +#define fill(v,d) memset(v,d,sizeof(v)) +#define INF 1000000005 +#define PI acos(-1.0) +vector > sa; +struct mytuple{ + int orig; + int first; + int second; +}; +bool cmp(mytuple a,mytuple b) +{ + if(a.first == b.first) + return a.second < b.second; + return a.first < b.first; +} +int main() +{ std::ios_base::sync_with_stdio(false); cin.tie(NULL); + string a; + int i,n,curr,cnt,stp; + cin>>a; + n = a.length(); + stp = log(n)/log(2) + 2; + sa.assign(stp,vector(n)); + loop(i,0,n) + sa[0][i] = a[i]-'a'; + mytuple L[n]; + stp = 1; + for(cnt = 1;cnt < n; cnt*=2) + { + for(i=0;i z[] = {-1,2,1,0,2,1,0} + s = aaaaa => z[] = {-1,4,3,2,1} + s = abacaba => z[] = {-1,0,1,0,3,0,1} +*/ +#include +using namespace std; +#define mod 1000000007 +#define all(v) v.begin(),v.end() +#define rep(i,a,b) for(i=(ll)a;i<(ll)b;i++) +#define revrep(i,a,b) for(i=(ll)a;i>=(ll)b;i--) +#define strep(it,v) for(it=v.begin();it!=v.end_();++it) +#define ii pair +#define MP make_pair +#define pb push_back +#define f first +#define se second +#define ll long long int +#define vi vector +ll modexp(ll a,ll b){ ll res = 1; while(b > 0){ if(b & 1) res = (res * a); a = (a * a); b/=2; } return res; } +#define rs resize +long long readLI(){ register char c; for(c = getchar(); !(c>='0' && c<='9'); c = getchar()); register long long a=c-'0'; + for(c = getchar(); c>='0' && c<='9'; c = getchar()) + a = (a<<3)+(a<<1)+c-'0'; + return a; +} +const int N = 100009; +string a; +ll i,z[N]; +void z_function(string s) +{ + ll l = 0,r = 0,n = s.length(); + rep(i,1,n){ + if(i <= r) z[i] = min(r - i + 1,z[i - l]); + while(i + z[i] < n and s[z[i]] == s[i + z[i]]) + z[i]++; + if(i + z[i] - 1 > r) + l = i, r = i + z[i] - 1; + } +} +int main() +{ + std::ios_base::sync_with_stdio(false); cin.tie(NULL); + cin>>a; + z[0] = -1; //Not possible of zero length + z_function(a); + rep(i,0,a.length()) cout<