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( ...
随机推荐
- 洛谷P2303 [SDOi2012]Longge的问题
题目背景 SDOi2012 题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). ...
- 在LNMP或Nginx上配置NameCheap免费SSL证书
- Secure CRT中解决vim高亮设置的方法
此文主要是解决vim编程中高亮显示的.原因是: 1.默认情况下,SecureCRT是有自己的终端显示颜色.这样在我们编程中不利于阅读内容. 2.我们必须到Linux系统中进行改进才能真正解决这样的问题 ...
- jsp简单学习总结
以下均为jsp页面 1:<jsp:include page="index.jsp"/>相当于嵌入一个页面.还有一种是<frame src="main_l ...
- [NOI1997] 积木游戏(dp)
COGS 261. [NOI1997] 积木游戏 http://www.cogs.pro/cogs/problem/problem.php?pid=261 ★★ 输入文件:buildinggame ...
- Java里边什么是值传递和引用传递?两个有什么区别
学过java基础的人都知道,在java中参数的传递过程中有值传递和应用传递,那么这两个到底有什么区别呢,下面我通过例子为大家详细的介绍下. 我们都知道Java中有八种数据类型,基础数据类型分别是:by ...
- AJAX json集合传入Controller后台
HTML代码 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- SQL数据库还原的二种方式和区别
1.数据库还原 在SQL中,直接选择选择“还原数据库”:选中.bak 文件即可. 2.生成脚本 新建同样的DB名字,在SQL打开脚本,执行脚本语言.数据库里面就会自动填充内容.
- mongoDB的基本用法
一.MongoDB初识 什么是MongoDB MongoDB是一个基于分布式文件存储的数据库.由c++语言编写.旨在为web应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库 ...
- 6CSS之文本
CSS文本:文本缩进(text-indent).文本对齐(text-align).文本修饰(text-decoration).文本大小写(text-transform).字符距离(letter-spa ...