码迷,mamicode.com
首页 > 其他好文 > 详细

每个字符串至少出现两次且不重叠的最长子串

时间:2018-08-18 19:37:52      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:queue   cout   turn   vector   sign   stdin   har   eva   -o   

Relevant Phrases of Annihilation

 SPOJ - PHRASES

https://cn.vjudge.net/problem/SPOJ-PHRASES

呵。。。呵。。。

我觉得我写的很对呀。。。

真是的。。。

这么漂亮。。。

行吧。。一下午没检查出来哪里有问题 真是的  太过分了。。。emm。。。

过几天再看。。啊啊啊。。。

 

网上正确代码:

#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 = 101000, INF = 0x7fffffff;
int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int ran[maxn], height[maxn], length[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;
    }
}
char str[maxn];
int id[maxn];
struct node
{
    int Min;
    int Max;
    int flag;
} anspos[15];
void ini()
{
    for(int i = 0; i <= 12; i++)
        anspos[i].Min = 0x3f3f3f3f,anspos[i].Max = -1;
}
bool can(int len,int n,int num)
{
    int l = 2,r = 2;
    ini();
    for(int i = 2; i <= n; i++)
    {
        if(height[i] >= len)
        {
            int id1=id[sa[i-1]];
            int id2=id[sa[i]];

            anspos[id1].Max=max(anspos[id1].Max,sa[i-1]);
            anspos[id1].Min=min(anspos[id1].Min,sa[i-1]);

            anspos[id2].Max=max(anspos[id2].Max,sa[i]);
            anspos[id2].Min=min(anspos[id2].Min,sa[i]);
            int t;
            for(t = 0; t < num; t++)
                if(anspos[t].Max - anspos[t].Min < len)
                    break;
            if(t == num)
                return 1;
        }
        else
        {
            for(int j = 0; j <= 10; j++)
                anspos[j].Min = 0x3f3f3f3f,anspos[j].Max = -1;
        }
    }
    for(int i = 0; i < num; i++)
        if(!anspos[i].flag)
            return 0;
    return 1;
}
int main()
{
    int k,q,t;
    scanf("%d",&t);
    while(t--)
    {
        ini();
        int tot = 0,len = 0x3f3f3f3f;
        scanf("%d",&q);
        for(int i = 0; i < q; i++)
        {
            scanf("%s",str);
            for(int j = 0; str[j]!=\0; j++)
            {
                id[tot] = i;
                s[tot++] = str[j];
            }
            id[tot] = i;
            s[tot++] = #+i;
            len = min(len,(int)strlen(str));
        }
        s[tot] = 0;
        n = tot+1;
        get_sa(200);
//        for(int i=1; i<n; i++)
//            cout<< height[i] <<endl;
        int l = 0,r = len;
        int ans = 0;
        while(l <= r)
        {
            int mid = l + (r - l) / 2;
            if(can(mid,tot,q))
            {
                l = mid+1;
            }
            else
                r = mid-1;
        }
        printf("%d\n",r);
    }
    return 0;
}

 

 

 

我的:

#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 = 101000, INF = 0x7fffffff;
int s[maxn];
int sa[maxn], t[maxn], t2[maxn], c[maxn], n;
int ran[maxn], height[maxn], length[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;
    }
}

struct node
{
    int minn, maxx;
}Node[15];

int solve(int k, int q)
{
    for(int i=1; i<=q; i++)
        Node[i].minn = INF, Node[i].maxx = -INF;
    int cnt = 0;
    for(int i=1; i<n; i++)
    {
        if(height[i] < k)
        {
            for(int j=1; j<=q; j++)
            {
                Node[j].minn = INF, Node[j].maxx = -INF;
            }

            continue;
        }
        for(int j=1; j<=q; j++)
        {
            if(sa[i] > length[j-1] && sa[i] < length[j])
            {
                Node[j].minn = min(Node[j].minn, sa[i]);
                Node[j].maxx = max(Node[j].maxx, sa[i]);
            }
            if(sa[i-1] > length[j-1] && sa[i-1] < length[j])
            {
                Node[j].minn = min(Node[j].minn, sa[i-1]);
                Node[j].maxx = max(Node[j].maxx, sa[i-1]);
            }
        }
        cnt = 0;
        for(int j=1; j<=q; j++)
        {
            if(Node[j].maxx - Node[j].minn >= k)
                cnt++;
        }
        if(cnt == q) return true;

    }
    return false;
}

int T, q, len;
char str[maxn];
int main()
{
    rd(T);
    while(T--)
    {
        n = 0;
        int l = 0, r = INF;
        length[0] = 0;
        rd(q);

        rep(i, 0, q)
        {
            rs(str);
            len = strlen(str);
            r = min(r, len);
            rep(j, 0, len)
                s[n++] = str[j];
            length[i+1] = n;
            s[n++] = # + i;
        }
        s[n++] = 0;
        get_sa(200);
//        for(int i=1; i<n; i++)
//            cout<< height[i] <<endl;
    //    cout<< l << "  " << r <<endl;
        while(l <= r)
        {
            int mid = l + (r - l) / 2;
            if(solve(mid, q)) l = mid + 1;
            else r = mid - 1;
        //    cout<< 111 <<endl;
        }
        cout<< r <<endl;

    }





    return 0;
}

 

每个字符串至少出现两次且不重叠的最长子串

标签:queue   cout   turn   vector   sign   stdin   har   eva   -o   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9498076.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!