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, and he enjoys collecting them.
In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.
Once DZY found a lucky string s. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the i-th pair has name ai, the second one has name bi. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever.
Please help DZY for each pair find the minimum length of the substring of s that contains both ai and bi, or point out that such substring doesn't exist.
A substring of s is a string slsl + 1... sr for some integers l, r (1 ≤ l ≤ r ≤ |s|). The length of such the substring is (r - l + 1).
A string p contains some another string q if there is a substring of p equal to q.
Input
The first line contains a string s (1 ≤ |s| ≤ 50000).
The second line contains a non-negative integer q (0 ≤ q ≤ 100000) — the number of pairs. Each of the next q lines describes a pair, the line contains two space-separated strings ai and bi (1 ≤ |ai|, |bi| ≤ 4).
It is guaranteed that all the strings only consist of lowercase English letters.
Output
For each pair, print a line containing a single integer — the minimum length of the required substring. If there is no such substring, output -1.
Sample Input
xudyhduxyz
3
xyz xyz
dyh xyz
dzy xyz
Sample Output
3
8
-1
题意
给你一个串,然后有q次询问,每次询问给你两个串s1,s2
你需要找到一个最短的串s,使得这个串包含这两个子串
题解:
因为询问的那个串的长度才4嘛
我就预处理所有串的出现的位置,然后每次O(n+m)去找最小的长度就好了
再加一个人尽皆知的剪枝:如果这个询问问过了,那就直接输出答案
然后就莽过去了……
代码
#include <bits/stdc++.h>
using namespace std;
const int N=5e4+10;
const int NN=6e5+10;
char s[N];
vector<int> v[NN];
map<pair<int,int>,int> mp;
int main()
{
scanf("%s",s);
int n=strlen(s);
for(int l=1;l<=4;l++)
{
for(int i=0;i+l-1<n;i++)
{
int tmp=0;
for(int j=l-1;j>=0;j--)
tmp=(tmp*27)+s[i+j]-'a'+1;
v[tmp].push_back(i);
}
}
int q;
scanf("%d",&q);
while(q--)
{
char s1[6],s2[6],ss[2];
scanf("%s%s",s1,s2);
int t1=0,t2=0,l1=strlen(s1),l2=strlen(s2);
for(int j=l1-1;j>=0;j--)
t1=(t1*27)+s1[j]-'a'+1;
for(int j=l2-1;j>=0;j--)
t2=(t2*27)+s2[j]-'a'+1;
if(mp[make_pair(t1,t2)]!=0)
{
printf("%d\n",mp[make_pair(t1,t2)]);
continue;
}
int ans=n+2;
for(int j=0,i=0;i<v[t1].size();i++)
{
for(;j<v[t2].size()&&v[t2][j]<v[t1][i];j++);
if(j>=v[t2].size()) break;
ans=min(ans,max(v[t2][j]+l2,v[t1][i]+l1)-min(v[t1][i],v[t2][j]));
}
for(int j=v[t2].size()-1,i=v[t1].size()-1;i>=0;i--)
{
for(;j>=0&&v[t2][j]>v[t1][i];j--);
if(j<0) break;
ans=min(ans,max(v[t2][j]+l2,v[t1][i]+l1)-min(v[t1][i],v[t2][j]));
}
if(ans==n+2) ans=-1;
mp[make_pair(t1,t2)]=ans;
printf("%d\n",ans);
}
return 0;
}
Codeforces Round #254 (Div. 1) D. DZY Loves Strings hash 暴力的更多相关文章
- Codeforces Round #254 (Div. 1) D - DZY Loves Strings
D - DZY Loves Strings 思路:感觉这种把询问按大小分成两类解决的问题都很不好想.. https://codeforces.com/blog/entry/12959 题解说得很清楚啦 ...
- 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个反应关系,按照一定顺序放进试管中.如果当前放入的药品与试管中的药品要反应,危险系数变为 ...
随机推荐
- 十七、springboot配置FastJson为Spring Boot默认JSON解析框架
前提 springboot默认自带json解析框架,默认使用jackson,如果使用fastjson,可以按照下列方式配置使用 1.引入fastjson依赖库: maven: <dependen ...
- 强大的vi的几个功能
1 拷贝第十行到第十三行到文件a中,不用!亦可 : 比如你要拷贝从第10行到第109行到文件123.txt中,可以用以下的命令:10,109w!123.txt
- 如何阻止点击scrollviewer里面的单位内容时,自动滚动
<Style TargetType="{x:Type ListBoxItem}"> <Setter Property="FocusVisualStyle ...
- MySQL 5.1完全卸载
第一步:控制面板里的增加删除程序内进行删除 第二步:删除MySQL文件夹下的my.ini文件,如果备份好,可以直接将文件夹全部删除 第三步:regedit进入注册表 HKEY_LOCAL_MACHIN ...
- 在jenkins里使用SCM管理jenkinsfile
注意,这样作的前提是,插件里一定要安装pipeline和git. 设置就比较简单了. 在gitlat里生成一个演示的jenkinsfile pipeline { agent { node { labe ...
- centos6编译安装zabbix3.0和中文支持整理文档
编者按: 最近公司部分业务迁移机房,为了更方便的监控管理主机资源,决定上线zabbix监控平台.运维人员使用2.4版本的进行部署,个人在业余时间尝鲜,使用zabbix3.0进行部署,整理文档如下,仅供 ...
- 易普优APS签约本田汽车零部件八千代工业集团!
2018年7月,易普优APS与八千代工业株式会社汽车零部件供应商正式签约,易普优APS在汽车零部件与整车行业的针对性解决方案的又一次得到客户高度认可与青睐! 日本八千代工业株式会社成立于1953年,总 ...
- Python全栈开发之21、django
http://www.cnblogs.com/wupeiqi/articles/5237704.html http://www.cnblogs.com/wupeiqi/articles/5246483 ...
- JAVA 并发编程-多个线程之间共享数据
原文地址:http://blog.csdn.net/hejingyuan6/article/details/47053409# 多线程共享数据的方式: 1,如果每个线程执行的代码相同,可以使用同一个R ...
- hdoj1050 Moving Tables(贪心)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1050 题意 有一条走廊,走廊两边各有200个房间,一边的房间编号是奇数,另一边是偶数.现在有n个箱子需 ...