hdu6096 String
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
1
4 4
aba
cde
acdefa
cdef
a a
cd ef
ac a
ce f
Sample Output
2
1
1
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的更多相关文章
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
- js报错: Uncaught RangeError: Invalid string length
在ajax请求后得到的json数据,遍历的时候chrome控制台报这个错误:Uncaught RangeError: Invalid string length,在stackoverflow查找答案时 ...
- c# 字符串连接使用“+”和string.format格式化两种方式
参考文章:http://www.liangshunet.com/ca/201303/218815742.htm 字符串之间的连接常用的两种是:“+”连接.string.format格式化连接.Stri ...
- 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
之前以为BinaryWriter写string会严格按构造时指定的编码(不指定则是无BOM的UTF8)写入string的二进制,如下面的代码: //将字符串"a"写入流,再拿到流的 ...
- JavaScript中String对象的方法介绍
1.字符方法 1.1 charAt() 方法,返回字符串中指定位置的字符. var question = "Do you like JavaScript?"; alert(ques ...
- 在多线程编程中lock(string){...}隐藏的机关
常见误用场景:在订单支付环节中,为了防止用户不小心多次点击支付按钮而导致的订单重复支付问题,我们用 lock(订单号) 来保证对该订单的操作同时只允许一个线程执行. 这样的想法很好,至少比 lock( ...
随机推荐
- [置顶]
Snow的追寻
题目描述 Snow终于得知母亲是谁,他现在要出发寻找母亲. 王国中的路由于某种特殊原因,成为了一棵有n个节点的根节点为1的树,但由于"Birds are everywhere.", ...
- JSP-Runoob:JSP 发送邮件
ylbtech-JSP-Runoob:JSP 发送邮件 1.返回顶部 1. JSP 发送邮件 虽然使用JSP实现邮件发送功能很简单,但是需要有JavaMail API,并且需要安装JavaBean A ...
- PCB Genesis 外形加内角孔实现方法
在PCB工程制作CAM时,经常会遇到外形拐角处直角的,而客户对内角是要求,比如最大内角要求R0.5mm或者不接受内角, 但成型方式为铣方式,又不是啤板成型,那怎么处理才可以达到要求效果呢,在这里介绍2 ...
- Unity 图形学 基础知识总结
1. 渲染流水线 三大块:应用阶段,几何阶段,光栅化阶段 渲染图元 顶点信息 GPU流水线 顶点数据=> 顶点着色器 ...
- codevs4511信息传递(Tarjan求环)
题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...
- day24 03 多继承
day24 03 多继承 正常的代码中 单继承==减少了代码的重复 继承表达的是一种 子类是父类的关系 1.简单的多继承关系 A,B,C,D四个类,其中D类继承A,B,C三个父类,因此也叫多继承,子 ...
- CSS-类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- ACM_求交集
求交集 Time Limit: 2000/1000ms (Java/Others) Problem Description: 输入集合A和B,按大小顺序输出A和B的交集. Input: 输入包含多组测 ...
- C# asp.net repeater实现排序功能,自动排序,点击头部排序,点击列排序
在网上看到好多关于repeater排序的,自己动手用了,发现一些问题,贴源码后把发现的问题以及解决方法给出 repeater实现排序功能(单击升序排列,再单击降序排列).原理很简单,在<TD&g ...
- Android文件操作报open failed: EBUSY (Device or resource busy)
Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...