HDOJ1251(前缀匹配---分块查找&map应用)
2015-08-04 17:00
253 查看
分块查找算法
利用STL库中的map解决
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int SIZE=1300000+16; const int BLOCKS=50000; //块的大小 char word[SIZE][11]; char pre[20]; int Num[27]; bool isPre(char mo[],char so[]) { int i=0,j=0; while(mo[i]&&so[j]) { if(mo[i]!=so[j]) return false; i++; j++; } if(so[j]=='\0') return true; return false; } int main() { int cnt=0; char fin[11]={'\0'}; while(gets(fin)) { if(fin[0]=='\0') break; int key=fin[0]-'a'; int pos=key*BLOCKS+Num[key]; Num[key]++; strcpy(word[pos],fin); } while(scanf("%s",pre)!=EOF) { int num=0; int key=(pre[0]-'a'); int start=key*BLOCKS; for(int i=start;i<start+Num[key];i++) { if(isPre(word[i],pre)) num++; } printf("%d\n",num); } return 0; }
利用STL库中的map解决
#include<iostream> #include<cstdio> #include<string> #include<map> using namespace std; map<string, int> word; int main() { string x=""; while(true) { char a; scanf("%c",&a); if(a=='\n') { scanf("%c",&a); x=""; } if(a=='\n') break; x+=a; word[x]++; } string pre; while(cin>>pre) { cout<<word[pre]<<endl; } //感受到STL的威力了么 return 0; }
相关文章推荐
- leetcode-Minimum Path Sum
- Python3中目录操作
- CPU缓存刷新的误解
- Linux 回调函数
- 9.3栈和队列(六)——队列
- docker删除None镜像
- 图像处理与视觉计算的网络资源和一些评述(转)
- android之hello world
- web切图技巧
- android知识点回顾
- jquery学习
- spring mvc controller间跳转 重定向 传参
- JRebel+myeclipse 热部署
- ASP.NET每一个页面首次访问超级慢
- caffe 安装记录
- hdu 2643 拓扑排序加优先队列
- spring aop 如何切面到mvc 的controller
- poj-1062昂贵的聘礼-Bellman-F
- LeetCode#43 Multiply Strings
- JQuery onload、ready概念介绍及使用方法