Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

suffix Array construction code

  • Loading branch information...
commit 4ed13271483625b8684118d70c84f57e575c66f5 1 parent 38088be
@kumar-abhishek authored
View
57 UVa_455_nsq.cpp
@@ -0,0 +1,57 @@
+/*
+ * =====================================================================================
+ * Filename: ZAlgorithm.cpp
+ * Description: Z Algorithm
+ * Version: 1.0
+ * Created: 03/07/2012 02:27:43 PM
+ * Compiler: gcc
+ * Author: Kumar Abhishek (), kr.abhish@gmail.com
+ * =====================================================================================
+ */
+#include<iostream>
+#include<climits>
+#include<cstdio>
+#include<cstring>
+#include<string>
+#include<vector>
+#include<algorithm>
+using namespace std;
+
+int main(){
+ int T;
+ cin>>T;
+ while(T--){
+ string text;
+ scanf("\n");
+ getline(cin,text);
+ int sz = text.size();
+ int ans = 0 ;
+ for(int L = 1 ;L < sz ; L++){
+ bool exists = true;
+ int i = 0, j = 0;
+ for(;i<sz && j<L;i++){
+ if(text[i] == text[j++]){
+ if(j == L)
+ j = 0;
+ continue;
+ }
+ else{
+ exists = false;
+ break;
+ }
+ }
+ if(exists == true && j == 0){
+ ans = L;
+ break;
+ }
+ }
+
+ if(ans == 0)
+ ans = sz;
+ printf("%d\n",ans);
+ if(T>0)
+ printf("\n");
+ }
+
+ return 0;
+}
View
1  reverseLinkedList.cpp
@@ -80,6 +80,7 @@ void RecursiveReverse(struct node** headRef) {
first->next = NULL; // (tricky step -- make a drawing)
*headRef = rest; // fix the head pointer
cout<<"rest: "<<rest->data<<" " ; //note this line://rest is being passed to head as a reference pointer so its value keeps changing, and when going back withinrecursive calls , its value does not get changed and remains at the tail of the original linked list. !!!!IMPORTANT !!!!!!remember this./
+ //NOTE:head and rest are basically pointing to same location because of the rest being passed as reference. So, once rest is at last node, it does not get modified back in while going back in recursive calls.
}
int main()
View
BIN  suffixArray/lecture11-4up.ps
Binary file not shown
View
88 suffixArray/suffixArray.cpp
@@ -0,0 +1,88 @@
+//suffix array construction algorithm:http://apps.topcoder.com/forums/?module=Thread&threadID=627379&start=0
+#include <iostream>
+#include <string>
+#include<cstring>
+#include <algorithm>
+#include <ctime>
+using namespace std;
+#define N 10000000
+#define REP(i,n) for(int i =0;i<n;i++)
+
+char str[N];
+int H = 0, Bucket[N], nBucket[N], c;
+
+struct Suffix{
+ int idx; // Suffix starts at idx, i.e. it's str[ idx .. L-1 ]
+ bool operator<(const Suffix& sfx) const
+ // Compares two suffixes based on their first 2H symbols,
+ // assuming we know the result for H symbols.
+ {
+ if(H == 0) return str[idx] < str[sfx.idx];
+ else if(Bucket[idx] == Bucket[sfx.idx])
+ return (Bucket[idx+H] < Bucket[sfx.idx+H]);
+ else
+ return (Bucket[idx] < Bucket[sfx.idx]);
+ }
+ bool operator==(const Suffix& sfx) const
+ {
+ return !(*this < sfx) && !(sfx < *this);
+ }
+} Pos[N];
+
+int UpdateBuckets(int L)
+{
+ int start = 0, id = 0, c = 0;
+ for(int i = 0; i < L; i++)
+ {
+ /*
+ If Pos[i] is not equal to Pos[i-1], a new bucket has started.
+ */
+ if(i != 0 && !(Pos[i] == Pos[i-1]))
+ {
+ start = i;
+ id++;
+ }
+ if(i != start) // if there is bucket with size larger than 1, we should continue ...
+ c = 1;
+ nBucket[Pos[i].idx] = id; // Bucket for suffix starting at Pos[i].idx is id ...
+ }
+ memcpy(Bucket, nBucket, sizeof(int) * L); //basically it copies only L bytes if you dont multiply by 4 : so it should be sizeof(int)*L
+ return c;
+}
+
+void SuffixSort(int L)
+{
+ for(int i = 0; i < L; i++) Pos[i].idx = i;
+ // H == 0, Sort based on first Character.
+ sort(Pos, Pos + L);
+ cout<<endl<<"Pos : ";
+ REP(i,L) cout<<Pos[i].idx<< " ";
+ cout<<endl;
+ // Create initial buckets
+ c = UpdateBuckets(L);
+ cout<<endl<<" buckets: ";
+ REP(i,L) cout<<Bucket[Pos[i].idx]<<" ";
+ for(H=1;c;H *= 2) {
+ // Sort based on first 2*H symbols, assuming that we have sorted based on first H character
+ sort(Pos, Pos+L);
+ cout<<endl<<"Pos : ";
+ REP(i,L) cout<<Pos[i].idx<< " ";
+ cout<<endl;
+ // Update Buckets based on first 2*H symbols
+ c = UpdateBuckets(L);
+ cout<<endl<<" buckets: ";
+ REP(i,L) cout<<Bucket[Pos[i].idx]<<" ";
+
+
+ }
+}
+
+int main()
+{
+ cin >> str;
+ int L = strlen(str) + 1;
+ SuffixSort(L);
+ for(int i = 0; i < L; i++)
+ cout << "'" << str + Pos[i].idx << "'" << endl;
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.