Codeforces Round #254 (Div. 1) 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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #254 (Div. 1) C. DZY Loves Colors 分块
C. DZY Loves Colors 题目连接: http://codeforces.com/contest/444/problem/C Description DZY loves colors, ...
- Codeforces Round #254 (Div. 1) A. DZY Loves Physics 智力题
A. DZY Loves Physics 题目连接: http://codeforces.com/contest/444/problem/A Description DZY loves Physics ...
- Codeforces Round #254 (Div. 2) A. DZY Loves Chessboard —— dfs
题目链接: http://codeforces.com/problemset/problem/445/A 题解: 这道题是在现场赛的最后一分钟通过的,相当惊险,而且做的过程也很曲折. 先是用递推,结果 ...
- 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 ...
- 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内 ...
- [题解]Codeforces Round #254 (Div. 2) B - DZY Loves Chemistry
链接:http://codeforces.com/contest/445/problem/B 描述:n种药品,m个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
随机推荐
- 【刷题】BZOJ 4000 [TJOI2015]棋盘
Description Input 输入数据的第一行为两个整数N,M表示棋盘大小.第二行为两个整数P,K, 表示攻击范围模板的大小,以及棋子在模板中的位置.接下来三行, 每行P个数,表示攻击范围的模版 ...
- GO内存管理
TMalloc模型 http://www.360doc.com/content/16/0811/09/14513665_582407916.shtml http://blog.csdn.net/cho ...
- SQL Server 一些查询技巧
--1.[行列转换] --列转行 USE tempdb GO IF (OBJECT_ID('DEPT') IS NOT NULL) DROP TABLE DEPT CREATE TABLE DEPT( ...
- 「Vue」JS方法学习
1.构造函数 大写开头的,能被NEW一个新实例,实例即执行回调函数 异步返回数据.then指定回调函数的时候,成功的回调函数必须传,失败的回调可以不传 var fs = require('fs') f ...
- Scala进阶之路-为什么要学习Scala以及开发环境搭建
Scala进阶之路-为什么要学习Scala以及开发环境搭建 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 最近人工智能和大数据那是相当的火呀,人工智能带动了Python的流行,区块 ...
- Object和泛型
这俩东东呢实现的效果是一样的,但是它们俩的效率不一样,原理也不一样.直接说结论吧,泛型比Object好用,效率高.因为Object有一个装箱和拆箱的过程,倘若是很多的数据那效率就低到不行了,而泛型没有 ...
- .net 未被引用的错误
开发的时候遇到了一个错误,如下: 错误 1 类型“System.ServiceModel.ClientBase`1<T0>”在未被引用的程序集中定义. 我原本以为是版本号的问题,添加了引用 ...
- log4j2打印jdbcTemplate的sql以及参数
log4j2打印jdbcTemplate的sql以及参数 ——IT唐伯虎 摘要: log4j2打印jdbcTemplate的sql以及参数. 在log4j2.xml加上这两个logger即可: < ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
- javascript数组赋值操作
最近在司徒正美的<javascript框架设计>,在里面发现了一个段代码 ...... var _len = arr1.length; while (_len) { arr2[--_len ...