Life Forms poj3294

来源:互联网 发布:入骨相思知不知 番外 编辑:程序博客网 时间:2024/05/28 11:48

  多个字符串连接注意每个串之间使用不同的分隔符分隔开


#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <queue>#include <algorithm>#include <vector>#include <cstring>#include <stack>#include <cctype>#include <utility>   #include <map>#include <string>  #include <climits> #include <set>#include <string>    #include <sstream>#include <utility>   #include <ctime>using std::priority_queue;using std::vector;using std::swap;using std::stack;using std::sort;using std::max;using std::min;using std::pair;using std::map;using std::string;using std::cin;using std::cout;using std::set;using std::queue;using std::string;using std::istringstream;using std::make_pair;using std::getline;using std::greater;using std::endl;using std::multimap;typedef long long LL;typedef unsigned long long ULL;typedef pair<int, int> PAIR;typedef multimap<int, int> MMAP;const int MAXN(111000);const int SIGMA_SIZE(26);const int MAXM(110);const int MAXE(4000010);const int MAXH(18);const int INFI((INT_MAX-1) >> 2);const int MOD(9999991);const ULL LIM(1000000000000000ull);struct SA{int S[MAXN];int sa[MAXN], t1[MAXN], t2[MAXN], cnt[MAXN], len, M;void init(int tl, int tm = 128){len = tl;M = tm;int *p1 = t1, *p2 = t2;for(int i = 0; i < M; ++i) cnt[i] = 0;for(int i = 0; i <= len; ++i) ++cnt[p1[i] = S[i]];for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];for(int i = len; i >= 0; --i) sa[--cnt[p1[i]]] = i;int temp = 1;for(int k = 1; temp <= len; k <<= 1){temp = 0;for(int i = len-k+1; i <= len; ++i) p2[temp++] = i;for(int i = 0; i <= len; ++i)if(sa[i] >= k)p2[temp++] = sa[i]-k;for(int i = 0; i < M; ++i) cnt[i] = 0;for(int i = 0; i <= len; ++i) ++cnt[p1[p2[i]]];for(int i = 1; i < M; ++i) cnt[i] += cnt[i-1];for(int i = len; i >= 0; --i) sa[--cnt[p1[p2[i]]]] = p2[i];swap(p1, p2);temp = 1;p1[sa[0]] = 0;for(int i = 1; i <= len; ++i)p1[sa[i]] = p2[sa[i-1]] == p2[sa[i]] && p2[sa[i-1]+k] == p2[sa[i]+k]? temp-1: temp++;M = temp;}}int rank[MAXN], height[MAXN];void getHeight(){int k = 0;for(int i = 1; i <= len; ++i)rank[sa[i]] = i;for(int i = 0; i < len; ++i){if(k) --k;int j = sa[rank[i]-1];while(S[i+k] == S[j+k]) ++k;height[rank[i]] = k;}}};SA sa;int K;int flag[MAXN];int ind;map<int, int> mp;bool legal(int value){memset(flag, 0, sizeof(flag));ind = 0;int i, j;for(i = 1; i <= sa.len; i = j){j = i+1;int cnt = 0;++ind;map<int, int>::iterator iter = mp.lower_bound(sa.sa[i]);if(iter->first-sa.sa[i] >= value){flag[iter->second] = ind;++cnt;}while(j <= sa.len && sa.height[j] >= value){iter = mp.lower_bound(sa.sa[j]);if(flag[iter->second] != ind){flag[iter->second] = ind;++cnt;}++j;}if(cnt >= K)return true;}return false;}vector<int> vec;int ans;void getans(){memset(flag, 0, sizeof(flag));int i, j;for(i = 1; i <= sa.len; i = j){j = i+1;int cnt = 0;++ind;map<int, int>::iterator iter = mp.lower_bound(sa.sa[i]);if(iter->first-sa.sa[i] >= ans){flag[iter->second] = ind;++cnt;}while(j <= sa.len && sa.height[j] >= ans){iter = mp.lower_bound(sa.sa[j]);if(flag[iter->second] != ind){flag[iter->second] = ind;++cnt;}++j;}if(cnt >= K)vec.push_back(sa.sa[i]);}}bool comp(const int &op1, const int &op2){int temp;for(int i = 0; i < ans; ++i)if((temp = sa.S[op1+i]-sa.S[op2+i]))return temp < 0;return false;}char str[1010];int main(){int n;bool println(false);while(scanf("%d", &n), n){K = n/2+1;mp.clear();int len = 0, tlen, sep = 128;int l = 1, r = 1;for(int i = 1; i <= n; ++i){scanf("%s", str);tlen = strlen(str);len += tlen;r = max(r, tlen);int j, k;for(j = len-tlen, k = 0; j < len; ++j, ++k)sa.S[j] = str[k];mp.insert(make_pair(len, i));if(i != n)sa.S[len++] = sep++;elsesa.S[len] = 0;}++r;sa.init(len, 256);sa.getHeight();while(l < r){int m = (l+r) >> 1;if(legal(m))l = m+1;elser = m;}ans = l-1;if(println)putchar('\n');println = true;if(ans){vec.clear();getans();sort(vec.begin(), vec.end(), comp);for(int i = 0; i < vec.size(); ++i){for(int j = 0; j < ans; ++j)putchar(sa.S[vec[i]+j]);putchar('\n');}}elseprintf("?\n");}return 0;}





原创粉丝点击