Skip to content

Commit

Permalink
suffix array learned
Browse files Browse the repository at this point in the history
  • Loading branch information
ramjeetsaran committed Jun 19, 2016
1 parent ed6f511 commit 95ed899
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 1 deletion.
23 changes: 23 additions & 0 deletions ALICESIE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//DATE: 20/06/2016
//Author: Ramjeet Saran
//http://www.spoj.com/problems/ALICESIE/


# include <bits/stdc++.h>

# define gc getchar_unlocked
# define pc putchar_unlocked
# define li long int
# define lli long long int

using namespace std;


int main() {
int T, N;
cin>>T;
while(T--) {
cin>>N;
cout<<(N + 1) / 2<<endl;
}
}
47 changes: 47 additions & 0 deletions DISUBSTR.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//DATE: 20/06/2016
//Author: Ramjeet Saran
//http://www.spoj.com/problems/DISUBSTR/

// SUFFIX ARRAY APPLICATION
// TRY 1: using SET MAP GOT TLE

# include <bits/stdc++.h>

# define gc getchar_unlocked
# define pc putchar_unlocked
# define li long int
# define lli long long int

using namespace std;

int LCP(string s1, string s2) {
int counter = 0, len1 = s1.length(), len2 = s2.length();
for(int i = 0; i < len1; i++) {
if(s1[i] == s2[i]) {
counter++;
} else {
break;
}
}
return counter;
}
int main() {
int T;
cin>>T;
while(T--) {
string S;
vector<string> suffixArray;
cin>>S;
int length = S.length();
for(int i = 0; i < length; i++) {
suffixArray.push_back(S.substr(i));
}
sort(suffixArray.begin(), suffixArray.end());
int ans = suffixArray[0].length();
for(int j = 1; j < length; j++) {
// cout<<suffixArray[ j ] << " "<<suffixArray[ j - 1]<<endl;
ans += suffixArray[j].length() - LCP(suffixArray[j], suffixArray[j - 1]);
}
cout<<ans<<endl;
}
}
33 changes: 33 additions & 0 deletions MCOINS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//DATE: 20/06/2016
//Author: Ramjeet Saran
//http://www.spoj.com/problems/MCOINS/

// GAME ALGORITHMS

# include <bits/stdc++.h>

# define gc getchar_unlocked
# define pc putchar_unlocked
# define li long int
# define lli long long int

using namespace std;


int main() {
int N, K, M;
cin>>N>>K>>M;
int *arr = new int[1000001];
memset(arr, 0, 1000001);
for(int i = 1; i <= 1000000; i++) {
if(arr[i - 1] == 0 || (i - N >= 0 && arr[i - N] == 0) || (i - K >= 0 && arr[i - K] == 0)) {
arr[i] = 1;
}
}
int A;
for(int j = 0; j < M; j++) {
cin>>A;
arr[A] == 1 ? cout<<"A" : cout<<"B";
}
cout<<endl;
}
6 changes: 5 additions & 1 deletion Readme
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@ http://www.spoj.com/users/raj94/
88) BUGLIFE
89) MISERMAN
90) ENIGMATH
91) FIBOSUM
91) FIBOSUM
92) ALICESIE
93) DISUBSTR
94) MCOINS
95) SILVER
24 changes: 24 additions & 0 deletions SILVER.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//DATE: 19/06/2016
//Author: Ramjeet Saran
//http://www.spoj.com/problems/SILVER/


#include <bits/stdc++.h>

# define MAX(a,b) a>b ? a : b;
# define MIN(a,b) a<b ? a : b;
# define lli long long int
# define ull unsigned long long int
# define FOR(i,j,n) for(i = j; i < n; i++)

using namespace std;

int main() {
int N;
scanf("%d", &N);
while(N) {
int res = (int) floor(log2(N));
printf("%d\n", res);
scanf("%d", &N);
}
}

0 comments on commit 95ed899

Please sign in to comment.