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. php 投票

    1.投票主界面(问题界面) <?php$db = new Mysqli("localhost","root","root"," ...

  2. 修改系统dpi

    系统dpi设置不合理,会导致屏幕显示效果差.配置系统dpi设置ro.sf.lcd_density的值即可,不同项目设置位置不同,以高通为例:需要修改 项目/device/qcom/common/roo ...

  3. SPOJ 4060 A game with probability

    博弈论+dp+概率 提交链接- 题意不是很好懂 Ai 表示剩 i 个石头. A 先手的获胜概率. Bi 表示剩 i 个石头. B先手的获胜概率. 如果想选,对于 Ai: 有 p 的概率进入 Bi−1 ...

  4. SGU 106 在区间范围内的线性方程解个数

    题意:求解方程ax+by+c=0,在区间x1->x2和y1->y2的解的个数. 看似简单,真心a的不容易啊! 开始跪于第8组数据,原因是没用long long !后来改了,跪于12组,超时 ...

  5. luogu P1476 休息中的小呆

    题目描述 当大家在考场中接受考验(折磨?)的时候,小呆正在悠闲(欠扁)地玩一个叫“最初梦想”的游戏.游戏描述的是一个叫pass的有志少年在不同的时空穿越对抗传说中的大魔王chinesesonic的故事 ...

  6. CodeChef - METEORAK Meteor

    Read problems statements in Mandarin Chineseand Russian. A meteor fell on Andrew's house. That's why ...

  7. 洛谷 P1710 地铁涨价

    题目背景 本题开O2优化,请注意常数 题目描述 博艾市除了有海底高铁连接中国大陆.台湾与日本,市区里也有很成熟的轨道交通系统.我们可以认为博艾地铁系统是一个无向连通图.博艾有N个地铁站,同时有M小段地 ...

  8. maven删除不必要的依赖;优化pom依赖研究

    mvn dependency:copy-dependencies -DoutputDirectory=/home/admin/git/oceanus/test 会把所有依赖的插件版本都拷贝进去,而不是 ...

  9. Effective C++ Item 47 请使用 traits classes 表现类型信息

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 经验:Traits classes 使得"类型相关信息"在编译期可用.它 ...

  10. 转:linux下ip修改与域名解析查看等

    转自: http://www.justwinit.cn/post/7038/ IP:     ifconfiggateway:172.16.0.254 [root@localhost ~]# nets ...