传送门:【POJ】3294 Life Forms
题目分析:我们将所有的串合并成一个串,相邻两串中间用分隔符隔开,注意分隔符一定要用各不相同的且都不在原串中出现的。然后构造后缀数组,这个用倍增算法就好了。然后,我们二分最长串的长度k,然后扫一遍height数组,当>=k时我们统计出现在不同串的个数cnt,当<k时我们就看是否cnt>n/2,是的话返回yes,否则返回no,yes的话就调整下界,否则调整上界。然后输出的话和二分长度时用的函数类似,只不过返回的地方我们换成输出。具体细节留待各位品味。输出‘?’当且仅当长度为0。
代码如下:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; #define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i ) #define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) #define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i ) #define clr( a , x ) memset ( a , x , sizeof a ) #define cpy( a , x ) memcpy ( a , x , sizeof a ) const int MAXN = 110005 ; char buf[1005] ; int s[MAXN] ; int start[105] ; int t1[MAXN] , t2[MAXN] , c[MAXN] , xy[MAXN] ; int sa[MAXN] , rank[MAXN] , height[MAXN] ; int vis[105] , Time ; int n , m ; int cmp ( int *r , int a , int b , int d ) { return r[a] == r[b] && r[a + d] == r[b + d] ; } void getHeight ( int n , int k = 0 ) { For ( i , 0 , n ) rank[sa[i]] = i ; rep ( i , 0 , n ) { if ( k ) -- k ; int j = sa[rank[i] - 1] ; while ( s[i + k] == s[j + k] ) ++ k ; height[rank[i]] = k ; } } void da ( int n , int m = 300 ) { int i , d , p , *x = t1 , *y = t2 , *t ; for ( i = 0 ; i < m ; ++ i ) c[i] = 0 ; for ( i = 0 ; i < n ; ++ i ) ++ c[x[i] = s[i]] ; for ( i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ; for ( i = n - 1 ; i >= 0 ; -- i ) sa[-- c[x[i]]] = i ; for ( d = 1 , p = 0 ; p < n ; d <<= 1 , m = p ) { for ( p = 0 , i = n - d ; i < n ; ++ i ) y[p ++] = i ; for ( i = 0 ; i < n ; ++ i ) if ( sa[i] >= d ) y[p ++] = sa[i] - d ; for ( i = 0 ; i < m ; ++ i ) c[i] = 0 ; for ( i = 0 ; i < n ; ++ i ) ++ c[xy[i] = x[y[i]]] ; for ( i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ; for ( i = n - 1 ; i >= 0 ; -- i ) sa[-- c[xy[i]]] = y[i] ; for ( t = x , x = y , y = t , p = 1 , x[sa[0]] = 0 , i = 1 ; i < n ; ++ i ) { x[sa[i]] = cmp ( y , sa[i - 1] , sa[i] , d ) ? p - 1 : p ++ ; } } getHeight ( n - 1 ) ; } int search ( int x , int l = 0 , int r = n ) { while ( l < r ) { int mid = ( l + r ) >> 1 ; if ( start[mid] >= x ) r = mid ; else l = mid + 1 ; } return start[l] == x ? l : l - 1 ; } int check ( int k , int cnt = 0 ) { ++ Time ; For ( i , 2 , m ) { if ( height[i] < k ) { if ( cnt > n / 2 ) return 1 ; ++ Time ; cnt = 0 ; } else { int x = search ( sa[i - 1] ) ; int y = search ( sa[i] ) ; if ( vis[x] != Time ) { ++ cnt ; vis[x] = Time ; } if ( vis[y] != Time ) { ++ cnt ; vis[y] = Time ; } } } if ( cnt >= ( n + 1 ) / 2 ) return 1 ; return 0 ; } void print ( int k , int cnt = 0 ) { ++ Time ; For ( i , 2 , m ) { if ( height[i] < k ) { if ( cnt > n / 2 ) { int L = sa[i - 1] ; int R = sa[i - 1] + k ; rep ( j , L , R ) printf ( "%c" , s[j] - 100 ) ; printf ( "\n" ) ; } ++ Time ; cnt = 0 ; } else { int x = search ( sa[i - 1] ) ; int y = search ( sa[i] ) ; if ( vis[x] != Time ) { ++ cnt ; vis[x] = Time ; } if ( vis[y] != Time ) { ++ cnt ; vis[y] = Time ; } } } } void solve () { m = 0 ; start[n] = MAXN ; rep ( i , 0 , n ) { start[i] = m ; scanf ( "%s" , buf ) ; int len = strlen ( buf ) ; rep ( j , 0 , len ) s[m + j] = buf[j] + 100 ; s[m + len] = i ;//*********divide sign m += len + 1 ; } s[m - 1] = 0 ; da ( m ) ; int l = 0 , r = 1000 ; while ( l < r ) { int mid = ( l + r + 1 ) >> 1 ; if ( check ( mid ) ) l = mid ; else r = mid - 1 ; } if ( !l ) printf ( "?\n" ) ; else print ( l ) ; } int main () { int flag = 0 ; Time = 0 ; clr ( vis , 0 ) ; while ( ~scanf ( "%d" , &n ) && n ) { if ( flag ) printf ( "\n" ) ; flag = 1 ; solve () ; } return 0 ; }