Long Long Message poj 2774 哈希+二分

The little cat is majoring in physics in the capital of Byterland. A piece of sad news comes to him these days: his mother is getting ill. Being worried about spending so much on railway tickets (Byterland is such a big country, and he has to spend 16 shours on train to his hometown), he decided only to send SMS with his mother. 

The little cat lives in an unrich family, so he frequently comes to the mobile service center, to check how much money he has spent on SMS. Yesterday, the computer of service center was broken, and printed two very long messages. The brilliant little cat soon found out: 

1. All characters in messages are lowercase Latin letters, without punctuations and spaces. 
2. All SMS has been appended to each other – (i+1)-th SMS comes directly after the i-th one – that is why those two messages are quite long. 
3. His own SMS has been appended together, but possibly a great many redundancy characters appear leftwards and rightwards due to the broken computer. 
E.g: if his SMS is “motheriloveyou”, either long message printed by that machine, would possibly be one of “hahamotheriloveyou”, “motheriloveyoureally”, “motheriloveyouornot”, “bbbmotheriloveyouaaa”, etc. 
4. For these broken issues, the little cat has printed his original text twice (so there appears two very long messages). Even though the original text remains the same in two printed messages, the redundancy characters on both sides would be possibly different. 

You are given those two very long messages, and you have to output the length of the longest possible original text written by the little cat. 

Background: 
The SMS in Byterland mobile service are charging in dollars-per-byte. That is why the little cat is worrying about how long could the longest original text be. 

Why ask you to write a program? There are four resions: 
1. The little cat is so busy these days with physics lessons; 
2. The little cat wants to keep what he said to his mother seceret; 
3. POJ is such a great Online Judge; 
4. The little cat wants to earn some money from POJ, and try to persuade his mother to see the doctor :( 

Input

Two strings with lowercase letters on two of the input lines individually. Number of characters in each one will never exceed 100000.

Output

A single line with a single integer number – what is the maximum length of the original text written by the little cat.

Sample Input

yeshowmuchiloveyoumydearmotherreallyicannotbelieveit
yeaphowmuchiloveyoumydearmother

Sample Output

27

题目大意:求两个字符串最长的公共子串长度是多少。

这个题可以用dp,后缀数组,后缀自动机或者哈希可以解决这类的问题,但是时间和空间的限制,只能用后缀自动机和哈希来写吧,但是菜鸟的我还不会后缀自动机,而且正在学习哈希,所以用哈希加二分的方法解决了。

题目链接:http://poj.org/problem?id=2774

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
const int Maxn = 1e5 +10 ;
const int seed = 131 ;

string s1, s2 ;

bool check (int mid){
    vector < ull > ve ;
    ull p = 1 , _Hash = 0 ;
    for (int i = 0; i < mid; i++){
        p *= seed ;
        _Hash = _Hash * seed + s1[i] ;
    }
    ve.push_back(_Hash) ;
    for (int i = mid; i < s1.size(); i++){
        _Hash = _Hash * seed + s1[i] - p * s1[i - mid] ;
        ve.push_back(_Hash) ;
    }
    sort(ve.begin(), ve.end()) ;
    _Hash = 0 ;
    for (int i = 0; i < mid; i++){
        _Hash = _Hash * seed + s2[i] ;
    }
    if (binary_search(ve.begin(), ve.end(), _Hash)) return true ;
    for (int i = mid; i < s2.size(); i++){
        _Hash = _Hash * seed + s2[i] - p * s2[i - mid] ;
        if (binary_search(ve.begin(), ve.end(), _Hash)) return true ;
    }
    return false ;
}

int main (){
    cin >> s1 >> s2 ;
    int len1 = s1.size() ;
    int len2 = s2.size() ;
    int right = min(len1, len2) ;
    int left = 0 ;
    int ans = 0 ;
    while (left <= right){
        int mid = (left + right) >> 1 ;
        if (check(mid)) left = mid + 1 ,ans = mid ;
        else right = mid - 1 ;
    }
    cout << ans << endl ;
    return 0 ;
}

刚开始对怎么在一个串中求给定长度len的字符串的哈希值很苦恼,在算法竞赛入门经典那本书上有介绍。

对于一个字符串,hash[i] = hash[i + 1]  * seed + s[i],    (seed是自己选择的一个数,我比较喜欢选131或者133这两个数。。我也不知道为什么。。。)    再求一个xp[i] = xp[i - 1] * seed ,  最后想要求的给定长度的子串的哈希值为  _hash[i] = hash[i] - hash[i + len] * xp[len]  。

/*
@Author: Top_Spirit
@Language: C++
*/
//#include <bits/stdc++.h>
//#include <unordered_map>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
const int Maxn = 1e5 +10 ;
const int seed = 131 ;

string s1, s2 ;
ull ha[Maxn], hb[Maxn], xp[Maxn] ;
int len1, len2 ;
ull hasha[Maxn] , hashb[Maxn] ;

bool check (int mid){
    for (int i = 0; i < len1 - mid + 1; i++){
        hasha[i] = ha[i] - ha[i + mid] * xp[mid] ;
    }
    for (int i = 0; i < len2 - mid + 1; i++) {
        hashb[i] = hb[i] - hb[i + mid] * xp[mid] ;
    }
    sort(hasha, hasha + len1 - mid + 1) ;
    for (int i = 0; i < len2 - mid + 1; i++){
        ull tmp = hashb[i] ;
        if (binary_search(hasha, hasha + len1 - mid + 1, tmp)) return true ;
    }
    return false ;
}

int main (){
    cin >> s1 >> s2 ;
    len1 = s1.size() ;
    len2 = s2.size() ;
    int right = min(len1, len2) ;
    for (int i = len1 - 1; i >= 0; i--) ha[i] = ha[i + 1] * seed + s1[i] ;
    for (int i = len2 - 1; i >= 0; i--) hb[i] = hb[i + 1] * seed + s2[i] ;
    xp[0] = 1 ;
    for (int i = 1; i <= right; i++){
        xp[i] = xp[i - 1] * seed ;
    }
    int left = 0 ;
    int ans = 0 ;
    while (left <= right){
        int mid = (left + right) >> 1 ;
        if (check(mid)) left = mid + 1 ,ans = mid ;
        else right = mid - 1 ;
    }
    cout << ans << endl ;
    return 0 ;
}

继续学习,止步不前!

猜你喜欢

转载自blog.csdn.net/weixin_41190227/article/details/86489383