Pages
Welcome~~~
It is easier to write an incorrect program than understand a correct one.
Friday, April 8, 2011
Thursday, April 7, 2011
Wednesday, April 6, 2011
Tuesday, April 5, 2011
Ways to change the background color on your computer
Monday, April 4, 2011
# Longest Common Sequence Problem
Contents
- Retracing our steps
- Simple example
- A recursive approach
- Problems with the recursive solution
- Dynamic programming
- Memoize
- Recursion limit
- Bottom up calculation
- Extracting an LCS from the grid
- Animation
- Performance
- Reducing the space for LCS lengths
- Hirschberg's Linear Space Algorithm
- Applied to the New York Marathon Results
- Hirschberg's linear space algorithm in C++
- Applications: edit distance
- Applications: genetic mutations
- Applications: version control
- Conclusions
- Next time
- Thanks
- Python 3 Notes
--
Saturday, April 2, 2011
Python Set C++ Set -- From: Word Aligned
Python set is an unordered container designed for fast membership, union, intersection, and differencing operations — much like the mathematical sets I learned about at school — whereas a C++ set is an ordered container, featuring logarithmic access times and persistent iterators.
Think: C++ set ≈ binary tree; Python set ≈ hashed array.
It's apparent which method is correct for which language now. To put something into a binary tree you must recurse down the tree and find where to insert it. Hence std::set::insert() is correct C++. To put something into a hashed array you hash it and add it right there. Hence set.add() is proper Python.
You insert into trees and add to hashes: so if your set is a tree, call S.insert(), and if it's a hash, S.add().
Such logical arguments don't always deliver.
Question: Suppose now that S is a string and you're after its length. Should you use S.length() orS.size()?
Answer: Neither or both.
In Python a string is a standard sequence and as for all other sequences len(S) does the trick. In C++ a string is a standard container and as for all other containers S.size() returns the number of elements; but, being std::string, S.length() does too.
Oh, and the next revision of C++ features an unordered_set (available now asstd::tr1::unordered_set) which is a hashed container. I think unordered_set is a poor name for something which models a set better than std::set does but that's the price it pays for coming late to the party. And you don't std::unordered_set::add elements to it, you std::unordered_set::insert them.
--
Friday, April 1, 2011
Longest common subsequence
Contents
- Retracing our steps
- Simple example
- A recursive approach
- Problems with the recursive solution
- Dynamic programming
- Memoize
- Recursion limit
- Bottom up calculation
- Extracting an LCS from the grid
- Animation
- Performance
- Reducing the space for LCS lengths
- Hirschberg’s Linear Space Algorithm
- Applied to the New York Marathon Results
- Hirschberg’s linear space algorithm in C++
- Applications: edit distance
- Applications: genetic mutations
- Applications: version control
- Conclusions
- Next time
- Thanks
- Python 3 Notes
# Find the number of digits in n!.
--
#Two strings,find out they are anagrams
int findAna(char *s1, char*s2){
int sum = 0;
while(*s1 != '\0')
sum ^= *s1++;
while(*s2 != '\0')
sum ^= *s2++;
return sum;
void main (){
int i;
i = findAna("abcd","bac");
printf("%d",i); // i = 0 ? => anagrams .... else not anagrams :)
}
# Construct tree from Ancestor Matrix
--