LuoGuP3667
LuoGuP3667
这题对我来说难的一批(题意理解错三遍,垃圾翻译,还是英文原题面好)
就是给你\(2n\)个串,要你找一个区间,使得前\(n\)个串的这个区间不能和后\(n\)个串中的区间有任何一个相同,求一个最短长度.
这显然可以二分,不过听取了\(dalao\)的建议,我选择了枚举左端点,二分右端点的方式.
怎么\(check\)呢?你把所有串\(hash\)一下,然后你显然可以任意查询某一个区间的\(hash\)值.
把后\(n\)个串的对应区间的\(hash\)值插入一个\(set\),直接查询就行了.
\(Code:\)
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#define MEM(x,y) memset ( x , y , sizeof ( x ) )
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define per(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define pii pair < int , int >
#define X first
#define Y second
#define rint read<int>
#define int long long
#define pb push_back
#define mid ( ( l + r ) >> 1 )
using std::set ;
using std::pair ;
using std::max ;
using std::min ;
using std::priority_queue ;
using std::vector ;
using std::swap ;
using std::sort ;
using std::unique ;
using std::greater ;
template < class T >
inline T read () {
T x = 0 , f = 1 ; char ch = getchar () ;
while ( ch < '0' || ch > '9' ) {
if ( ch == '-' ) f = - 1 ;
ch = getchar () ;
}
while ( ch >= '0' && ch <= '9' ) {
x = ( x << 3 ) + ( x << 1 ) + ( ch - 48 ) ;
ch = getchar () ;
}
return f * x ;
}
const int inf = 1e15 ;
const int N = 1e3 + 100 ;
const int base = 233 ;
const int mod = 1e9 + 7 ;
int n , m , g[N] , ans ; char s[N] ;
unsigned int hash[N][N] ;
std::set < int > st ;
inline int query (int x , int l , int r)
{ return ( hash[x][r] - hash[x][l-1] * g[r-l+1] % mod + mod ) % mod ; }
inline bool check (int l , int r) {
st.clear () ;
rep ( i , n + 1 , ( n << 1 ) ) st.insert ( query ( i , l , r ) ) ;
rep ( i , 1 , n )
if ( st.find ( query ( i , l , r ) ) != st.end () ) return false ;
return true ;
}
signed main (int argc , char * argv[]) {
n = rint () ; m = rint () ;
rep ( i , 1 , ( n << 1 ) ) {
scanf ("%s" , s + 1 ) ;
rep ( j , 1 , m ) hash[i][j] = ( hash[i][j-1] * base % mod + s[j] ) % mod ;
}
g[0] = 1 ; rep ( i , 1 , m ) g[i] = g[i-1] * base % mod ; ans = inf ;
rep ( i , 1 , m ) {
int l = i + 1 , r = m , res = ( m << 1 ) ;
while ( l <= r ) {
if ( check ( i , mid ) ) res = mid , r = mid - 1 ;
else l = mid + 1 ;
}
ans = min ( ans , res - i + 1 ) ;
}
printf ("%lld\n" , ans ) ;
return 0 ;
}
May you return with a young heart after years of fighting.