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个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
随机推荐
- 洛谷P3348 [ZJOI2016]大森林(LCT,虚点,树上差分)
洛谷题目传送门 思路分析 最简单粗暴的想法,肯定是大力LCT,每个树都来一遍link之类的操作啦(T飞就不说了) 考虑如何优化算法.如果没有1操作,肯定每个树都长一样.有了1操作,就来仔细分析一下对不 ...
- bzoj1008/luogu3197 越狱 (快速幂)
算$m^n-m*(m-1)^{n-1}$,就是总的减去不越狱的,不越狱就每次都选一个和上一个不一样的
- 解题:国家集训队 Crash 的文明世界
题面 这种套着高次幂的统计问题一般都要用到第二类斯特林数和自然数幂的关系:$a^k=\sum\limits_{i=0}^{k}S_k^iC_a^i*i!$ 那么对于每个点$x$有: $ans_x=\s ...
- 【UVA534】Frogger 最小瓶颈路
题目大意:给定一张 N 个点的完全图,求 1,2 号节点之间的一条最小瓶颈路. 题解:可知,最小瓶颈路一定存在于最小生成树(最小瓶颈树)中.因此,直接跑克鲁斯卡尔算法,当 1,2 号节点在同一个联通块 ...
- Django中简单添加HTML、css、js等文件(非正规添加,适合小白)
Django中简单添加HTML.css.js等文件 首先申明下自己的环境, python版本3.65(亲测3.7版本有毒,没解决掉!) Django版本1.11.15(版本比较成熟,也可以用最新的版本 ...
- 各种蕴含算法思想的DP - 3
内容中包含 base64string 图片造成字符过多,拒绝显示
- git 创建空提交
git commit --allow-empty -m "Empty Commit to setup deployments"
- Golang的交互模式进阶-读取用户的输入
Golang的交互模式进阶-读取用户的输入 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 读写数据除了 fmt 和 os 包,我们还需要用到 bufio 包来处理缓冲的输入和输出. ...
- C语言复习---零散补充
一:double和float使用scanf获取数据 printf输出float和double都可以用%f,double还可以用%lf. 2 scanf输入float用%f,double输入用%lf,不 ...
- [转载]CSS Tools: Reset CSS
http://meyerweb.com/eric/tools/css/reset/ The goal of a reset stylesheet is to reduce browser incons ...