之前做过的oj, acm题目忘了很多了, 又要开始要刷题了, go on!

#1260 : String Problem I

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

我们有一个字符串集合S,其中有N个两两不同的字符串。

还有M个询问,每个询问给出一个字符串w,求有多少S中的字符串可以由w添加恰好一个字母得到。

字母可以添加在包括开头结尾在内的任意位置,比如在"abc"中添加"x",就可能得到"xabc", "axbc", "abxc", "abcx".这4种串。

输入

第一行两个数N和M,表示集合S中字符串的数量和询问的数量。

接下来N行,其中第i行给出S中第i个字符串。

接下来M行,其中第i行给出第i个询问串。

所有字符串只由小写字母构成。

数据范围:

N,M<=10000。

S中字符串长度和<=100000。

所有询问中字符串长度和<=100000。

输出

对每个询问输出一个数表示答案。

样例输入
3 3
tourist
petr
rng
toosimple
rg
ptr
样例输出
0
1
1

二分查找,在大型数组中很常用。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
using namespace std;
const int maxn = 10005; struct Node{
int len;
string s;
}nd[maxn];
int n,m; int Match(const string &s1, const string &s2){
bool isPlus = true;
int j = 0;
for(int i=0; i<s1.length(); i++){
if(s1[i] != s2[j]){
if(isPlus){
isPlus = false;
}else{
return false;
}
}else{
j++;
}
}
return true;
} int cmp(const void* a, const void* b){
Node* aa = (Node *)a;
Node* bb = (Node *)b;
return aa->len - bb->len;
} int main(){
freopen("in.txt", "r", stdin); int i,j, mid, left, right, target_len, test_start, cnt;
string target;
scanf("%d %d", &n, &m );
getchar();
for(int i=0; i<n; i++){
cin>>nd[i].s;
nd[i].len = nd[i].s.length();
}
qsort(nd, n, sizeof(nd[0]), cmp);
for(i=0; i<m; i++){
cin>>target;
target_len = target.length() + 1;
left = 0; right = n-1;
test_start = -1;
while(left <= right){
mid = left + (right - left)/2;
if(nd[mid].len == target_len){
j = mid-1;
while(j>=0 && nd[j].len == nd[mid].len){
j--;
}
test_start = j + 1;
break;
}else if(nd[mid].len > target_len){
right = mid - 1;
}else{
left = mid + 1;
}
}
if(test_start == -1){
printf("%d\n", 0);
}else{
cnt = 0;
for(j=test_start; nd[j].len == target_len; j++){
if(Match(nd[j].s, target)){
cnt++;
}
}
printf("%d\n", cnt);
}
}
return 0;
}

  

hihocoder 1260的更多相关文章

  1. hihocoder #1260 : String Problem I

    题目链接   时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 我们有一个字符串集合S,其中有N个两两不同的字符串. 还有M个询问,每个询问给出一个字符串w,求有多少S中的 ...

  2. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  3. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  4. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  5. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  6. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  7. 【DP】HDU 1260

    HDU 1260 Tickets 题意:有N个人要买票,你可以一个一个人卖票,时间分别为Xs,也可以相邻两个人一起卖票,时间为Ys,从早上八点开始卖票,问你何时最早将N个人的票卖完. 思路:解决情况是 ...

  8. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  9. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

随机推荐

  1. Episode 388: Testing vs Monitoring

    A user-facing bug causes search results to be unavailable for your service. Someone suggests adding ...

  2. Windows 运行时组件

    Windows 运行时组件是自包含对象,可将其实例化,并可采用任一语言使用它,包括 C#.Visual Basic.JavaScript 和 C++. 你可以使用 Visual Studio 和 C# ...

  3. 在公司内网上创建自己的 OSM.Planet 街道级别地图服务器及其客户端程序

    转自我的BLOG http://blog.csdn.net/goldenhawking/article/details/6402775  最近经过陛下点拨,涉猎了“OpenStreetMap”,做了不 ...

  4. Android驱动入门-LED--HAL硬件抽象层程序设计①

    硬件平台: FriendlyARM Tiny4412 Cortex-A9 操作系统: UBUNTU 14.04 LTS 时间:2016-09-21  16:15:26 设计HAL硬件抽象层程序,则需要 ...

  5. [No000050]练习一万小时便能成为天才

    练习一万小时便能成为天才 世界上顶尖的记忆高手都是训练出来的! 加拿大畅销书作家麦尔坎·葛拉威尔在<异数>一书中指出:"人们眼中的天才之所以卓越非凡,并非天资超人一等,而是付出了 ...

  6. 斗地主(Noip2015Day1T3)

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  7. .Net程序员学习Linux最简单的方法

    有很多关于Linux的书籍,博客.大多数都会比较“粗暴“的将一大堆的命令塞给读者,从而使很多.NET程序员望而却步.未入其门就路过了. 所以我设想用一种更为平滑的学习方式, 就是在学习命令时,先用纯语 ...

  8. nginx利用geo模块做限速白名单以及geo实现全局负载均衡的操作记录

    geo指令使用ngx_http_geo_module模块提供的.默认情况下,nginx有加载这个模块,除非人为的 --without-http_geo_module.ngx_http_geo_modu ...

  9. C语言操作内存

    炒了个栗子 #include <stdio.h> #define N1 (5) #define N2 (7) #define N3 (3) int main(){ int *ip; int ...

  10. Structs2配置文件相关说明

    1,structs.xml文件解析 <package name="front" namespace="/front" extends="stru ...