之前做过的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;
}

  

最新文章

  1. &gt;hibernate.cfg.xml的一些常用配置
  2. JavaScript DOM 编程艺术&#183;setInterval与setTimeout的动画实现解析
  3. 李洪强iOS开发之OC[008] -创建一个对象并访问实例变量
  4. ASP.NET线程与异步
  5. CSU 1160(进制问题)
  6. jquery1.9学习笔记 之层级选择器(二)
  7. CDZSC_2015寒假新人(2)——数学 H
  8. - 高级篇:二,IL设置静态属性,字段和类型转换
  9. ASP.NET MVC5路由系统机制详细讲解
  10. MyEclipse常用操作
  11. install pytorch
  12. 总结过滤器,监听器,servlet的异同点,已经执行顺序。
  13. 20160226.CCPP体系详解(0036天)
  14. WSDL文件
  15. 关于this的指向
  16. C# 匿名类型如何使用
  17. 把旧系统迁移到.Net Core 2.0 日记(4) - 使用EF+Mysql
  18. 【noip模拟赛3】编码
  19. Effective STL 学习笔记 Item 30: 保证目标区间足够大
  20. go语言基础之 if else的使用

热门文章

  1. TCP连接建立系列 — 服务端接收ACK段(一)
  2. Linux 下从头再走 GTK+-3.0 (二)
  3. android apk 防止反编译技术第四篇-对抗JD-GUI
  4. C#迭代重载等
  5. oracle:sql函数
  6. Ahead-of-time compilation(AOT)
  7. RFID基础知识
  8. ES5特性之Object.freeze
  9. C语言操作内存
  10. text