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. luogu 1045 麦森数

    题目大意: 从文件中输入P(1000<P<3100000),计算2^P−1的位数和最后500位数字(用十进制高精度数表示) 思路: 一道高精度练习题 其中位数是一个结论 位数=[P*log ...

  2. 湖南集训day8

    难度:☆☆☆☆☆☆☆ /* 可以先考虑一维,可知 模k意义下相同的前缀和任意两个相减都是k的倍数 问题等价于统计前缀何种模k相同的数的对数. 多维的时候二维前缀和,压行或者压列,n^3可以解决. */ ...

  3. 【知识总结】线性筛_杜教筛_Min25筛

    首先感谢又强又嘴又可爱脸还筋道的国家集训队(Upd: WC2019 进候选队,CTS2019 不幸 rk6 退队)神仙瓜 ( jumpmelon ) 给我讲解这三种筛法~~ 由于博主的鸽子属性,这篇博 ...

  4. flask 初始

    一.flask安装 这里提供两种安装方式: 第一种: pip3 install flask 第二种: pip3 install -i https://pypi.douban.com/simple/ f ...

  5. html基础代码演示2

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  6. [转]Android监听ListView里Button事件

    本文转自:http://blog.csdn.net/lovediji/article/details/6753349 public View getView(int position, View co ...

  7. the interview questions of sql server

    1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-0 ...

  8. Android 6.0一个完整的native service

     上一篇博客<Android 6.0 如何添加完整的系统服务(app-framework-kernel)>http://www.cnblogs.com/hackfun/p/7418902. ...

  9. 【PL/SQL】用星号拼出金字塔

    代码中首先声明了几个变量,然后使用嵌套循环去输出空格和星号,其中: 每层空格数=总层数-该层层数 每层星号数=当前层数*2-1 代码如下: declare v_number1 ); --外层循环控制金 ...

  10. JS高级——Object.prototype成员

    基本概念 成员 描述 Object.prototype.__proto__ 指向当对象被实例化的时候,用作原型的对象. Object.prototype.hasOwnProperty() 返回一个布尔 ...