寒假集训作业(7)——排序查找
来源:互联网 发布:centos下关闭selinux 编辑:程序博客网 时间:2024/05/21 10:43
利用STL库进行的查找:
#include <string>#include <list>#include <algorithm>#include <iostream>using namespace std;int a[100010],b[100010];int main(){ list <int> search; list <int>::iterator searcher; int n,m,temp1,temp2; cin>>n>>m; for(int i=0;i<=n-1;i++) { temp1=0; cin>>temp1; search.push_back(temp1); } for(int i=0;i<=m-1;i++) { temp2=0; cin>>temp2; searcher=find(search.begin(),search.end(),temp2); if (searcher == search.end()) { cout << "NO" << endl; } else { cout << "YES" << endl; } }}
以上的代码又可以当作模板。大概测试了一下,100000组数据,查找100000个,只需十秒。但仍然超时= =
以下是hash查找,即把需要查找的数字作为新数组hash的下标
#include <stdio.h> const int maxn = 1000001; int hash[maxn]; int main() { int t,m; scanf("%d %d", &t, &m); for(int i = 0; i < t; i++) { int n; scanf("%d", &n); hash[n]++; } for(int i = 0; i < m; i++) { int n; scanf("%d", &n); if(hash[n]) { puts("YES"); } else puts("NO"); } return 0; }
#include<string.h>#include<iostream>using namespace std;int main(){ int i,j,p,k,length; char s[110]; while(cin>>s) { k=0; length=strlen(s); for(i=k; i<length; i++) { if(s[k]<s[i])k=i; } for(j=0; j<=k; j++) cout<<s[j]; cout<<"(max)"; for(i=k+1; i<length; i++) { if(s[i]==s[k]) { cout<<s[i]; cout<<"(max)"; } else cout<<s[i]; } cout<<endl; }}
求最大元素(ASCII)
排序问题 猥琐龌龊淫荡的浪川把时间限定到了15ms,用cin竟然总是25ms,以后还是改用scanf吧= =
模板是MoreWindows写的,链接如下:
http://blog.csdn.net/morewindows/article/details/6678165
#include <iostream>#include <algorithm>#include <functional>#include <cstdio>using namespace std;int a[1000010];//将有二个有序数列a[first...mid]和a[mid...last]合并。void mergearray(int a[], int first, int mid, int last, int temp[]){int i = first, j = mid + 1;int m = mid, n = last;int k = 0;while (i <= m && j <= n){if (a[i] >= a[j])temp[k++] = a[i++];elsetemp[k++] = a[j++];}while (i <= m)temp[k++] = a[i++];while (j <= n)temp[k++] = a[j++];for (i = 0; i < k; i++)a[first + i] = temp[i];}void mergesort(int a[], int first, int last, int temp[]){if (first < last){int mid = (first + last) / 2;mergesort(a, first, mid, temp); //左边有序mergesort(a, mid + 1, last, temp); //右边有序mergearray(a, first, mid, last, temp); //再将二个有序数列合并}}/*bool MergeSort(int a[], int n){int *p = new int[n];if (p == NULL)return false;mergesort(a, 0, n - 1, p);delete[] p;return true;}*/int temp[1000010];int main(){ //freopen("D:\\aaa.txt","r",stdin); //freopen("D:\\ans2.txt","w",stdout); int n; while(~scanf("%d",&n)) { for(int i=0;i<=n-1;i++) { scanf("%d",&a[i]); } mergesort(a,0,n+1,temp); //sort(a,a+n+1,greater<int>()); for(int i=0;i<=n-1;i++) { if(i!=n-1) printf("%d ",a[i]); else printf("%d\n",a[i]); } }}
经测试,sort 跟mergesort在这组数据中是差不多的。
- 寒假集训作业(7)——排序查找
- 寒假集训作业(1)——结构体
- 寒假集训作业(2)——链表
- 寒假集训作业(3)——栈与队列
- 寒假集训作业(4)——字符串
- 寒假集训作业(6)——动态规划初步
- 寒假集训作业(8)——数学问题
- 寒假集训第四天——排序
- 寒假集训——二分查找,哈希查找
- 寒假集训作业(9)——动态规划初步(2)
- 寒假集训作业(5)——递归与递推
- 寒假集训——KMP
- 寒假集训——AreYouBusy
- 寒假集训——饭卡
- 寒假集训——字典树(模板)
- 寒假集训附加题目题解报告(7)——"数学好奇心"
- 寒假集训——二叉树
- 寒假集训——Find Metal Mineral
- The method getContextPath() from the type HttpServletRequest refers to the missing type
- NET 不规则、可改变大小、边框半透明窗体设计
- PB中取得数据窗口所有列名以及列标题名称
- android通过json解析到的路径后异步加载网络图片
- 单片机的调试的快捷方式
- 寒假集训作业(7)——排序查找
- struts2下载文件名设置和中文下载时乱码如何设置
- 白帽子讲安全 的一些记录点
- SQLite剖析(10):异步IO模式、共享缓存模式和解锁通知
- 数组指针和指针数组
- Javascript 动态创建表格(还能删除行列)
- 客户端与服务器通过gson解析json数据
- Phonegap-2.2.0的word Plugin插件编写
- 设置sqlplus中的Edit编辑模式