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( ...
随机推荐
- Robert 的军队
题目描述 Winter is coming. Robert 是个昏庸的君主,整日沉迷于吃喝玩乐,终于,当寒冬降临,他不得不组 织军队来对抗敌人. 尽管如此,他仍然是个喜欢玩耍的人,还有点强迫症,他希望 ...
- PCB javascript解析钻孔(Excellon)格式实现方法
解析钻孔(Excellon)格式前首先得了解此格式,这样才能更好的解析呀. 一个钻孔里面包含的基本信息如下: 1.单位:公式mm,英制inch 2.省零方式:前省零,后省零 3.坐标方式:绝对坐标,相 ...
- 慕课网3-13编程练习:采用flex弹性布局制作页面主导航
小伙伴们,伸缩容器的属性我们已经学完了,接下来使用我们所学的伸缩容器属性完成下面的效果图. 要求: 1.logo.导航项.登录注册按钮这三项在水平和垂直方向上都对齐,而且他们之间的距离也相等. 2.导 ...
- find_in_set的用法(某个字段包含某个字符)
有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...
- Lambda表达式怎么写SQL中的in?
ambda表达式查询没有IN这个方法,可以变通一下,in查询的数组是否包含在映射对象里面的集合里 直接贴代码吧,一看就懂了 class Program { static void Main(strin ...
- 【知识总结】卡特兰数 (Catalan Number) 公式的推导
卡特兰数的英文维基讲得非常全面,强烈建议阅读! Catalan number - Wikipedia (本文中图片也来源于这个页面) 由于本人太菜,这里只选取其中两个公式进行总结. (似乎就是这两个比 ...
- mysql触发器的操作
一.创建触发器 1.创建有一条执行语句的触发器 CREATE TRIGGER trigger_name BEFORE|AFTER trigger_EVENT(INSERT|DELETE|UPDATE) ...
- firefox 附加组件栏安装
firefox 在升级到 30的版本后,发现附加组件栏不兼容了. 搜索组件,add-on bar 会得到一个 new add-on bar的组件,安装完后发现上面不显示ip, 后来才发现,应该安装Th ...
- Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
项目中用到了Recyclerview,在第一行代码中学到了一种相对来说简单的点击事件方法,可是这种点击事件是在adapter中写的,没有教怎么更新item的ui和更新Recyclerview之外的控件 ...
- 开发日记(项目中SQL查询的优化)
今天发现自己之前写的一些SQL查询在执行效率方面非常不理想,于是尝试做了些改进. 需求为查询国地税表和税源表中,国税有而税源没有的条目数,之前的查询如下: SELECT COUNT(NAME) ...