-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Time-Complexity :- O(length of string) | ||
Used for outputing indices where a particular text appears in a given string | ||
*/ | ||
#include<bits/stdc++.h> | ||
#include<sstream> | ||
#include<ext/pb_ds/tree_policy.hpp> | ||
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<int,int> | ||
#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<int> 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<<i-j+1<<" "; | ||
j=b[t.length()-1]; | ||
} | ||
i++; | ||
} | ||
} | ||
int main() | ||
{ std::ios_base::sync_with_stdio(false); cin.tie(NULL); | ||
cin>>t>>p; | ||
b.resize(p.length()); | ||
preprocess(p); | ||
loop(i,0,b.size()) | ||
cout<<b[i]<<" "; | ||
cout<<endl; | ||
kmp(t,p); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
Time-Complexity :- O(nlog(n)^2),where n is the length of string | ||
Used for outputing all the suffixes in a sorted way | ||
*/ | ||
#include<bits/stdc++.h> | ||
#include<sstream> | ||
#include<ext/pb_ds/tree_policy.hpp> | ||
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<int,string> | ||
#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<vector<int> > 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<int>(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<n;i++) | ||
{ | ||
L[i].first = sa[stp-1][i]; | ||
L[i].orig=i; | ||
if(i+cnt<n) | ||
L[i].second = sa[stp-1][i+cnt]; | ||
else | ||
L[i].second = -1; | ||
} | ||
sort(L,L+n,cmp); | ||
sa[stp][L[0].orig] = curr = 0; | ||
for(i=1;i<n;i++) | ||
{ | ||
if((L[i].first != L[i-1].first) || (L[i].second != L[i-1].second)) | ||
curr++; | ||
sa[stp][L[i].orig] = curr; | ||
} | ||
stp++; | ||
} | ||
loop(i,0,n) | ||
{ stp = L[i].orig; | ||
cout<<stp<<" "<<a.substr(stp,n-stp)<<endl; | ||
} | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Complexity = O(length of string) | ||
s = aaabaab => 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<bits/stdc++.h> | ||
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<ll,ll> | ||
#define MP make_pair | ||
#define pb push_back | ||
#define f first | ||
#define se second | ||
#define ll long long int | ||
#define vi vector<ll> | ||
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<<z[i]<<" "; | ||
return 0; | ||
} |