1. 程式人生 > >字串hash + 二分答案

字串hash + 二分答案

// Memory   Time
// 1347k     0MS
// by : Snarl_jsb
// 2014-10-04-21.16
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<climits>
#include<cmath>
#define ULL unsigned long long
using namespace std;

string s1,s2;
int l1,l2,seed=131;
vector<ULL> hash;
bool judge(int x)
{
    hash.clear();//多組資料時不要忘了清空全域性陣列
    //構造s1串的hash表
    ULL tmp=0;
    for (int i = 0; i < x; i++)
    {
        tmp=tmp* seed + s1[i];
    }
    hash.push_back(tmp);
    ULL base =1;
    for (int i = 1; i < x; i++)//求出到達x的base值
    {
        base *= seed;
    }
    for (int i = x; i < l1; i++)
    {
        tmp=(tmp*seed+s1[i])-base*s1[i-x]*seed;
        hash.push_back(tmp);
    }
    //構造完畢
    sort(hash.begin(),hash.end()); //二分查詢加速,必需先排序
    ULL hashval = 0;
    for (int i = 0; i < x; i++)//求出s2串0到x的hash值
    {
        hashval = hashval * seed + s2[i];
    }
    if (binary_search(hash.begin(),hash.end(),hashval))//查詢s2串0到x的hash值是否在s1串的hash表中
        return 1;
    for (int i = x; i < l2; i++)//如果上面的s2串0到x的hash值未匹配成功,這兒接著匹配s2串長度為x的hash值是否出現在s1串的hash表中
    {
        hashval = hashval*seed+s2[i]-s2[i-x]*base*seed;
        if (binary_search(hash.begin(),hash.end(),hashval))
            return 1;
    }
    return 0;
}
int main()
{
    while (cin>>s1>>s2)
    {
        l1=s1.size();
        l2=s2.size();
        int ans = 0;
        int low=0,high = min(l1,l2);
        while (low <= high)//二分答案
        {
            int mid = (low+high)>>1;
            if (judge(mid))//判斷答案是否可行
            {
                ans = mid;
                low = mid+1;
            }
            else
                high = mid-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}