我发现进度一快,学的快,忘得的贼快,所以开个贴,把每个专题的基本操作给弄个基本操作,天天看一遍。
1:二分
int look(int a[], int key, int n) 如过匹配就返回下标,没找到就返回-1;
{
int low, high, mid;
low = 0;
high = n;
while(low <= high)
{
mid = (low+high)/2;
if(key == a[mid]) return mid;
else if(key > a[mid])
low = mid + 1;
else if(key < a[mid])
high = mid - 1;
}
return -1;
}
2:快排
void p(int a[], int min, int max)
{
int x = a[min], i = min, j = max;
if(min >= max) return;
while(i<j)
{
while(i<j&&a[j]>=x) j--;
a[i] = a[j];
while(i<j&&a[i]<=x) i++;
a[j] = a[i];
}
a[i] = x;
p(a, min, i-1);
p(a, i+1, max);
}
补充
快排在这个头文件 #include<algorithm>
用法主要两个:
1 如果是数组sort(a, a+n);
2 结构体的话:
struct node{
int x, y;
}e[11111];
int cmp(node a, node b)
{
return a.x < b.y;
}
sort(e, e+n, cmp) 按照cmp的方式排序,也就是按照结构体里的x从小到大。
3:KMP:
例题:
学骚话
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
众所周知,青春期猪头少年梓川咲太骚话连篇。
这可让众多网友羡慕不已,于是观众们纷纷尊称其为师傅,更有甚者已经开始模仿师傅学习了。
现在给你两行字符,第一行是师傅梓川咲太的正宗骚话,第二行是某位**网友的模仿。
请问这位网友的骚话完成度是多少?
骚话完成度是指该骚话在原版中的出现次数,在原版中出现时可重叠。
Input
输入包含多组数据。
每组数据由两行字符串构成,分别表示猪头少年的正宗骚话和网友的模仿。
字符串长度均在1000000以内。
Output
输出X的最大长度,占一行
Sample Input
abababababa
aba
Sample Output
5
#include<stdio.h>
#include <stdlib.h>
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
char s1[11111111], s2[11111111];
int nex[11111111];
int flag;
void shu(char a[]) 求next数组;
{
int i = 0;
int j = -1;
nex[0] = -1;
int n = strlen(a);
while(i < n )
{
if(j == -1 || a[i] == a[j])
{
i++;
j++;
nex[i] = j;
}
else j = nex[j];
}
}
void pan(char s1[], char s2[]) 比较s1,s2;
{
int m = strlen(s1);
int n = strlen(s2);
int i = 0, j = 0;
while(i < m && j<n)
{
if(j == -1 || s1[i] == s2[j])
{
i++;
j++;
}
else j = nex[j];
if(j >= n)
{
flag ++;
i = i-n+1;
j = 0;
}
}
}
int main()
{
while(scanf("%s",s1)!=EOF)
{
flag = 0;
scanf("%s",s2);
shu(s2);
pan(s1, s2);
printf("%d\n",flag);
}
return 0;
}
4:平衡排序二叉树
数据结构实验之查找二:平衡二叉树
Time Limit: 400 ms Memory Limit: 65536 KiB
Problem Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Sample Input
5
88 70 61 96 120
Sample Output
70
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct tree
{
int data;
tree *lboy;
tree *rgirl;
int dp;
};
int depth(tree *root)
{
if (!root) return 0;
return max(depth(root->lboy), depth(root->rgirl)) + 1;
}
tree *ll(tree *root) 右旋
{
tree *p = root->lboy;
root->lboy = p->rgirl;
p->rgirl = root;
p->dp = max(depth(p->lboy), depth(p->rgirl)) + 1;
root->dp = max(depth(root->lboy), depth(root->rgirl)) + 1;
return p;
}
tree *rr(tree *root) 左旋
{
tree *p = root->rgirl;
root->rgirl = p->lboy;
p->lboy = root;
p->dp = max(depth(p->rgirl), depth(p->lboy)) + 1;
root->dp = max(depth(root->lboy), depth(root->rgirl)) + 1;
return p;
}
tree *rl(tree *root) 根的右孩子先右旋,根再左旋。
{
root->rgirl = ll(root->rgirl);
return rr(root);
}
tree *lr(tree *root) 根的左孩子先左旋,根再右旋。
{
root->lboy = rr(root->lboy);
return ll(root);
}
tree *creat(tree *root, int x) 创建
{
if(!root)
{
root = new tree;
root->lboy = root->rgirl = NULL;
root->data = x;
root->dp = 1;
}
else if(x < root->data)
{
root->lboy = creat(root->lboy, x);
if(abs(depth(root->lboy) - depth(root->rgirl)) > 1) 左右孩子深度相差大于一得话就不平衡
{
if(x < root->lboy->data)
{
root = ll(root);
}
else root = lr(root);
}
}
else if(x > root->data)
{
root->rgirl = creat(root->rgirl, x);
if(abs(depth(root->lboy) - depth(root->rgirl)) > 1)
{
if(x > root->rgirl->data)
{
root = rr(root);
}
else root = rl(root);
}
}
root->dp = depth(root);
return root;
}
int main()
{
int i, x, n;
scanf("%d",&n);
tree *root = NULL;
for(i = 0; i < n; i++)
{
cin>>x;
root = creat(root, x);
}
printf("%d\n",root->data);
return 0;
}
二叉树层序遍历:用队列实现
void cengci(tree *root)
{
queue<tree*>q;
if(root)
{
printf("%c",root->data);
q.push(root);
}
while(!q.empty())
{
root = q.front();
q.pop();
if(root->lboy)
{
printf("%c",root->lboy->data);
q.push(root->lboy);
}
if(root->rgirl)
{
printf("%c",root->rgirl->data);
q.push(root->rgirl);
}
}
}
5:随机生成在【min,max】区间的整数,生成n个。
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
srand((unsigned)time(NULL));
int min, max, num;
while(scanf("%d %d %d",&min,&max,&num)!=EOF)
{
max++;
for(int i=1; i<=num; i++)
{
int a=(rand()% (max-min)) + min;
cout<<"num"<<i<<' '<<a<<endl;
}
cout<<endl;
}
return 0;
}
6,DFS(深度优先搜索) 是递归
BDS(广度优先搜索) 是队列
从编号小到大遍历并输出。
void DFS(int a) //从a编号开始遍历
{
MAP[a][a] = 1;
if(a == 0) cout << a;
else cout << " " << a;
for(int i = 0; i < m; i++)
{
if(MAP[i][i] == 0 && MAP[a][i])
{
DFS(i);
}
}
}
void BFS(int a) //从a编号开始遍历
{
queue<int> q;
int t = 0;
mm[a] = 1; mm数组是标记是否查看过的。
q.push(a);
while(!q.empty())
{
int s = q.front();
q.pop();
if(t == 0)
{
cout << s;
t = 1;
}
else cout << " " <<s;
for(int i = 0; i < n; i++)
{
if(MAP[s][i] == 1 && mm[i] == 0)
{
q.push(i);
mm[i] = 1;
}
}
}
}