CF444D.DZY Loves Strings

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 ss . A lot of pairs of good friends came to DZY when they heard about the news. The first member of the ii -th pair has name aia_{i} , the second one has name bib_{i} . 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 ss that contains both aia_{i} and bib_{i} , or point out that such substring doesn't exist.

A substring of ss is a string slsl+1... srs_{l}s_{l+1}...\ s_{r} for some integers l,rl,r (1<=l<=r<=s)(1<=l<=r<=|s|) . The length of such the substring is (rl+1)(r-l+1) .

A string pp contains some another string qq if there is a substring of pp equal to qq .

输入格式

The first line contains a string ss (1<=s<=50000)(1<=|s|<=50000) .

The second line contains a non-negative integer qq (0<=q<=100000)(0<=q<=100000) — the number of pairs. Each of the next qq lines describes a pair, the line contains two space-separated strings aia_{i} and bib_{i} (1<=ai,bi<=4)(1<=|a_{i}|,|b_{i}|<=4) .

It is guaranteed that all the strings only consist of lowercase English letters.

输出格式

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.

输入输出样例

  • 输入#1

    xudyhduxyz
    3
    xyz xyz
    dyh xyz
    dzy xyz
    

    输出#1

    3
    8
    -1
    
  • 输入#2

    abcabd
    3
    a c
    ab abc
    ab d
    

    输出#2

    2
    3
    3
    
  • 输入#3

    baabcabaaa
    2
    abca baa
    aa aba
    

    输出#3

    6
    4
    

说明/提示

The shortest substrings in the first sample are: xyz, dyhduxyz.

The shortest substrings in the second sample are: ca, abc and abd.

The shortest substrings in the third sample are: baabca and abaa.

首页