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;
}

最新文章

  1. 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写
  2. linux的bash 终端操作快捷键
  3. ArcGIS Engine开发之旅01---产品组成、逻辑体系结构
  4. 2014第五届蓝桥杯试题C/C++程序设计B组——切面条
  5. Windows下Android模拟环境的搭建
  6. linux之SQL语句简明教程---LIKE
  7. 我的Python成长之路---第四天---Python基础(16)---2016年1月23日(寒风刺骨)
  8. .Net Core微服务系列--开篇
  9. Android 音视频开发(四):使用 Camera API 采集视频数据
  10. Delphi中的消息 (转载)
  11. java中自定义注释@interface的用法
  12. opencv学习之路(1)、示例程序
  13. tornado输入-get_query_argument()等 笔记
  14. selenium+python自动化----xlrd,xlswriter
  15. OpenCV/OpenCL/OpenGL区别
  16. github air项目中遇到的几个问题及解决(nodejs居多)
  17. Seaborn绘图
  18. dubbo 面试题
  19. 在Android中使用FlatBuffers(中篇)
  20. Mybatis学习--XML映射配置文件

热门文章

  1. Intent 对象在 Android 开发中的应用
  2. sleep命令
  3. html5学习之canvas
  4. linux常用命令总结-&gt;1
  5. idea心得
  6. 使用MongoDB命令工具导出、导入数据
  7. Hadoop(二):MapReduce程序(Java)
  8. python基础学习之路No.4 数据转换以及操作
  9. 洛谷P1286 两数之和
  10. 漂亮的SVG时钟