String 
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)

Problem Description 
Bob has a dictionary with N words in it. 
Now there is a list of words in which the middle part of the word has continuous letters disappeared. The middle part does not include the first and last character. 
We only know the prefix and suffix of each word, and the number of characters missing is uncertain, it could be 0. But the prefix and suffix of each word can not overlap. 
For each word in the list, Bob wants to determine which word is in the dictionary by prefix and suffix. 
There are probably many answers. You just have to figure out how many words may be the answer.

Input 
The first line of the input gives the number of test cases T; T test cases follow. 
Each test case contains two integer N and Q, The number of words in the dictionary, and the number of words in the list. 
Next N line, each line has a string Wi, represents the ith word in the dictionary (0<|Wi|≤100000) 
Next Q line, each line has two string Pi , Si, represents the prefix and suffix of the ith word in the list (0<|Pi|,|Si|≤100000,0<|Pi|+|Si|≤100000) 
All of the above characters are lowercase letters. 
The dictionary does not contain the same words.

Limits 
T≤5 
0< N,Q≤100000 
∑Si+Pi≤500000 
∑Wi≤500000

Output 
For each test case, output Q lines, an integer per line, represents the answer to each word in the list.

Sample Input 

4 4 
aba 
cde 
acdefa 
cdef 
a a 
cd ef 
ac a 
ce f

Sample Output 



0

分析:比赛时未做出,还是太菜。。

   事实证明只要不是太暴力都能过吧,大概。。

   按题解说,前缀和后缀分别按字典序排序,然后对询问二分到端点所在区间;

   这样问题转化为了扫描线,可以用数据结构离线查询;

   然后因为有重叠,所以枚举重叠长度哈希即可;

   用了string,map,发现其实评测机还是很快的,orz;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>
#include <ctime>
#define rep(i,m,n) for(i=m;i<=(int)n;i++)
#define mod 998244353
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
#define ls rt<<1
#define rs rt<<1|1
#define all(x) x.begin(),x.end()
const int maxn=1e5+;
const int N=5e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qmul(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=(f+p)%mo;p=(p+p)%mo;q>>=;}return f;}
ll qpow(ll p,ll q,ll mo){ll f=;while(q){if(q&)f=f*p%mo;p=p*p%mo;q>>=;}return f;}
int n,m,k,t,ret[maxn],dq[maxn],td1[maxn],td2[maxn],id1[maxn],id2[maxn];
map<unsigned ll,int>pq;
unsigned ll h[][maxn],xp[maxn];
string a[maxn],b[maxn],c,d;
void init()
{
xp[]=;
for(int i=;i<=maxn-;i++)xp[i]=xp[i-]*;
}
void ha(string p)
{
unsigned ll now=;
int len=p.length();
for(int i=len-;i>=;i--)now=now*+p[i];
pq[now]=;
}
void ha(string p,int tp,int len)
{
h[tp][len]=;
for(int i=len-;i>=;i--)h[tp][i]=h[tp][i+]*+p[i];
}
bool cmp1(int x,int y){return a[x]<a[y];}
bool cmp2(int x,int y){return b[x]<b[y];}
void add(int x,int y)
{
while(x<=n)dq[x]+=y,x+=x&(-x);
}
int get(int x)
{
int ret=;
while(x)ret+=dq[x],x-=x&(-x);
return ret;
}
vi qu[maxn],pos[maxn];
struct node
{
int x,y,z,w;
}q[maxn];
int main()
{
int i,j;
init();
scanf("%d",&t);
while(t--)
{
pq.clear();
scanf("%d%d",&n,&m);
rep(i,,n)
{
cin>>a[i];
b[i]=a[i];
reverse(b[i].begin(),b[i].end());
ha(a[i]);
id1[i]=id2[i]=i;
dq[i]=;
}
sort(id1+,id1+n+,cmp1);
sort(id2+,id2+n+,cmp2);
sort(a+,a+n+);
sort(b+,b+n+);
rep(i,,n)td1[id1[i]]=i,td2[id2[i]]=i;
rep(i,,n)pos[td1[i]].pb(td2[i]);
rep(i,,m)
{
ret[i]=;
cin>>c>>d;
int len1=c.length(),len2=d.length();
ha(c,,len1);
ha(d,,len2);
int l=len1-,r=;
while(l>=&&r<len2)
{
if(h[][l]-h[][len1]*xp[len1-l]==h[][]-h[][r+]*xp[r+])
{
unsigned ll tmp=h[][]-h[][l]*xp[l]+h[][]*xp[l];
if(pq.count(tmp))ret[i]--;
}
--l,++r;
}
reverse(d.begin(),d.end());
int pos1=,pos2=,pos3=,pos4=;
l=,r=n;
while(l<=r)
{
int mid=l+r>>;
if(a[mid]>=c)pos1=mid,r=mid-;
else l=mid+;
}
if(pos1&&a[pos1].substr(,len1)!=c)pos1=;
l=,r=n;
while(l<=r)
{
int mid=l+r>>;
if(a[mid]<=c||a[mid].substr(,len1)==c)pos2=mid,l=mid+;
else r=mid-;
}
l=,r=n;
while(l<=r)
{
int mid=l+r>>;
if(b[mid]>=d)pos3=mid,r=mid-;
else l=mid+;
}
if(pos3&&b[pos3].substr(,len2)!=d)pos3=;
l=,r=n;
while(l<=r)
{
int mid=l+r>>;
if(b[mid]<=d||b[mid].substr(,len2)==d)pos4=mid,l=mid+;
else r=mid-;
}
if(pos1&&pos3)
{
q[i]=node{pos1,pos3,pos2,pos4};
qu[pos1-].pb(-i),qu[pos2].pb(i);
}
}
rep(i,,n)
{
rep(j,,pos[i].size()-)add(pos[i][j],);
rep(j,,qu[i].size()-)
{
int x=qu[i][j];
if(x<)
{
x=-x;
ret[x]-=get(q[x].w)-get(q[x].y-);
}
else ret[x]+=get(q[x].w)-get(q[x].y-);
}
}
rep(i,,m)printf("%d\n",ret[i]);
rep(i,,n)qu[i].clear(),pos[i].clear();
}
return ;
}

hdu6096 String的更多相关文章

  1. 透过WinDBG的视角看String

    摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...

  2. JavaScript String对象

    本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...

  3. ElasticSearch 5学习(9)——映射和分析(string类型废弃)

    在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...

  4. [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密

    string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...

  5. js报错: Uncaught RangeError: Invalid string length

    在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...

  6. c# 字符串连接使用“+”和string.format格式化两种方式

    参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...

  7. 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed

    之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...

  8. JavaScript中String对象的方法介绍

    1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...

  9. 在多线程编程中lock(string){...}隐藏的机关

    常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...

随机推荐

  1. go语言笔记——map map 默认是无序的,不管是按照 key 还是按照 value 默认都不排序

    示例 8.1 make_maps.go package main import "fmt" func main() { var mapLit map[string]int //va ...

  2. [python基础] python生成wordcloud并保存

    1.核心包 #jieba.pandas用来处理数据,数据源以xls格式存储的,这里用pandas进行处理import jieba from jieba import analyse import pa ...

  3. JAVA基础(多线程Thread和Runnable的使用区别(转载)

    转自:http://jinguo.iteye.com/blog/286772 Runnable是Thread的接口,在大多数情况下“推荐用接口的方式”生成线程,因为接口可以实现多继承,况且Runnab ...

  4. tinymce 富文本编辑器 编写资料

    tinymce官方文档: 粘贴图片插件 博客搬运地址 使用Blob获取图片并二进制显示实例页面 tinymce自动调整插件 是时候掌握一个富文本编辑器了——TinyMCE(1) XMLHttpRequ ...

  5. nodejs操作mysql

    var mysql = require('mysql');var pool = mysql.createPool({ host: 'localhost', user: 'root', password ...

  6. jquery得到焦点和失去焦点

    鼠标在搜索框中点击的时候里面的文字就消失了,经常会用到搜索框的获得焦点和失去焦点的事件,接下来介绍一下具体代码,感兴趣的朋友额可以参考下   input失去焦点和获得焦点 鼠标在搜索框中点击的时候里面 ...

  7. hdu2030

    http://acm.hdu.edu.cn/showproblem.php?pid=2030 #include<stdio.h> #include<math.h> #inclu ...

  8. 题解报告:hdu 2516 取石子游戏(斐波那契博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个, ...

  9. Docker学习系列(一):windows下安装docker(转载)

    本文目录如下: windows按照docker的基本要求 具体安装步骤 开始使用 安装远程连接工具连接docker 安装中遇到的问题 Docker的更新 Docker中的jupyter windows ...

  10. 一键生成Spring MVC + MyBatis + maven项目

    首先创建一个新的maven项目,在src/main/java创建一个类Test 然后在Test复制以下代码: import java.io.*; import java.sql.Connection; ...