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 4946 [Noi2017]蔬菜
Description http://www.lydsy.com/JudgeOnline/upload/Noi2017D2.pdf Solution 网上大部分都是并查集写法,但是有大神写了非并查集写 ...
- 洛谷 P1412 经营与开发 解题报告
P1412 经营与开发 题目描述 \(4X\)概念体系,是指在\(PC\)战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以"\(EX\)"为开头的英语单词. \(eXplo ...
- 解题:CTSC 2008 祭祀
题面 洛谷要求输出方案,懒得写了,但是还是放一下链接看看吧 (虽然现在二分图已经过气了=.=) 要求最长反链,最长反链=最小链覆盖,先Floyd传递闭包之后链覆盖就变成了边覆盖,然后最小边覆盖=总点数 ...
- Codeforces 468C/469E 易错点
#include <stdio.h> #include <stdlib.h> typedef long long ll; int main() { ll x=1e17; ll ...
- 在windows上部署使用Redis出现问题的解决方法
下载Redis 在Redis的官网下载页上有各种各样的版本,我这次是在windows上部署的,要去GitHub上下载.目前的是2.8.12版的,直接解压,在\bin\release 目录下有个压缩包, ...
- win7下PLSQL Developer提示“ORA-12154: TNS:无法解析指定的连接标识符”
解决方法:卸载掉重新安装,注意安装的目录的文件夹不要有特殊的符号,例如64位系统的的安装目录会到Program Files (x86),这时候就会出现"ORA-12154: TNS:无法解析 ...
- Dubbo学习笔记10:Dubbo服务消费方启动流程源码分析
同理我们看下服务消费端启动流程时序图: 在<Dubbo整体架构分析>一文中,我们提到服务消费方需要使用ReferenceConfig API来消费服务,具体是调用代码(1)get()方法来 ...
- [整理]zepto的初次使用
http://www.css88.com/doc/zeptojs_api/ http://chaoskeh.com/blog/some-experience-of-using-zepto.html
- Codeforces 238 div2 B. Domino Effect
题目链接:http://codeforces.com/contest/405/problem/B 解题报告:一排n个的多米诺骨牌,规定,若从一边推的话多米诺骨牌会一直倒,但是如果从两个方向同时往中间推 ...
- 记关于webpack4下css提取打包去重复的那些事
注意使用vue-cli3(webpack4),默认小于30k不会抽取为公共文件,包括css和js,已测试 经过2天的填坑,现在终于有点成果 环境webpack4.6 + html-webpack-pl ...