分块查找算法

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int SIZE=+;
const int BLOCKS=; //块的大小
char word[SIZE][];
char pre[];
int Num[];
bool isPre(char mo[],char so[])
{
int i=,j=;
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=;
char fin[]={'\0'};
while(gets(fin))
{
if(fin[]=='\0')
break;
int key=fin[]-'a';
int pos=key*BLOCKS+Num[key];
Num[key]++;
strcpy(word[pos],fin);
} while(scanf("%s",pre)!=EOF)
{
int num=; int key=(pre[]-'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 ;
}

利用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 ;
}

最新文章

  1. centos7安装svn1.8.16
  2. c#去除List中的重复项
  3. PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现
  4. Ural 1018 (树形DP+背包+优化)
  5. 详解DHV:怎么具体展示高价值
  6. Storyboard的使用以及使用多个Storyboard的方法
  7. 使用WebMatrix发布网站到Windows Azure
  8. Vigen&#232;re 密码NOIP 2012 提高组 第一天 第一题
  9. Linux makefile教程之书写命令四[转]
  10. search help 概述
  11. ESP8266开发课堂之 - 建立一个新项目
  12. 表现层技术以及Freemaker使用教程
  13. Usermod:user oracle is currently logged in 家目录不能改变解决方法
  14. 第1阶段——关于u-boot目标文件start.o中.globl 和.balignl理解(3)
  15. Android实战之ListView复选框
  16. [linux] tcpdump抓包案例
  17. onclick传对象
  18. Python 使用 xlwings 往 excel中写入一列数据的两种方法
  19. python中和生成器协程相关的yield之最详最强解释,一看就懂(一)
  20. 每日英语:Missing at Mobile World Congress: Innovation

热门文章

  1. jvm本身的多线程机制
  2. 【python】-- pymsql 外键
  3. Linux使用yum安装rpm包
  4. lzugis—搭建属于自己的小型的版本号控制SVN
  5. eclipse svn 分支合并到主干
  6. ssm框架与shiro的整合小demo,用idea开发+maven管理
  7. C#线程使用学习
  8. cache:annotation-driven&quot; 的前缀 &quot;cache&quot; 未绑定
  9. hadoop自带例子SecondarySort源码分析MapReduce原理
  10. Data Structure Array: Move all zeroes to end of array