之前做过的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. x01.os.15: 看上去很美

    张碧晨在韩国学的不是技巧,而是基本功:气息!声音由气息托着,似真声而不是真声,似假声又不是假声,所以才能在动听的地方唱得更动听.编程也是一样,基本功很重要:内存!所谓的黑客高手,攻击的一大手段,便是利 ...

  2. discuz mysqli_connect() 不支持 advice_mysqli_connect

      看网友的解决方案是:下面2行去掉注释 ? 1 2 ;extension=php_mysql.dll ;extension=php_mysqli.dl 尝试修改 ? 1 #vi /etc/php.i ...

  3. shell编程入门

    背景知识 Shell 是用户与内核进行交互操作的一种接口,是 Linux 最重要的软件之一.目前最流行的 Shell 称为 bash Shell,bash Shell 脚本编程以其简洁.高效而著称,多 ...

  4. System.getProperty()引起的悲剧--您的主机中的软件中止了一个已建立的连接

    我已无法形容此刻我的心情.. 本来是已经写好的netty5的demo程序,server和client之间创建tcp长连接的..然后随便传点数据的简单demo..然后今天试了一下tcp粘包的例子,用到了 ...

  5. Binary search tree

    #ifndef __TREE_H #define __TREE_H #include <iostream> template<typename T> class TreeNod ...

  6. c++关于析构的那点小事(个人吐槽向

    #include<iostream> using namespace std; class test { int *i; public: test(int n) { i = new int ...

  7. (七)中介者模式-C++实现

    用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其解耦合松散而且可以独立地改变他们之间的交互. 中介者模式适合于 系统中不希望对象之间直接交互,即不希望类之间相互包含, ...

  8. ActionErrors和ActionError

    **ActionErrors和ActionError都是ActionMessage的子类,ActionError存放在 ActionErrors中,ActionError对象中的参数为配置文件中配置的 ...

  9. CF723D. Lakes in Berland[DFS floodfill]

    D. Lakes in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...