HDOJ1251(前缀匹配---分块查找&map应用)
2024-09-07 02:36:57
分块查找算法
#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 ;
}
最新文章
- centos7安装svn1.8.16
- c#去除List中的重复项
- PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现
- Ural 1018 (树形DP+背包+优化)
- 详解DHV:怎么具体展示高价值
- Storyboard的使用以及使用多个Storyboard的方法
- 使用WebMatrix发布网站到Windows Azure
- Vigen&#232;re 密码NOIP 2012 提高组 第一天 第一题
- Linux makefile教程之书写命令四[转]
- search help 概述
- ESP8266开发课堂之 - 建立一个新项目
- 表现层技术以及Freemaker使用教程
- Usermod:user oracle is currently logged in 家目录不能改变解决方法
- 第1阶段——关于u-boot目标文件start.o中.globl 和.balignl理解(3)
- Android实战之ListView复选框
- [linux] tcpdump抓包案例
- onclick传对象
- Python 使用 xlwings 往 excel中写入一列数据的两种方法
- python中和生成器协程相关的yield之最详最强解释,一看就懂(一)
- 每日英语:Missing at Mobile World Congress: Innovation
热门文章
- jvm本身的多线程机制
- 【python】-- pymsql 外键
- Linux使用yum安装rpm包
- lzugis—搭建属于自己的小型的版本号控制SVN
- eclipse svn 分支合并到主干
- ssm框架与shiro的整合小demo,用idea开发+maven管理
- C#线程使用学习
- cache:annotation-driven"; 的前缀 ";cache"; 未绑定
- hadoop自带例子SecondarySort源码分析MapReduce原理
- Data Structure Array: Move all zeroes to end of array