
算法
坚持是一种信仰
这个作者很懒,什么都没留下…
展开
-
输出一个字符串的所有组合
#include#includevoid mswap(char *str, int x, int y){ char c = str[x]; str[x] = str[y]; str[y] = c;}void rank(char* str, int begin, int end){ if(begin == (end - 1)) {原创 2013-08-14 15:26:24 · 367 阅读 · 0 评论 -
第一个只出现一次的字符
#include #include //find the first letter which one appear once//input abacddaef, the reture should be 'b'char * findLetter(char * str){ int hashTable[256] = {0}; if (str == NULL) return NUL原创 2013-12-07 11:30:37 · 392 阅读 · 0 评论 -
Trie树
之前一直以为trie树多么的高深,董老师讲的非常简单易懂。http://dongxicheng.org/structure/trietree/转载 2013-12-09 05:05:12 · 362 阅读 · 0 评论 -
树 --- 创建+中序遍历+按层次输出+计算树的高度+输出和为n的所有路径
#include #include #include using namespace std;typedef struct lnode { int data; struct lnode *left, *right;}node;void createTree(node * &root, int x){ if (root == NU原创 2013-11-20 09:12:36 · 590 阅读 · 0 评论 -
动态规划求连续元素的和最大
/***********dynamic algorithm to get the max sum of the continous elements of a integer array**/#include#includeusing namespace std;/**********sum[i] = max{sum[i-1] + a[i], a[i]}start[i]原创 2013-11-14 15:37:18 · 527 阅读 · 0 评论 -
快速排序
#include#includeusing namespace std;int m_partition(int a[], int low, int high){int temp = a[low];while (low = temp)high--;a[low] = a[high];while (low <= high && a[low] <= temp)low++;a[high] = a[low];原创 2013-12-13 12:21:27 · 374 阅读 · 0 评论 -
最长公共子串(有序)
LCS[i][j] = LCS[i-1][j-1] + 1; if a[i] = b[j];else LCS[i][j] = 0;#include #include #include using namespace std;const int max_len = 20;void LCS(char a[], char b[]){ if (a == NULL ||原创 2013-12-30 10:56:38 · 425 阅读 · 0 评论 -
n个整数中删除重复的整数
边扫面边与前面的比较#include void delDupData(int a[], int n){ int i, end, j; end = 0; for (i = 1; i < n; i++) { for (j =0; j <=end; j++) { if (a[j] == a[i])原创 2014-01-02 13:46:13 · 736 阅读 · 0 评论 -
链表 -- 反转 (递归 + 非递归)
#include#includeusing namespace std;typedef struct lnode{ int data; struct lnode * next;}node;void createLink(node *&head){ int x; node * p = head; while (scanf(原创 2013-11-19 09:25:17 · 577 阅读 · 0 评论 -
从一个升序数组中二分查找第一个大于等于val的数
#include #include #include int findval(int a[], int len, int val){ int begin, end, mid; begin = 0; end = len -1; while (begin < end) { mid = (begin + end) / 2; if (val > a[mid]) be原创 2014-01-08 22:45:58 · 1128 阅读 · 0 评论 -
分治法---归并排序
分支法的步骤:1. devide2. conquer3. combine #include#includeusing namespace std;//Below algorithm is according MIT's charles.void merge(int a[], int first, int mid, int last){ int* res = new原创 2013-11-14 16:06:32 · 493 阅读 · 0 评论 -
编程之美---数组中超过出现次数超过一半的数字
每次删除两个不同的数,因此我们可以考虑在遍历数组的时候保存两个值:一个是数组中的一个数字,一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1。 如果下一个数字和我们之前保存的数字不同,则次数减1。如果次数为零,我们需要保存下一个数字,并把次数重新设为1。 #include #include //find the king which ap原创 2013-12-07 12:52:22 · 528 阅读 · 0 评论 -
编程之美---数组中超过出现次数超过一半的数字
因此我们可以考虑在遍历数组的时候保存两个值:一个是数组中的一个数字,一个是次数。当我们遍历到下一个数字的时候,如果下一个数字和我们之前保存的数字相同,则次数加1。 如果下一个数字和我们之前保存的数字不同,则次数减1。如果次数为零,我们需要保存下一个数字,并把次数重新设为1。 #include #include //find the king which appear time is mo原创 2013-12-07 12:49:10 · 104 阅读 · 0 评论 -
Dijkstra 算法 -- 从单节点到所有节点的最短距离
和prim非常相似#include #include using namespace std;#define MAXLEN 10#define INFINITE 0xffffint d[MAXLEN];int visited[MAXLEN] = {0};int m_min(int x, int y){ return ((x > y)? y :原创 2013-11-16 20:39:34 · 445 阅读 · 0 评论 -
链表 -- 合并两个有序链表 (递归 + 非递归)
#include#includeusing namespace std;typedef struct lnode{ int data; struct lnode * next;}node;void createLink(node *&head){ int x; node * p = head; while (scanf(原创 2013-11-19 10:18:53 · 596 阅读 · 0 评论 -
句子反转
先把每个单词反转,再反转整个句子 #include #include #include using namespace std;void reverseWord(char *begin, char *end){ assert(begin != NULL && end != NULL); char c; while (begin < end)原创 2013-11-21 11:05:17 · 759 阅读 · 0 评论 -
回溯算法 --- N 皇后问题
#include #include #include using namespace std;int const N = 20; //max support queen numbervoid printResult(int result[], int n, int m){ cout<<"output the "<<m<<" result:"<<endl;原创 2013-11-22 10:31:08 · 526 阅读 · 0 评论 -
编程之美 --- 将一个正整数n分解成m个连续自然数的和
将一个正整数n分解成m个连续自然数的和如:6 = 1+2+39 = 2+3+49 = 4+5#includevoid getSeqSum(int n){ int begin, i,j, sum; begin = 1; sum = begin; for (i = 1; i <= (n+1)/2; ) { //re原创 2013-11-22 14:20:12 · 1749 阅读 · 0 评论 -
KMP 算法--- 搜索匹配字符串
#include #include #include using namespace std;//next[j] 表示s[i]<>p[j],下一个需要比较的字符从next[j]开始//生成next[]的过程相当于自身匹配前缀串和后缀串, i指向后缀串,j指向前缀串void getNextVal(char p[], int len, int next[]){原创 2013-11-27 16:36:17 · 383 阅读 · 0 评论 -
动态规划 -- 背包问题
//动态规划 -- 背包问题//dp[i][j] = max{dp[i-1][j], dp[i-1][j-w[j]] + val[i]}#include# include# define max(a,b) a>b?a:busing namespace std;int main(){ int dp[101][1001],m,W,w[101],val[10原创 2013-11-14 16:20:49 · 496 阅读 · 0 评论 -
动态规划法求两个序列的最长公共子序列
动态规划法求两个序列的最长公共子序列问题,MIT的perfessor讲的非常详细,根据他的算法的两种实现注意,这里所指的最长公共子序列是可以不相邻的,与平常所说的最长公共子串(相邻的)不一样。 #include#includeusing namespace std;#define max_num(x, y) (x)>(y)?(x):(y)int c[100][100原创 2013-11-14 14:49:49 · 838 阅读 · 0 评论 -
寻找最大的k个数
#include#includeusing namespace std;const int k = 5;void heapAdjust(int a[], int first, int last){ int key = a[first]; for (int i = 2*first; i < last; i = 2*i) { if (i+1 <原创 2013-11-14 18:26:10 · 505 阅读 · 0 评论 -
最小生成树prim算法实现
最小生成树prim算法实现今天从志权师兄那里学会了最小生成树。所谓生成树,就是n个点之间连成n-1条边的图形。而最小生成树,就是权值(两点间直线的值)之和的最小值。 首先,要用二维数组记录点和权值。如上图所示无向图:int map[7][7]; map[1][2]=map[2][1]=4; map[1][3]=ma原创 2013-11-15 16:29:22 · 407 阅读 · 0 评论 -
从一个字符串中删除指定字符串
如abcdefg 删除子字符串ef后得到abcdg,被删除的字符串必须全部连续匹配上才算,子字符有可能出现多次。一次扫描,变扫描边删除。这么简单的题目当时因为紧张没有写出来,太遗憾了。#include #include #include void deletestr(char str[], char substr[]){ char *p, *q, *end; int i,原创 2014-01-08 21:59:32 · 968 阅读 · 0 评论