D - DZY Loves Strings

思路:感觉这种把询问按大小分成两类解决的问题都很不好想。。

https://codeforces.com/blog/entry/12959

题解说得很清楚啦。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e5 + ;
const int M = 1e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, B;
string s, str;
string p[N], q[N];
int hsp[N], hsq[N], mn[M], len[M];
int gg[N][];
vector<int> vc[M]; bool vis[M];
int ans[N]; inline int get_hash(string& a) {
int ans = ;
for(int i = ; i < a.size(); i++)
ans *= , ans += a[i]-'a'+;
return ans;
} int Merge(vector<int>& a, vector<int>& b, int lena, int lenb) {
int ans = inf;
if(!a.size() || !b.size()) return ans;
for(int i = , j = ; i < a.size(); i++) {
while(j+ < b.size() && a[i] >= b[j]) j++;
if(j- >= ) ans = min(ans, max(a[i]+lena, b[j-]+lenb) - min(b[j-], a[i]));
ans = min(ans, max(a[i]+lena, b[j]+lenb) - min(b[j], a[i]));
}
return ans;
} int main() {
memset(gg, -, sizeof(gg));
memset(ans, inf, sizeof(ans));
cin >> s;
scanf("%d", &n); B = ceil(*s.size() / sqrt(n));
for(int i = ; i <= n; i++) {
cin >> p[i] >> q[i];
hsp[i] = get_hash(p[i]);
hsq[i] = get_hash(q[i]);
}
for(int i = ; i < s.size(); i++) {
str = s.substr(i, );
while(str.size()) {
int hs = get_hash(str);
vc[hs].push_back(i);
len[hs] = str.size();
gg[i][str.size()] = hs;
str.pop_back();
}
}
for(int i = ; i <= ; i++) {
if(vc[i].size() >= B) {
for(int j = ; j <= n; j++) mn[hsp[j]] = mn[hsq[j]] = inf;
for(int j = , k = ; j < s.size(); j++) {
while(k+ < vc[i].size() && j >= vc[i][k]) k++;
for(int z = ; z <= ; z++) {
int hs = gg[j][z];
if(hs == -) continue;
if(k- >= ) mn[hs] = min(mn[hs], max(vc[i][k-]+len[i], j+len[hs]) - min(j, vc[i][k-]));
mn[hs] = min(mn[hs], max(vc[i][k]+len[i], j+len[hs]) - min(j, vc[i][k]));
}
}
for(int j = ; j <= n; j++) {
if(hsp[j] == i) ans[j] = min(ans[j], mn[hsq[j]]), vis[j] = true;
else if(hsq[j] == i) ans[j] = min(ans[j], mn[hsp[j]]), vis[j] = true;
}
}
}
int cnt = , mx = ;
for(int i = ; i <= n; i++) {
if(!vis[i])
ans[i] = Merge(vc[hsp[i]], vc[hsq[i]], len[hsp[i]], len[hsq[i]]);
}
for(int i = ; i <= n; i++) printf("%d\n", ans[i] == inf ? - : ans[i]);
return ;
} /*
*/

Codeforces Round #254 (Div. 1) D - DZY Loves Strings的更多相关文章

  1. Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力

    D. DZY Loves Strings 题目连接: http://codeforces.com/contest/444/problem/D Description DZY loves strings ...

  2. Codeforces Round #FF (Div. 2):B. DZY Loves Strings

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 线段树

    题目链接: http://codeforces.com/problemset/problem/444/C J. DZY Loves Colors time limit per test:2 secon ...

  4. Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块

    C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...

  5. Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题

    A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...

  6. Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs

    题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...

  7. Codeforces Round #254 (Div. 2)B. DZY Loves Chemistry

    B. DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #254 (Div. 1) C DZY Loves Colors

    http://codeforces.com/contest/444/problem/C 题意:给出一个数组,初始时每个值从1--n分别是1--n.  然后两种操作. 1:操作 a.b内的数字是a,b内 ...

  9. [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry

    链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...

随机推荐

  1. MT【172】内外圆

    $P,Q$是两个定点,M为平面内一个动点,且$\dfrac{|MP|}{|MQ|}=\lambda(\lambda>0,\lambda\ne1)$, 点M的轨迹围成的区域面积为S , 设$S=f ...

  2. SpringBoot中的定时任务与Quartz的整合

    SpringBoot集成Quartz 定时任务Quartz : 就是在指定的时间执行一次或者循环执行,在项目的开发中有时候会需要的, 还是很有用的. SpringBoot内置的定时 添加依赖 < ...

  3. 使用highlight.js高亮静态页面的语言代码

    显示静态的代码其实html的pre标签基本可以满足需求了,至少不会将换行的文本显示成一堆字符串. 不过能使静态的文本能高亮显示,倒更炫酷一点.其实很简单的,引入highlight.js包,可以使用cd ...

  4. Spring MVC 向页面传值-Map、Model和ModelMap

    原文链接:https://www.cnblogs.com/caoyc/p/5635878.html Spring MVC 向页面传值-Map.Model和ModelMap 除了使用ModelAndVi ...

  5. jsoncpp的安装与使用示例

    安装: 生成静态库 生成静态库: 第一步:生成目标文件: g++ -g -Wall -c json_reader.cpp json_value.cpp json_writer.cpp -I. -I.. ...

  6. python实现windows Service服务程序

    python实现windows Service服务程序 win32serviceutil.ServiceFramework是封装得很好的Windows服务框架,本文通过继承它来实现. 通过SvcDoR ...

  7. OpenStack 网络服务 Neutron 私有网络构建(十九)

    本章内容基于之前提供者网络构建的基础上进行改动,之前文章参考如下: Openstack 网络服务 Neutron介绍和控制节点部署 (九) Openstack 网络服务 Neutron计算节点部署(十 ...

  8. Python正则表达式(regular expression)简介-re模块

    Python正则表达式(regular expression)简介-re模块 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 就其本质而言,正则表达式(或RE模块)是一种小型的,高度 ...

  9. Splay模板讲解及一些题目

    普通平衡树模板以及文艺平衡树模板链接. 简介 平衡二叉树(Balanced Binary Tree)具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二 ...

  10. Spring Mvc + Maven + BlazeDS 与 Flex 通讯 (七)

    BlazeDS 说明 BlazeDS是由Adobe开源的基于amf协议的,用于解决flex与java通讯的组件; 基于传统的文本协议的XML传输方式,在抽象层方面会有很大的压力,特别在需要序列化与反序 ...