
算法设计
sinshine
这个作者很懒,什么都没留下…
展开
-
BSC,MSC,HLR的作用
BSC,MSC,HLR分别的作用:MSC交换设备,程控交换机;BSC是基站控制器,用来控制BTS;HLR是本地位置寄存器,可以理解成一个数据库,存放本地移动用户的一些数据。BSC:基站控制器,它与BTS基站收发机构成基站子 系统。MSC:移动交换中心,它是网络的核心,提供交换功能并且面对下列功能实体:BSS、HLR、VLR、AUC、EIR、OMC和固定网,把移动用户与固定网用户转载 2012-04-01 10:13:08 · 10121 阅读 · 0 评论 -
dijkstra算法
//Dijkstra 算法的时间复杂度为O(n^2) 空间复杂度取决于存储方式,邻接矩阵为O(n^2)//非负权, 有向#include #include using namespace std;const int max_vex = 100;const in原创 2011-10-03 20:29:44 · 1885 阅读 · 0 评论 -
字符存在
当有一个字符串str1,另一个字符串str2,要判断str2中的字符是否在str1中存在,可以使用hash表判断,O(m+n)。也可以将每一个字符一次赋予一个素数, 从2开始,然后将这些素数相乘。然后遍历第二个字符串,用素数去除前一次的乘积,若无余数,就存在,反之则不存在。原创 2011-10-03 12:24:07 · 410 阅读 · 0 评论 -
Fibonacci数列的递归优化
// right = F(n-2) , return value = F(n-1)int fibonacci_R(int n, int& right){ if(2 == n) { right = 1; return 1; } int t = 0; righ原创 2011-10-12 14:55:15 · 593 阅读 · 0 评论 -
判断一个点是否在一个三角形内(二维)
设有三角形abc和点p,需要判断p是否在三角形abc内部只需要用叉乘进行判断,abXap, apXac,pbXpc是否都与abXac平行。另外面积法也能判断,判断三角形abp,acp,bcp的面积和是否等于三角形abc的面积。其中,求面积可以用叉乘来实现。原创 2011-09-29 21:48:24 · 632 阅读 · 0 评论 -
删数问题
正整数a,删掉k位数,使剩下的数字按原序组合形成的新的数最小。贪心算法,最近下降点先删除。13789 --> 13 k = 3void delete_k(string &int_str, int k){ if (int_str.length() <=原创 2011-09-29 21:16:39 · 522 阅读 · 0 评论 -
排序数组循环移位后查找问题
#include using namespace std;// a 排序数组右移后的数组// for example: 4,5,6,7,8,9,0,1,2,3// 二分,返回索引int select( int a[], int left, int righ原创 2011-09-29 16:53:44 · 1743 阅读 · 5 评论 -
如何维护一个中位数
设计一个数据结构,包括两个函数,插入数据和获得中位数。利用大根堆和小根堆,其中大根堆维护较小的一半数据,小根堆维护较大的一半数据。然后根据相应的情况,对两个堆做相应的堆化操作,以满足两个堆中元素数目一致。时间复杂度O(lgn)extension:设计一个堆原创 2011-10-11 21:29:38 · 1117 阅读 · 0 评论 -
字符串组合
// 字符串中的字符彼此不相同// buffer for resultchar ret[100];// str: orginal string// i: index for str// n: index for ret// sz: size of the su原创 2011-08-28 11:03:20 · 381 阅读 · 0 评论 -
堆排序
#include #include using namespace std;void show(int a){ cout << a <<" ";}template void my_swap(T &a, T &b){ T t = a; a = b; b原创 2011-08-27 00:02:14 · 404 阅读 · 0 评论 -
线性时间选择
void swap(int* a, int* b){ int t = *a; *a = *b; *b = t;}void my_sort(int a[], int left, int right){ if (a == 0 || right <=原创 2011-09-27 13:48:05 · 571 阅读 · 0 评论 -
排序N个比N^7小的数,要求的算法是O(n)
采用基数排序,用10个桶(vector),每一个桶用队列表示(queue),分别代表0-9,然后依次从低位到高位开始将要排序的数倒入桶中,然后再顺序取出来,直到排序完成。若有5个数,12,4,5,130,28第一次(个位):0: 13012: 12转载 2011-10-04 20:54:33 · 1013 阅读 · 0 评论 -
RSA 算法
一、RSA算法 :首先: 找出三个数, p, q, r, 其中 p, q 是两个相异的质数,r 是与 (p-1)(q-1) 互质的数...... p, q, r 这三个数便是 private key .接著: 找出 m, 使得 rm == 1 mod (p-1)(q-1).....这个 m 一定存在, 因为 r 与 (p-1)(q-1) 互质, 用辗转相除法就可以得到了.....转载 2012-04-01 09:29:48 · 1349 阅读 · 0 评论 -
crc16
#include using namespace std;// x^16+x^12+x^5+1unsigned short crc16(unsigned char *ptr, int len){ unsigned short CRC = 0xFFFF; // vc++ sizeof(unsigned short) == 2 unsigned char j, tmp =原创 2011-11-26 14:06:51 · 773 阅读 · 0 评论 -
经典hanno
/*hanno tower -1- | | --2-- | | ---3--- | | ... | |原创 2011-10-28 15:44:21 · 451 阅读 · 0 评论 -
strstr实现
// 从字符串s1中寻找s2第一次出现的位置(不比较结束符NULL)// if 0 == s2, then return s1char* my_strstr(const char* s1, const char* s2)// look for the first s2 in s1{ int k = 0;// compact with c if (0 == s2 || '\0'原创 2011-09-13 17:26:54 · 417 阅读 · 0 评论 -
查找字符串中第一个出现一次的字符
#include using namespace std;// look up the first character in the string.// using hash table to reduce the time used.char look_up(con原创 2011-10-08 14:15:09 · 674 阅读 · 0 评论 -
寻找两个满足条件的数
题目:输入一个数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。ans:用位图原创 2011-10-07 22:00:02 · 444 阅读 · 0 评论 -
状态机
从倒水问题中,深刻理解了状态机设计的重要性,包括状态机抽象和状态机转化关系的设计。倒水问题,定义好状态和状态转化关系,剪枝去掉不可能的状态和重复的状态,用一片内存空间来保存状态。原创 2011-10-16 20:01:10 · 520 阅读 · 0 评论 -
已知一个序列seq=[a,b,....,z,aa,ab,...,zz,aaa,aab,....],求任意一个字符串s=[a-z]+在seq中出现的位置
// 26进制,不过有缺点,需要大数处理#include using namespace std;long pow(long x, long y){ long ret = 1; while(y>0) { ret = ret*x; --y; } re原创 2011-10-08 15:05:40 · 2312 阅读 · 0 评论 -
查找字符串中最长的连续数字子串
#include using namespace std;int max_num_str(char* pstr, char* output){ if ( *pstr == '\0') { *output = '\0'; return 0; } char原创 2011-10-08 14:46:53 · 1840 阅读 · 0 评论 -
求对称子字符串的最大长度
求对称子字符串的最大长度,如abcbae,那么最大长度为5。动态规划,O(n*n)bool a[100][100] = {false};int get_longest(char *b){ int len = 0; int slen = strlen(b);原创 2011-08-27 22:00:54 · 428 阅读 · 0 评论 -
二叉搜索树镜像
struct node{ int val; node* p_left; node* p_right; node(int t) { val = t; p_left = p_right = 0; }};typedef struct node* link;原创 2011-08-28 20:49:26 · 531 阅读 · 0 评论 -
链表反转
struct node{ int val; struct node* next; node(int t, node* n=0) { val = t; next = n; }};typedef struct node* link;void insert原创 2011-08-27 22:33:31 · 354 阅读 · 0 评论 -
找出两个链表的第一个公共结点
设有链表a,表长为m,链表b,表长为n,若分别遍历a和b,其时间复杂度为o(mn),这是最直观的方法。但是,细想一下,若两个链表相交,则从它们交叉的第一个公共结点到链表尾部的长度是相等的。因此,我们若能同步两个链表,就能在线性时间内找出第一个公共结点。 分别遍历a原创 2011-08-17 21:44:21 · 481 阅读 · 0 评论 -
自底向上归并,链表
link merge_sort(link t){ queue que; if (0 == t || 0 == t->p_next) return t; for (link u = 0; t!=0; t=u) { u = t->p_next; t->p_next转载 2011-09-11 00:05:20 · 514 阅读 · 0 评论 -
链表归并排序
#include #include using namespace std;struct node{ int val; node* p_next; node(int t, node* pnode=NULL) { val = t; p_next = pn转载 2011-09-10 23:53:11 · 472 阅读 · 0 评论 -
链表选择排序
#include #include using namespace std;struct node{ int _val; node* _next; node(int t=0, node* p=0) { _val = t; _next = p; }}原创 2011-09-11 21:57:11 · 408 阅读 · 0 评论 -
O(nlogn)时间复杂度 链表排序
可以使用归并排序int counter = 0;// statisticstruct list_node{ int val; struct list_node* next; list_node(int t, struct list_node* pn = 0)原创 2011-09-08 23:35:12 · 2774 阅读 · 0 评论 -
double型数据的整数次方
// 此方法能减少乘法次数, O(1)#include #include using namespace std;// do not consider doulbe overflowdouble pow(double base, int exponent)原创 2011-09-08 23:31:20 · 749 阅读 · 0 评论 -
素数表
#define MAX 200void print_prime(int m){ assert(m <= MAX); bitset flag; flag.reset(); flag.set(0); flag.set(1); for (int i = 4; i <=原创 2011-09-07 17:11:25 · 483 阅读 · 0 评论 -
去掉字符串相邻重复单词
// 空间复杂度O(1),时间复杂度O(n)void str_unique(char* str, int fs, int fd, int ed){ if (fs> (int)strlen(str)) return; // skip extra whitespace原创 2011-09-07 11:30:34 · 1146 阅读 · 0 评论 -
打印所有匹配括号组合
char dst[100];void parentheses_match(char dst[], int k, int left_remain, int left_used, int right_used, int M){ if (right_used >= M) {原创 2011-09-07 14:12:19 · 935 阅读 · 0 评论 -
快速排序
void my_swap(int &a, int &b){ int t = a; a = b; b = t;}int partion(int a[], int left, int right){ int t = a[right]; int i = left-1原创 2011-08-17 21:47:45 · 393 阅读 · 1 评论 -
字符串全排列
void swap_char(char &a, char &b){ char t = a; a = b; b = t;}// 重复字符处理bool foo(char* str, char p, int begins, int ends原创 2011-08-25 22:02:09 · 344 阅读 · 0 评论 -
判断二叉树是否平衡
#include #include using namespace std;struct node{ int val; struct node *left; struct node *right; node(int t) { val = t; lef原创 2011-08-25 22:06:34 · 331 阅读 · 0 评论 -
二叉树转双向链表
#include using namespace std;// 树节点struct node{ int val; struct node* left; struct node* right; node(int t) { val = t; left原创 2011-08-25 23:36:11 · 758 阅读 · 0 评论 -
判断一棵树是不是另一棵树的子树
特殊情况,空树为任何树(包括空树)的子树struct node{ int val; struct node *left; struct node *right; node(int t) { val = t; left = 0; right = 0; }原创 2011-08-17 22:15:34 · 4162 阅读 · 2 评论 -
求两数和
不用加减乘除,求两数和,无符号数.unsigned int add(unsigned int a, unsigned int b){ if (0 == b) return a; unsigned int c = a ^ b; // carray unsigned i原创 2011-08-25 17:39:54 · 379 阅读 · 0 评论 -
二叉树中两个子节点的最近公共节点
求二叉树中两个子节点的最近公共节点// 树节点struct node{ struct node* left; struct node* right; int val; node(int t=0) { val = t; left = 0; right原创 2011-08-25 17:27:24 · 698 阅读 · 0 评论