Codeforces Round #254 (Div. 1) D. DZY Loves Strings

D. DZY Loves Strings
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
xudyhduxyz
3
xyz xyz
dyh xyz
dzy xyz
output
3
8
-1
input
abcabd
3
a c
ab abc
ab d
output
2
3
3
input
baabcabaaa
2
abca baa
aa aba
output
6
4
Note

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.

题意:给你一个长的字符串,有Q次询问,每次询问有两个短的字符串
叫你在长的字符串中找一个子串,这个字串包含这两个短的字符串,而且长度最小
解法:长度为1-4的所有子串进行哈希,记录其出现的开始位置,
询问的是 a ,b 两个字符串的话,可以很据哈希找出 a 出现的所有位置 ,在对 a 的位置 s 进行算时 ;
可以二分b的位置,找到第一个比 s 大的位置在哈希数组下标 p ,那么和 s匹配的也就只能是 p ,或者 p-1
这样不加猥琐的优化是会T的,因为a的位置可以有n个,
这里加两个优化,
第一,a 的位置如果比 b 的多,就交换 a,b;
第二,用 map记录计算过的所有 a,b.以后有相同的a,b就直接输出(感觉很猥琐==)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<ctime>
#include<map>
#include<set>
#include<stack>
#include<list>
#define maxn 100010
#define INF 78974453
#define MAX 710000
#define LL long long
using namespace std ;

typedef pair<int, int> pii;
char a[maxn] ;
vector<int>qe[MAX] ;
map<pii,int>dp ;
LL hehe[20] ;

int get( char a[] ,int n  )
{
    int ans = 0 ;
    for( int i = 0 ; i < n;i++){
        ans = ans*27+a[i]-'a'+1 ;
    }
    return ans ;
}
int getans( int x, int y ,int n1,int n2)
{
    int ans = INF;
    int ll,rr,l,r ;
    for( int i = 0 ; i < qe[x].size() ;i++)
    {
        l = qe[x][i] ;
        r = l+n1 ;
        int p = int(lower_bound(qe[y].begin(), qe[y].end(), l) - qe[y].begin()); // b[p] >= l
        if( p < qe[y].size())
        {
            rr = qe[y][p]+n2 ;
            if(r > rr) rr = r ;
            ans = min(ans,rr-l) ;
        }
        p-- ;
        if( p >= 0 )
        {
            rr = qe[y][p]+n2 ;
            if(r > rr) rr = r ;
            ans = min(ans,rr-qe[y][p]) ;
        }
    }
    return ans ;
}
int main()
{
        int n ,i , j ,k , m ;
        int x ,y ;
        char s1[5],s2[5] ;
        hehe[1] = 1 ;
        for( i = 2; i <= 15 ;i++)
            hehe[i] = hehe[i-1]*10 ;
        scanf("%s",a);
        n = strlen(a) ;
        for( i = 0 ; i < n ;i++ )
        {
            for( j = 1 ; j < 5 ;j++ )
            {
                if(j+i > n) break ;
                k = get(a+i,j) ;
                qe[k].push_back(i) ;
            }
        }
        scanf("%d",&m) ;
        while(m--)
        {
            scanf("%s%s",s1,s2) ;
            int xx = strlen(s1) ;
            int yy = strlen(s2) ;
            x = get(s1,xx) ;
            y = get(s2,yy) ;
            if(qe[x].size() > qe[x].size() || (qe[x].size() == qe[y].size() && x > y))
            {
                swap(x,y) ;
                swap(xx,yy) ;
            }
            int &ans = dp[pii(x,y)] ;
            if(!ans) ans = getans(x,y,xx,yy) ;
            printf("%d\n",ans==INF?-1:ans) ;
        }
        return 0 ;
}

  

posted @ 2014-07-26 10:45  _log__  阅读(243)  评论(0编辑  收藏  举报