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

题意:

  给你n个母串,m个询问

  每次询问给你一个前缀,后缀

  问你有多少个母串的前缀,后缀等于当前,且不相交

题解:

  先将所有母串正串,反串排序,那么每个询问的前缀后缀,会对应存在于两段区间

  现在要查询的就是这两个区间同时存在哪些母串数量

  将母串在正串,反串存在的位置x,y看作一个点,查询的看作一个区间,这个就是平面上一个矩阵包含多少个点,用线段树+扫描线解决

  有一种情况是重复的了比如 母串含有 aaa,查询aa aa

  这个时候就遍历重叠的是哪一部分,hash去重就行了

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, M = 1e3+,inf = 2e9; const ULL mod = 1004535809ULL;
int n,m,ans[N],t;
struct ss{
string s;
int id;
}a[N],b[N];
string c[N],d[N];
struct Point{
int x,y,id;
bool operator < (const Point &j) const {
if(x == j.x) return y < j.y;
else return x < j.x;
}
}p[N]; bool cmp(ss s1,ss s2) {
return s1.s < s2.s;
}
struct Que{
int top,down,x,type,qid;
bool operator < (const Que &j) const {
if( x == j.x)
return type > j.type;
else return x < j.x;
}
}Q[N]; int sum[N * ];
void build(int i,int ll,int rr) {
sum[i] = ;
if(ll == rr) return ;
build(ls,ll,mid);build(rs,mid+,rr);
}
void update(int i,int ll,int rr,int x) {
if(ll == rr) {
sum[i] += ;
return ;
}
if(x <= mid) update(ls,ll,mid,x);
else update(rs,mid+,rr,x);
sum[i] = sum[ls] + sum[rs];
}
int ask(int i,int ll,int rr,int x,int y) {
if(ll == x && rr == y) return sum[i];
if(y <= mid) return ask(ls,ll,mid,x,y);
else if(x > mid) return ask(rs,mid+,rr,x,y);
else return ask(ls,ll,mid,x,mid) + ask(rs,mid+,rr,mid+,y);
}
map<string ,int > mp;
ULL sqr[N]; void init() {
mp.clear();
for(int i = ; i <= m; ++i) ans[i] = ;
}
int main() {
int T;
sqr[] = 1LL;
for(int i = ; i < N; ++i) sqr[i] = sqr[i-] * mod;
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
init();
for(int i = ; i <= n; ++i) {
cin>>a[i].s;a[i].id = i;
b[i] = a[i];
reverse(b[i].s.begin(),b[i].s.end());
c[i] = a[i].s;
d[i] = b[i].s; mp[c[i]] += ;
}
sort(a+,a+n+,cmp);
sort(b+,b+n+,cmp); for(int i = ; i <= n; ++i)
p[a[i].id].x = i,p[b[i].id].y = i; sort(d+,d+n+);
sort(c+,c+n+);
int cnt = ;
for(int i = ; i <= m; ++i) {
cin>>c[]>>d[];
reverse(d[].begin(),d[].end());
int l = lower_bound(c+,c+n+,c[]) - c;
c[] += ('z'+);
int r = lower_bound(c+,c+n+,c[]) - c - ;
c[].erase(--c[].end());
int l1 = lower_bound(d+,d+n+,d[]) - d; d[] += ('z'+);
int r1 = lower_bound(d+,d+n+,d[]) - d - ;
d[].erase(--d[].end());
reverse(d[].begin(),d[].end());
if(l > r || l1 > r1) ans[i] = ;
else {
++cnt;
Q[cnt].top = r1;
Q[cnt].x = l-;
Q[cnt].down = l1;
Q[cnt].type = -;
Q[cnt].qid = i; ++cnt;
Q[cnt].top = r1;
Q[cnt].x = r;
Q[cnt].down = l1;
Q[cnt].type = ;
Q[cnt].qid = i;
} for(int j = c[].length() - ,k = ; k < d[].length() && c[].begin()!=c[].end(); j = c[].length() - ,++k)
{
// cout<<c[0][j]<<" "<<d[0][k]<<endl;
if(c[][j] == d[][k])
{
c[].erase((--c[].end()));
ans[i] -= mp[c[] + d[]];
}else break;
} }
for(int i = ; i <= n; ++i) {
++cnt;
Q[cnt].top = p[i].y;
Q[cnt].x = p[i].x;
Q[cnt].type = ;
}
build(,,n);
sort(Q+,Q+cnt+);
for(int i = ; i <= cnt; ++i) {
if(Q[i].type == ) {
update(,,n,Q[i].top);
}
if(Q[i].type == ) {
ans[Q[i].qid] += ask(,,n,Q[i].down,Q[i].top);
}
if(Q[i].type == -) {
ans[Q[i].qid] -= ask(,,n,Q[i].down,Q[i].top);
}
}
for(int i = ; i <= m; ++i) {
printf("%d\n",ans[i]);
}
}
return ;
} /*
1
1 1
aaa
aa aa
*/

HDU 6096 String 排序 + 线段树 + 扫描线的更多相关文章

  1. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  2. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

  3. HDU 3265 Posters ——(线段树+扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...

  4. HDU 5091---Beam Cannon(线段树+扫描线)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5091 Problem Description Recently, the γ galaxies bro ...

  5. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. 【42.49%】【hdu 1542】Atlantis(线段树扫描线简析)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...

  7. HDU 1828 Picture(线段树扫描线求周长)

    Picture Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  9. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...

随机推荐

  1. [POJ1143]Number Game

    [POJ1143]Number Game 试题描述 Christine and Matt are playing an exciting game they just invented: the Nu ...

  2. uva 10515 规律打表

    Problem G Power et al. Input: Standard Input Output: Standard Output Finding the exponent of any num ...

  3. 标准C程序设计七---25

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  4. WAMP本地环境升级php版本--第二次尝试

    wamp 环境下 php5.6.25 升级php7.1.17 实践 本文参考:https://www.cnblogs.com/hubaohua1588/p/6884146.html来进行操作. 1.从 ...

  5. substring用法

    字符串截取,substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串. substring(int beginIndex, int endIndex) 返回一 ...

  6. mac 获得进程信息的方法

    NSProcessInfo可以获得当前进程的信息.获得所有活动进程信息可以尝试使用下面的方法. 进程的信息可以通过ps命令得到也可以通过sysctl方法得到. 但是我总是不能获取进程的流量信息,关于这 ...

  7. Docker如何部署Python项目

    Docker 部署Python项目 作者:白宁超 2019年5月24日09:09:00 导读: 软件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正确,软件才能运行.如 ...

  8. delphi学习路线

     酷派(53376063) 11:04:19 1.语法基础PASCAL精要 先看个1-3遍,这是基础中的基础,只要没弄清楚看10遍都不多,当然最好结合着代码实例去看.(以后遇到哪儿不熟练继续反复看)2 ...

  9. java判断字符串中是否含有汉字

    原文:http://www.open-open.com/code/view/1426332240717 判断字符串中是否含有汉字: String str = "test中文汉字"; ...

  10. docker 配置 direct-lvm

    当前需要设置的宿主机是环境是搭建在vbox虚拟机上的centos7系统.测试环境中出现过一次意外情况,当时为了测试docker日志文件限制,运行了一个docker容器,但是后面忘记停止了,几天后发现了 ...