Cheat Sheet 2014
Cheat Sheet 2014
Puzzles
Contents
1 STL Useful Tips
1.1 Common libraries . . . . . . . . . . .
1.2 I/O . . . . . . . . . . . . . . . . . . .
1.3 Useful constant . . . . . . . . . . . .
1.4 Space waster . . . . . . . . . . . . .
1.5 Tricks in cmath . . . . . . . . . . . .
1.6 Initialize array with predefined value
1.7 Modifying sequence operations . . .
1.8 Merge . . . . . . . . . . . . . . . . .
1.9 String . . . . . . . . . . . . . . . . .
1.10 Heap . . . . . . . . . . . . . . . . . .
1.11 Sort . . . . . . . . . . . . . . . . . .
1.12 Permutations . . . . . . . . . . . . .
1.13 Searching . . . . . . . . . . . . . . .
1.14 Random algorithm . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
2
2
2
3
3
3
3
3
4
4
5
5
6
6
2 Number Theory
2.1 Prime number under 100 . . . . . .
2.2 Max or min . . . . . . . . . . . . .
2.3 Greatest common divisor GCD
2.4 Least common multiple LCM .
2.5 If prime number . . . . . . . . . .
2.6 Prime factorization . . . . . . . . .
2.7 Leap year . . . . . . . . . . . . . .
2.8 Binary exponiential . . . . . . . . .
2.9 ab mod p . . . . . . . . . . . . . . .
2.10 Factorial mod . . . . . . . . . . . .
2.11 Generate combinations . . . . . . .
2.12 10-ary to m-ary . . . . . . . . . . .
2.13 m-ary to 10-ary . . . . . . . . . . .
2.14 Binomial coefficient . . . . . . . . .
2.15 Catalan numbers . . . . . . . . . .
2.16 Eulerian numbers . . . . . . . . . .
2.17 Karatsuba algorithm in Java . . .
2.18 Eulers totient function . . . . . . .
2.19 Split plane . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6
6
6
6
6
6
7
7
7
8
8
8
9
9
9
10
10
10
11
11
3 Searching Algorithms
3.1 Find rank k in array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.2 KMP Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
11
12
4 Dynamic Programming
4.1 0/1 Knapsack problems . . . . . . . . . . . .
4.2 Complete Knapsack problems . . . . . . . . .
4.3 Longest common subsequence (LCS) . . . . .
4.4 Longest increasing common sequence (LICS)
4.5 Longest Increasing Subsequence (LIS) . . . .
4.6 Maximum submatrix . . . . . . . . . . . . . .
4.7 Partitions of integers . . . . . . . . . . . . . .
4.8 Partitions of sets . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
13
13
13
14
14
15
15
16
17
5 Trees
5.1 Tree traversal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Depth and width of tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17
17
18
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 Graph Theory
6.1 Graph representation . . . . . . . . . . . . .
6.2 Flood fill algorithm . . . . . . . . . . . . . .
6.3 SPFA shortest path . . . . . . . . . . . .
6.4 Floyd-Warshall algorithm shortest path of
6.5 Prim minimum spanning tree . . . . . .
6.6 Eulerian circuit . . . . . . . . . . . . . . . .
6.7 Topological sort . . . . . . . . . . . . . . . .
1
1.1
. . . . .
. . . . .
. . . . .
all pairs
. . . . .
. . . . .
. . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1.2
I/O
1.3
Useful constant
INT_MIN
INT_MAX
LONG_MIN
LONG_MAX
LLONG_MIN
LLONG_MAX
(~0u) // infinity (for long and long long)
// use (~0u)>>2 for int.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
19
19
21
21
22
23
23
24
1.4
Space waster
1.5
Tricks in cmath
1.6
1.7
void
void
void
void
void
//
//
void
void
void
1.8
Merge
1.9
String
// Searching
unsigned int find(const string &s2, unsigned int pos1 = 0);
unsigned int rfind(const string &s2, unsigned int pos1 = end);
unsigned int find_first_of(const string &s2, unsigned int pos1 = 0);
unsigned int find_last_of(const string &s2, unsigned int pos1 = end);
unsigned int find_first_not_of(const string &s2, unsigned int pos1 = 0);
unsigned int find_last_not_of(const string &s2, unsigned int pos1 = end);
// Insert, Erase, Replace
string& insert(unsigned int pos1, const string &s2);
string& insert(unsigned int pos1, unsigned int repetitions, char c);
string& erase(unsigned int pos = 0, unsigned int len = npos);
string& replace(unsigned int pos1, unsigned int len1, const string &s2);
string& replace(unsigned int pos1, unsigned int len1, unsigned int repetitions, char c);
// String streams
stringstream s1;
int i = 22;
s1 << "Hello world! " << i;
cout << s1.str() << endl;
1.10
Heap
1.11
Sort
1.12
bool
bool
bool
bool
Permutations
next_permutation(iterator
next_permutation(iterator
prev_permutation(iterator
prev_permutation(iterator
first,
first,
first,
first,
iterator
iterator
iterator
iterator
last);
last, LessThanOrEqualFunction comp);
last);
last, LessThanOrEqualFunction comp);
1.13
Searching
1.14
Random algorithm
srand(time(NULL));
// generate random numbers between [a,b)
rand() % (b - a) + a;
// generate random numbers between [0,b)
rand() % b;
// generate random permutations
random_permutation(anArray, anArray + 10);
random_permutation(aVector, aVector + 10);
2
2.1
Number Theory
Prime number under 100
2.2
Max or min
2.3
2.4
2.5
If prime number
bool prime(int n)
{
if (n<2) return false;
2.6
Prime factorization
2.7
Leap year
bool isLeap(int n)
{
if (n%100==0)
if (n%400==0) return true;
else return false;
if (n%4==0) return true;
else return false;
}
2.8
Binary exponiential
else
{
a *= a;
n >>= 1;
}
return res;
}
ab mod p
2.9
2.10
Factorial mod
//n! mod p
int factmod (int n, int p) {
long long res = 1;
while (n > 1) {
res = (res * powmod (p-1, n/p, p)) % p;
for (int i=2; i<=n%p; ++i)
res=(res*i) %p;
n /= p;
}
return int (res % p);
}
2.11
Generate combinations
a[i]=a[i-1]+1;
}
}
2.12
10-ary to m-ary
char a[16]={0,1,2,3,4,5,6,7,8,9,
A,B,C,D,E,F};
string tenToM(int n, int m)
{
int temp=n;
string result="";
while (temp!=0)
{
result=a[temp%m]+result;
temp/=m;
}
return result;
}
2.13
m-ary to 10-ary
string num="0123456789ABCDE";
int mToTen(string n, int m)
{
int multi=1;
int result=0;
for (int i=n.size()-1;i>=0;i--)
{
result+=num.find(n[i])*multi;
multi*=m;
}
return result;
}
2.14
Binomial coefficient
2.15
Catalan numbers
Cn =
n1
X
k=0
Ck Cn1k =
1
n
n+1 k
(1)
The first terms of this sequence are 2, 5, 14, 42, 132, 429, 1430 when C0 = 1. This is the number of ways to build a balanced
formula from n sets of left and right parentheses. It is also the number of triangulations of a convex polygon, the number of
rooted binary tress on n + 1 leaves and the number of paths across a lattice which do not rise above the main diagonal.
2.16
Eulerian numbers
n
n1
n1
=k
+ (n k + 1)
k
k
k1
// This is the number of permutations of length n with exactly k ascending sequences or runs.
// Basis: k=0 has value 1
#define MAXN 100 // largest n or k
long eularian(n,k)
int n,m;
{
int i,j;
long e[MAXN][MAXN];
for (i=0; i<=n; i++) e[i][0] = 1;
for (j=0; j<=n; j++) e[0][j] = 0;
for (i=1; i<=n; i++)
for (j=1; j<i; j++)
e[i][j] = k*e[i-1][j] + (i-j+1)*e[i-1][j-1];
return e[n][k];
}
2.17
(2)
int N = Integer.parseInt(args[0]);
BigInteger a = new BigInteger(N, random);
BigInteger b = new BigInteger(N, random);
start = System.currentTimeMillis();
BigInteger c = karatsuba(a, b);
stop = System.currentTimeMillis();
System.out.println(stop - start);
start = System.currentTimeMillis();
BigInteger d = a.multiply(b);
stop = System.currentTimeMillis();
System.out.println(stop - start);
System.out.println((c.equals(d)));
}
}
2.18
// the positive integers less than or equal to n that are relatively prime to n.
int phi (int n)
{
int result = n;
for (int i=2; i*i<=n; ++i)
if(n %i==0)
{
while(n %i==0)
n /= i;
result -= result / i;
}
if (n > 1)
result -= result / n;
return result;
}
2.19
Split plane
(n+1)n
2
+ 1 sub-regions.
Searching Algorithms
3.1
3.2
KMP Algorithm
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<int> VI;
void buildTable(string& w, VI& t)
{
t = VI(w.length());
int i = 2, j = 0;
t[0] = -1; t[1] = 0;
while(i < w.length())
{
if(w[i-1] == w[j]) { t[i] = j+1; i++; j++; }
else if(j > 0) j = t[j];
else { t[i] = 0; i++; }
}
}
int KMP(string& s, string& w)
{
int m = 0, i = 0;
VI t;
buildTable(w, t);
while(m+i < s.length())
{
if(w[i] == s[m+i])
{
i++;
if(i == w.length()) return m;
}
else
{
m += i-t[i];
if(i > 0) i = t[i];
}
}
return s.length();
}
int main(void)
{
string a = (string) "The example above illustrates the general technique for assembling "+
"the table with a minimum of fuss. The principle is that of the overall search: "+
"most of the work was already done in getting to the current position, so very "+
"little needs to be done in leaving it. The only minor complication is that the "+
"logic which is correct late in the string erroneously gives non-proper "+
"substrings at the beginning. This necessitates some initialization code.";
string b = "table";
int p = KMP(a, b);
cout << p << ": " << a.substr(p, b.length()) << " " << b << endl;
return 0;
}
Dynamic Programming
4.1
#include<iostream>
using namespace std;
int f[1000]={0};
int n=0, m=0;
int main(void)
{
cin >> n >> m;
for (int i=1;i<=n;i++)
{
int price=0, value=0;
cin >> price >> value;
for (int j=m;j>=price;j--)
if (f[j-price]+value>f[j])
f[j]=f[j-price]+value;
}
cout << f[m] << endl;
return 0;
}
4.2
#include<iostream>
using namespace std;
int f[1000]={0};
int n=0, m=0;
int main(void)
{
cin >> n >> m;
for (int i=1;i<=n;i++)
{
4.3
int dp[1001][1001];
int lcs(const string &s, const string &t)
{
int m = s.size(), n = t.size();
if (m == 0 || n == 0) return 0;
for (int i=0; i<=m; ++i)
dp[i][0] = 0;
for (int j=1; j<=n; ++j)
dp[0][j] = 0;
for (int i=0; i<m; ++i)
for (int j=0; j<n; ++j)
if (s[i] == t[j])
dp[i+1][j+1] = dp[i][j]+1;
else
dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
return dp[m][n];
}
4.4
#include<iostream>
using namespace std;
int
int
int
int
a[100]={0};
b[100]={0};
f[100]={0};
n=0, m=0;
int main(void)
{
cin >> n;
for (int i=1;i<=n;i++) cin >> a[i];
cin >> m;
for (int i=1;i<=m;i++) cin >> b[i];
for (int i=1;i<=n;i++)
{
int k=0;
for (int j=1;j<=m;j++)
{
4.5
#include<iostream>
using namespace std;
int n=0;
int a[100]={0}, f[100]={0}, x[100]={0};
int main(void)
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> a[i];
x[i]=INT_MAX;
}
f[0]=0;
int ans=0;
for(int i=1;i<=n;i++)
{
int l=0, r=i;
while (l+1<r)
{
int m=(l+r)/2;
if (x[m]<a[i]) l=m; else r=m;
// change to x[m]<=a[i] for non-decreasing case
}
f[i]=l+1;
x[l+1]=a[i];
if (f[i]>ans) ans=f[i];
}
cout << ans << endl;
return 0;
}
4.6
Maximum submatrix
4.7
Partitions of integers
int n,m;
{
int i,j;
long f[[MAXN][MAXN];
f [1][1] = 1;
for (i=0;i<=n;i++) f[i][0] = 0;
for (i=1; i<=n; i++)
for (j=1; j<i; j++)
if (i-j <= 0)
f[i][j] = f[i][k-1];
else
f[i][j] = f[i-j][k]+f[i][k-1];
return f[n][k];
}
4.8
Partitions of sets
5
5.1
n
n
=
=1
1
n
Trees
Tree traversal
int L[100]={0};
int R[100]={0};
void DLR(int m)
{
cout << m << " ";
if (L[m]!=0) DLR(L[m]);
if (R[m]!=0) DLR(R[m]);
}
void LDR(int m)
{
if (L[m]!=0) LDR(L[m]);
cout << m << " ";
if (R[m]!=0) LDR(R[m]);
}
void LRD(int m)
{
if (L[m]!=0) LRD(L[m]);
if (R[m]!=0) LRD(R[m]);
cout << m << " ";
}
int main(void)
{
cin >> n;
for (int i=1;i<=n;i++)
cin >> L[i] >> R[i];
(3)
(4)
5.2
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int l[100]={0};
int r[100]={0};
stack<int> mystack;
int n=0;
int w=0;
int d=0;
int depth(int n)
{
if (l[n]==0 && r[n]==0)
return 1;
int depthl=depth(l[n]);
int depthr=depth(r[n]);
int dep=depthl>depthr ? depthl:depthr;
return dep+1;
}
void width(int n)
{
if (n<=d)
{
int t=0,x;
stack<int> tmpstack;
while (!mystack.empty())
{
x=mystack.top();
mystack.pop();
if (x!=0)
{
t++;
tmpstack.push(l[x]);
tmpstack.push(r[x]);
}
}
w=w>t?w:t;
mystack=tmpstack;
width(n+1);
}
}
int main(void)
{
cin >> n;
for (int i=1;i<=n;i++)
cin >> l[i] >> r[i];
d=depth(1);
mystack.push(1);
width(1);
cout << w << " " << d << endl;
return 0;
}
Graph Theory
6.1
Graph representation
6.2
6.3
6.4
// map[i][j]=infinity at start
void floyd()
{
6.5
int d[1001]={0};
bool v[1001]={0};
int a[1001][1001]={0};
int main(void)
{
int n=0;
cin >> n;
for (int i=1;i<=n;i++)
{
int x=0, y=0, z=0;
cin >> x >> y >> z;
a[x][y]=z;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (a[i][j]==0) a[i][j]=INT_MAX;
cout << prim(1,n) << endl;
}
int prim(int u, int n)
{
int mst=0,k;
for (int i=0;i<d.length;i++) d[i]=INT_MAX;
for (int i=0;i<v.length;i++) v[i]=false;
d[u]=0;
int i=u;
while (i!=0)
{
v[i]=true;k=0;
mst+=d[i];
for (int j=1;j<=n;j++)
if (!v[j])
{
if (a[i][j]<d[j]) d[j]=a[i][j];
if (d[j]<d[k]) k=j;
}
i=k;
}
return mst;
}
6.6
Eulerian circuit
// USACO Fence
#include<iostream>
using namespace std;
int f[100]={0}, ans[100]={0};
bool g[100][100]={0}, v[100]={0};
int n=0, m=0, c=0;
void dfs(int k)
{
for (int i=1;i<=n;i++)
if (g[k][i])
{
g[k][i]=false;
g[i][k]=false;
dfs(i);
}
m++;
ans[m]=k;
}
int main(void)
{
cin >> n >> m;
for (int i=1;i<=m;i++)
{
int x=0, y=0;
g[x][y]=true;
g[y][x]=true;
f[x]++;
f[y]++;
}
m=0;
int k1=0;
for (int i=1;i<=n;i++)
{
if (f[i]%2==1) k1++;
if (k1>2)
{
cout << "error" << endl;
return 0;
}
if (f[i]%2 && c==0) c=i;
}
if (c==0) c=1;
dfs(x);
for (int i=m;i>=1;i--) cout << ans[i] << endl;
return 0;
}
6.7
Topological sort