您的位置:首页 > 理论基础 > 数据结构算法

程序设计竞赛(acm)常用数据结构

2012-02-01 08:53 453 查看
程序设计竞赛(acm)常用数据结构

一、栈stack

包含头文件:stack

定义: stack<int> name; //int型的

用法:

出栈: name.pop(); //出栈

入栈: name.push(T); //T为入栈元素

栈顶: name.top() //返回栈首

判空: name.empty(); //若为空返回true

长度: name.size(); //栈的大小

二、队列queue

包含头文件:queue

定义: queue<int> name; //int型的

用法:

出队: name.pop();

入队: name.push(T); //T为入栈元素

队首: name.front()

判空: name.empty() //若为空返回true

队尾: name.back()

长度: name.size();

三、优先队列priority_queue

包含头文件:queue



struct node

{

friend bool operator < (const node a,const node b)

{

return a.priority < b. priority;

}

int priority; //优先级

int value; //值

};



定义: priority_queue<node,vector<node>,cmp> name;

用法:

队首元素:name.top()

其余操作和队列一样

四、堆 heap

包含头文件:algorithm

STL里面的堆操作一般用到的只有4个:make_heap();、pop_heap();、push_heap();、sort_heap();

1、make_heap();

函数原型:void make_heap(first_pointer,end_pointer,compare_function);

一个参数是数组或向量的头指针,第二个向量是尾指针。第三个参数是比较函数的名字。在缺省的时候,默认是大跟堆。(下面的参数都一样就不解释了)

作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)

2、pop_heap();

函数原型:void pop_heap(first_pointer,end_pointer,compare_function);

作用:pop_heap()不是真的把最大(最小)的元素从堆中弹出来。而是重新排序堆。它把first和last交换,然后将[first,last-1)的数据再做成一个堆。

3、push_heap();

函数原型:void pushheap(first_pointer,end_pointer,compare_function);

作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加进来,做成一个堆。

4、sort_heap()

函数原型:void sort_heap(first_pointer,end_pointer,compare_function);

作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然,经过排序之后就不是一个有效堆了)



下面是例子:

[c-sharp] view plaincopyprint?#include<algorithm>



#include<cstdio>



using namespace std;



bool cmp(int a,int b)



{



return a>b;



}



int main()



{



int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};



make_heap(&number[0],&number[12]);



//结果是:51 35 40 23 29 20 26 22 19 12 17 15



for(i=0;i<12;i++)



printf("%d ",number[i]);



printf("/n");







make_heap(&number[0],&number[12],cmp);



//结果:12 17 15 19 23 20 26 51 22 29 35 40



for(i=0;i<12;i++)



printf("%d ",number[i]);



printf("/n");











number[12]=8; //加入元素8



//加入后调整



push_heap(&number[0],&number[13],cmp);



//结果:8 17 12 19 23 15 26 51 22 35 40 20



for(i=0;i<13;i++)



printf("%d ",number[i]);



printf("/n");







//弹出元素8



pop_heap(&number[0],&number[13],cmp);



//结果:12 17 15 19 23 20 26 51 22 29 35 40



for(i=0;i<13;i++)



printf("%d ",number[i]);



printf("/n");







sort_heap(&number[0],&number[12],cmp);



//结果不用说都知道是有序的了!



for(i=0;i<12;i++)



printf("%d ",number[i]);



return 0;



}

#include<algorithm>

#include<cstdio>

using namespace std;

bool cmp(int a,int b)

{

return a>b;

}

int main()

{

int i,number[20]={29,23,20,22,17,15,26,51,19,12,35,40};

make_heap(&number[0],&number[12]);

//结果是:51 35 40 23 29 20 26 22 19 12 17 15

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("/n");



make_heap(&number[0],&number[12],cmp);

//结果:12 17 15 19 23 20 26 51 22 29 35 40

for(i=0;i<12;i++)

printf("%d ",number[i]);

printf("/n");





number[12]=8; //加入元素8

//加入后调整

push_heap(&number[0],&number[13],cmp);

//结果:8 17 12 19 23 15 26 51 22 35 40 20

for(i=0;i<13;i++)

printf("%d ",number[i]);

printf("/n");



//弹出元素8

pop_heap(&number[0],&number[13],cmp);

//结果:12 17 15 19 23 20 26 51 22 29 35 40

for(i=0;i<13;i++)

printf("%d ",number[i]);

printf("/n");



sort_heap(&number[0],&number[12],cmp);

//结果不用说都知道是有序的了!

for(i=0;i<12;i++)

printf("%d ",number[i]);

return 0;

}



五、快速排序sort

包含头文件:algorithm

用法:

sort(a,a+n); //由小到大

sort(a,a+n,cmp); //cmp排序的比较函数

qsort( a , SIZE , sizeof(a[0])*SIZE , cmp);



cmp写法

1、int/char型

[c-sharp] view plaincopyprint?int cmp(const void * a , const void * b)



{



return * (int *)a - *(int *)b;



}

int cmp(const void * a , const void * b)

{

return * (int *)a - *(int *)b;

}



2、double型

[c-sharp] view plaincopyprint?int cmp(const void *a , const void * b)



{



return *(double *)a > *(double *)b ? 1 : -1;



}

int cmp(const void *a , const void * b)

{

return *(double *)a > *(double *)b ? 1 : -1;

}



3、char * 型



[c-sharp] view plaincopyprint?int cmp(const void * a,const void *b)



{



return strcmp( (char *)a , (char *)b );



}

int cmp(const void * a,const void *b)

{

return strcmp( (char *)a , (char *)b );

}



4、结构体一级排序

[c-sharp] view plaincopyprint?struct node



{



double data;



int other;



}s[100];



int cmp(const void *a , const void *b)



{



return *(node *)a->other - *(node *)a->other



}

struct node

{

double data;

int other;

}s[100];

int cmp(const void *a , const void *b)

{

return *(node *)a->other - *(node *)a->other

}



5、结构体二级排序

[c-sharp] view plaincopyprint?struct node



{



int x;



int y;



}s[100];



int cmp( const void *a , const void *b)



{



struct node *c=(node *)a;



struct node *d=(node *)b;



if( c->x != d->x) return c->x - d->x;



else return d->y – c->y;



}

struct node

{

int x;

int y;

}s[100];

int cmp( const void *a , const void *b)

{

struct node *c=(node *)a;

struct node *d=(node *)b;

if( c->x != d->x) return c->x - d->x;

else return d->y – c->y;

}



六、稳定快速排序stable_sort

包含头文件:algorithm

用法:

stable_sort (a,a+n); //由小到大

stable_sort (a,a+n,cmp); //cmp排序的比较函数



七、第K小数nth_element

包含头文件:algorithm

用法:nth_element(c,c+z,c+j);

解释:从c开始到c+j的数组中第K小元素,数组c第K个即第K小数,c[K-1]即为第K小数,且之前的都比这个数小。

八、KMP算法

KMP算法

[c-sharp] view plaincopyprint?void Get_next(char *a)



{



int j,k;



j=0;k=-1;



next[0]=-1;



while(a[j])



{



if(k==-1||a[j]==a[k])



{ k++;



j++;



next[j]=k;



}



else



k=next[k];



}



}

void Get_next(char *a)

{

int j,k;

j=0;k=-1;

next[0]=-1;

while(a[j])

{

if(k==-1||a[j]==a[k])

{ k++;

j++;

next[j]=k;

}

else

k=next[k];

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: