zoukankan      html  css  js  c++  java
  • Life Forms POJ

    题意:

      求不小于字符串一半长度个字符串中的最长字串

    解析:

      论文题例11

      将n个字符串连起来,中间用不相同的且没有出现在字符串中的字符隔开, 求后缀数组, 然后二分答案变为判定性问题,

    然后判断每组的后缀是否出现在不小于 k 个的原串中, 这个做法的时间复杂度为O(nlogn)

    #include <iostream>
    #include <cstdio>
    #include <sstream>
    #include <cstring>
    #include <map>
    #include <cctype>
    #include <set>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #define rap(i, a, n) for(int i=a; i<=n; i++)
    #define rep(i, a, n) for(int i=a; i<n; i++)
    #define lap(i, a, n) for(int i=n; i>=a; i--)
    #define lep(i, a, n) for(int i=n; i>a; i--)
    #define rd(a) scanf("%d", &a)
    #define rlld(a) scanf("%lld", &a)
    #define rc(a) scanf("%c", &a)
    #define rs(a) scanf("%s", a)
    #define MOD 2018
    #define LL long long
    #define ULL unsigned long long
    #define Pair pair<int, int>
    #define mem(a, b) memset(a, b, sizeof(a))
    #define _  ios_base::sync_with_stdio(0),cin.tie(0)
    //freopen("1.txt", "r", stdin);
    using namespace std;
    const int maxn = 1000005, INF = 0x7fffffff;
    int s[maxn];
    int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
    int ran[maxn], height[maxn], length[105], ans[maxn];
    bool vis[105];
    void get_sa(int m)
    {
        int i, *x = t, *y = t2;
        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(int k = 1; k <= n; k <<= 1)
        {
            int p = 0;
            for(i = n-k; i < n; i++) y[p++] = i;
            for(i = 0; i < n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
            for(i = 0; i < m; i++) c[i] = 0;
            for(i = 0; i < n; i++) c[x[y[i]]]++;
            for(i = 0; i< m; i++) c[i] += c[i-1];
            for(i = n-1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i];
            swap(x, y);
            p = 1; x[sa[0]] = 0;
            for(i = 1; i < n; i++)
                x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
            if(p >= n) break;
            m = p;
        }
        int k = 0;
        for(i = 0; i < n; i++) ran[sa[i]] = i;
        for(i = 0; i < n; i++)
        {
            if(k) k--;
            int j = sa[ran[i]-1];
            while(s[i+k] == s[j+k]) k++;
            height[ran[i]] = k;
        }
    }
    
    int solve(int k, int q)
    {
        mem(vis, 0);
        int cnt = 0, siz = 0;
        for(int i=1; i<n; i++)
        {
            if(height[i] < k)
            {
                if(cnt > q/2) ans[++siz] = sa[i-1];
                mem(vis, 0);
                cnt = 0;
                continue;
            }
            for(int j=1; j<=q; j++)
            {
                if(sa[i] < length[j] && sa[i] > length[j-1]) if(!vis[j]) cnt++, vis[j] = 1;
                if(sa[i-1] < length[j] && sa[i-1] > length[j-1]) if(!vis[j]) cnt++, vis[j] = 1;
            }
        }
        if(cnt > q/2) ans[++siz] = sa[n-1];
        if(siz)
        {
            ans[0] = siz;
            return true;
        }
        return false;
    }
    
    int q;
    char str[maxn];
    int main()
    {
        int flag = false;
        while(~rd(q) && q)
        {
            length[0]  = 0;
            int l = 1, r = 0, len;
            n = 0;
            int cnt = 28;
            rep(i, 0, q)
            {
                rs(str);
                if(!i) len = strlen(str), r = len;
                rep(j, 0, len)
                    s[n++] = str[j] - 'a' + 1;
                length[i+1] = n;
                s[n++] = cnt++;
            }
            s[n++] = 0;
            get_sa(200);
    
            while(l <= r)
            {
                int mid = l + (r - l) / 2;
                if(solve(mid, q)) l = mid + 1;
                else r = mid - 1;
            }
            if(flag) printf("
    ");
            else flag = true;
    
            if(l == 1)
            {
                printf("?
    ");
                continue;
            }
            for(int i=1; i<=ans[0]; i++)
            {
                for(int j=ans[i]; j<=ans[i]+r-1; j++)
                    printf("%c", s[j]-1 + 'a');
                printf("
    ");
            }
        }
        return 0;
    }
    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    C/S 架构和B/S架构的区别
    LoadRunner工作原理
    修改密码测试用例
    登录测试用例
    上传测试点
    avd 配置信息详解
    创建模拟器
    命令模式下启动模拟器,加载软件
    启动模拟器时出现Failed to create Context 0x3005
    SDK Manager更新时出现Failed to fectch URl https://dl-ssl.google.com/android/repository/addons_list.xml, reason: Connection to https://dl-ssl.google.com ref
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9496873.html
Copyright © 2011-2022 走看看