Showing posts with label suffix array. Show all posts
Showing posts with label suffix array. Show all posts

Sunday, September 11, 2016

HackerRank - String function calculation (II)

Sept. 11, 2016

Plan to find time, at least 30 minutes, warm up the algorithm next week first, and then, continue to work on this advanced problem - suffix array, LCP, two pointer techniques.

After 5 month (April, 2016), come back to work on the algorithm, using suffix array, LCP, two pointer technique:

https://www.hackerrank.com/challenges/string-function-calculation

Previous work on suffix array


1. http://juliachencoding.blogspot.ca/2016/04/april-11-2016-plan-to-work-on-lcp-array.html
2. http://juliachencoding.blogspot.ca/2016/04/april-11-2016-plan-to-work-on-lcp-array.html
3. http://juliachencoding.blogspot.ca/2016/09/first-code-practice-trie.html

More in detail:
Practice 1: brute force, score 8.89 out of 80 - April 10, 2016

http://juliachencoding.blogspot.ca/2016/04/hackerrank-string-function-calculation.html

Practice 2:

Timeout issue - find ways to cut down the time - string algorithm - Boyer Moore

http://juliachencoding.blogspot.ca/2016/04/hackerrank-string-function-calculation_10.html

Practice 3: suffix array, still time out

http://juliachencoding.blogspot.ca/2016/04/hackerrank-string-calculate-function-iii.html

Plan to work on LCP array later - took 5 months break

LCP first practice using Trie  - Sept 12, 2016

http://juliachencoding.blogspot.ca/2016/09/first-code-practice-trie.html

Read competitive book, learn the TLE - time limit exception - how to evaluate,

Preparation:  (Sept. 15, 2016 1+ hour reading)

Study Algorithm Edition 4 - Robert Sedgewick/ Kevin Wayne

Page 875-885 Suffix Array

Monday, September 5, 2016

Suffix Array and longest common prefix array (LCP array) - study

June 5, 2016

 It is the labor day long weekend, spent 3 hours (6:30am - 10:00am) to read suffix array from this favorite competitive programming book, and try to please herself, a new goal - score any point with suffix array work or LCP (even cannot remember the full name - called longest common prefix array), HackerRank practice or code sprint.

It is a lonely journey - reading the book, but it is perfect for physical recovery  - muscle and bones -
laying on the bed with the excellent one night sleep, just after 3+ hour tennis sports, and do not want to move, read a book.

Yesterday, Julia warmed up more than 1+ hour, one single match, one double match lasted more than one hour, until tie break. She lost double with 5 to 7 lost the match.  She suffered tennis elbow pain issues.

The competitive book about suffix array:

6.4 Suffix Tree and Suffix Array - page 114 - 119


Motivation talk
1. suffix array - who did the research to introduce the term - suffix array in 1993?

 https://en.wikipedia.org/wiki/LCP_array
 https://en.wikipedia.org/wiki/Udi_Manber

More reading:
Suffix array:
http://algs4.cs.princeton.edu/63suffix/

Play with some code first, get solid understanding suffix array - what are the benefits using suffix array? Shorten time complexity - 

suffix tree -> suffix array -> sorted array -> LCP - longest common prefix

Arguments:
1. Building efficient Suffix Tree under contest environment is a bit complex and risky
2. Suffix Array invented by Udi Manber and Gene Myers, has similar functionalities as Suffix Tree but simpler to implement, especially in programming contest setting
3. ...

Facts:
1. Suffix Array is an integer array that contains indices of sorted suffixes


1. Write C# version of this Java code:
http://algs4.cs.princeton.edu/63suffix/SuffixArray.java.html

2. Suffix array - longest repeated substring - using suffix array
http://algs4.cs.princeton.edu/63suffix/LongestRepeatedSubstring.java.html

3. Keyword in context (KWIC)
Given the suffix array, easy to search for a string or sentence via binary search. Memory is linear. Search is O(K log N) where K is the length of the string you are searching for. (Can be done in K + log N by using the lcp array.)

study the code:

More reading:
1. https://leetcode.com/articles/longest-common-prefix/

2. http://www.geeksforgeeks.org/longest-common-prefix-set-1-word-by-word-matching/
3. http://www.geeksforgeeks.org/longest-common-prefix-set-2-character-by-character-matching/
4. http://www.geeksforgeeks.org/longest-common-prefix-set-3-divide-and-conquer/
5. http://www.geeksforgeeks.org/longest-common-prefix-set-4-binary-search/

- Julia likes to calm down quickly when she gets nervous. When she has a negative self-talk, she will remind herself - "Everyone faces challenges on court and I'm no different." Replace with positive self-talk.

Monday, August 1, 2016

HackerRank - Prepare to get experience on advanced level algorithms on HackerRank

August 1, 2016

 Choose a small topic to work on, when to choose to work on advanced algorithm and what to learn through the practice.

 Pragmatic ideas:

1. How is the algorithm developed by editors? Best algorithm lecture material to study.

2. Study some code for classical problems through submissions.

Julia spent over 100 hours to work on HackerRank, solved over 50+ algorithms problems, and then, she is getting better to understand the problem statement on HackerRank. But she only chose to work on easy, medium difficult questions.

Last time - 3 hours - really struggling - world code sprint #5 with advanced, difficult questions.  

Some facts on 3 hours activities: 
1. Tried to guess, break down small problems.
2. Wrote down some notes
3. Tried to guess what kind of problem it is - DP, DFS, graph, etc.

Julia likes to come back to review, and if she can write a blog on her 3 hours experience:

1. Spent 30+ minutes to read the problem statement
http://juliachencoding.blogspot.ca/2016/07/build-forest-hackerrank-world.html

2. Spent 30+ minutes to read the problem statement
http://juliachencoding.blogspot.ca/2016/07/build-palindrome-hackerrank-world.html



Actionable Items:

1. Work on suffix array first, learn basics first:
http://www.geeksforgeeks.org/suffix-array-set-1-introduction/

2. Read the article, get all questions related to suffix array in the contest:
(plan to spend 2 hours to study)
http://www.stanford.edu/class/cs97si/suffix-array.pdf

3. Review previous suffix array blog and C# implementation of suffix array:
http://juliachencoding.blogspot.ca/search/label/suffix%20array%20C%23


Monday, July 25, 2016

Build a palindrome - HackerRank world codesprint #5

July 25, 2016

Read the problem statement more than 30 minutes:

Build a palindrome - problem statement is here


Try to come out the idea to solve the problem first. (Advanced problem) - Learning starts from reading the analysis. Prepare for advanced level challenges from HackerRank, one by one.

Read the editorial notes:

editorial notes


Here are a list terms to review:

1. Consider that >= (L+1)/2 characters are present in the first string.
2. string hashing/ a palindromic tree
3. Iterate on each index i of the given string a and consider the longest palindrome starting from i as a part of your solution to find the maximum length possible, L, for string s.
4. Suffix array
5. LCP - largest common prefix array

Required Knowledge: Suffix Array, Palindromic Tree, String Hashing, Implementation

one selected code to study - Just use it as an example to motivate herself to work hard one by one on small topic, and then, one day in the future, build complicated stuff like this challenge. 

C# code to study


Java code to study


C++ code to study


Previous blog about suffix array

Follow up after 6 months


March 26, 2017

Format the style of the blog, clean up web links and make the web links readable.




Monday, April 11, 2016

HackerRank: String Calculate function (III) - Suffix array (II)

Plan to work on LCP array later. 

Use C# implementation of suffix array in blog #1, still time out on hackerRank

SuffixArray implemented in C#, by MSFT, Google employee; Julia has to catch up C#, and learn 
how to write beautiful code like this:

Study C# code how to implement IEnumberable interface in suffixArray code:

how String.Compare api is designed
Julia also takes some time to work on suffix array, and get some drawing about suffix tree, and play with two case:
aaaaaa, 
banana, 
abcabcddd, 
For those two strings, what are suffix tree? Get so close to concrete examples, draw diagram, run test case, study code, and compare to other C# submission. 

Focus on how to find the range of suffix array to fit in the pattern: 


Walk through the examples how suffix array is implemented in C# code:


public SuffixArray Search(string str)

        {
            if (m_lower > m_upper)
                return this;
            // Otherwise search for boundaries that enclose all
            // suffixes that start with supplied string.
            var lower = Search(str, c_lower);
            var upper = Search(str, c_upper);
            return new SuffixArray(m_text, m_pos, lower + 1, upper);
        }

Example 1: string "aaaaaa", pattern string "aaa". 
suffix array {5, 4, 3, 2, 1, 0}
suffix string:
a
aa
aaa
aaaa
aaaaa
aaaaaa

suffix array is acending order, string.Compare("a","aa")  = -1, string.Compare("aa","aa") = 0. string.Compare("aa","a") = 1

suffix array is implemented using interface IEnumberable. 
we try to find 2 index, low, top, 
index = 1, "aa", string.compare("aa", 0,"aaa",0, 3) = -1, 
now, we need to find top index, 
assume that "abc" is in the array, string.compare("abc",0,"aaa",0, 3) = 1, first one is >-1, stop; 
aaa, aaaa, aaaaa, aaaaa, all computed value of comparison = 0 since we only check substring with length 3. 
So top index is 5. 
So, count of substring "aaa" is calculate by top index - low index + 1
top index = 5, 
low index = 1+1
so count = 4. 

Try to figure out how this binary search algorithm is used to calculate low index and top index, called twice, comparison value for low index = 0, for high index is -1. 
In other words, find last one is smaller than "aaa", and first one bigger than "aaa". 

Example 2: "banana", pattern "ban", 
 Read the content in the blog first:
http://www.geeksforgeeks.org/pattern-searching-set-8-suffix-tree-introduction/

And then, 
suffix strings:
banana
anana
nana
ana
na
a

Sort them ascending order:
a
ana
anana
banna
na
nana

pattern string "ban", first to find a string which is last one < "ban", -> "anana", index 2, "ana" < "ban"
and then, find first string which is bigger than "ban", only comparing length 3 substring, 
"banna" = "ban" based on comparison (substring len = 3), so
"na" - index = 4 > "ban"
2+1 = 3, 
top index = 3 
so, ban pattern match is  3-3 +1 = 1



"Work on Examples First" - Practice feels good!

The LCP-LR array helps improve this to O(m+logN)O(m+logN), in the following way


LCP array reading
https://www.hackerrank.com/challenges/pseudo-isomorphic-substrings/topics/lcp-array

https://en.wikipedia.org/wiki/LCP_array

Follow up: May 4, 2015
Read the article:

June 10, 2016 - Plan to spend 30 minutes to read the slides:

Julia, learn something here from the blog:

http://decomplexify.blogspot.ca/2014/07/lcp-array.html?view=classic

Sunday, April 10, 2016

HackerRank: String Calculate function (III) - Suffix array

April 10, 2016

Time spent: 9:00pm - 11:40pm

Problem statement - string calculate function on Hackerrank 



KMP Algorithm, Rabin Karp Algorithm, Finite Automate based Algorithm, Boyer Moore algorthm. 

All of the above algorithms preprocess the pattern to make the pattern searching faster. The best time complexity that we could get by preprocessing pattern is O( n ) where n is length of the text. 

So, Julia second try still failed, no progress on time out issue. 

Now, Julia read the blog and learn suffix array / LCP array in next hour 9:00 pm -10:00 pm:

A suffix tree is built of the text. After preprocessing text (building suffix tree of text), we can search any pattern in O(m) time where m is length of the pattern. O(n) -> O(m), solve timeout issue