zoukankan      html  css  js  c++  java
  • CodeForces-427D:Match & Catch (后缀自动机)

    Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings s1 and s2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task.

    Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string.

    Given two strings s1 and s2 consist of lowercase Latin letters, find the smallest (by length) common substring p of both s1 and s2, where p is a unique substring in s1 and also in s2. See notes for formal definition of substring and uniqueness.

    Input

    The first line of input contains s1 and the second line contains s2 (1 ≤ |s1|, |s2| ≤ 5000). Both strings consist of lowercase Latin letters.

    Output

    Print the length of the smallest common unique substring of s1 and s2. If there are no common unique substrings of s1 and s2 print -1.

    Example

    Input
    apple
    pepperoni
    Output
    2
    Input
    lover
    driver
    Output
    1
    Input
    bidhan
    roy
    Output
    -1
    Input
    testsetses
    teeptes
    Output
    3

    题意:给定字符串S,T。求最短的公共字串s的长度|s|,s满足在各自的母串里只出现了一次。

    思路:建立S的后缀自动机,拓扑得到每个字串出现的次数num1。 然后T在后缀自动机上面走,也得到字串T的次数num2。 然后每个集合如果在各自串都只出现一次,就可以用当前集合的最小长度比较答案了。

    经验:对建立后缀自动机的S串,在设置num1的时候,可以在np处设置num1=1(也就是所有Blue节点),或者建立完自动机后跑一遍,跑到的地方num1=1。

    具体的可以参考代码。

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int inf=10000000;
    const int maxn=20005;
    int ans=inf,L; char c[maxn];    
    struct Sam
    {
        int fa[maxn],ch[maxn][26],maxlen[maxn],Last,cnt;
        int a[maxn],b[maxn],num1[maxn],num2[maxn],np,nq,p,q;
        Sam(){
            Last=cnt=1;
        }
        void add(int x)
        {
            np=++cnt; p=Last; Last=np;
            maxlen[np]=maxlen[p]+1; num1[np]++;//方法1 
            while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
            if(!p) fa[np]=1;
            else {
                q=ch[p][x];
                if(maxlen[p]+1==maxlen[q]) fa[np]=q;
                else {
                    nq=++cnt; maxlen[nq]=maxlen[p]+1;
                    fa[nq]=fa[q]; fa[q]=fa[np]=nq;
                    memcpy(ch[nq],ch[q],sizeof(ch[nq]));
                    while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
                }
            }
        }
        void sort()
        {
            for(int i=1;i<=cnt;i++) a[maxlen[i]]++;
            for(int i=1;i<=cnt;i++) a[i]+=a[i-1];
            for(int i=1;i<=cnt;i++) b[a[maxlen[i]]--]=i;    
            //for(int i=1,tp=1;i<=cnt;i++){
            //    tp=ch[tp][c[i]-'a']; num1[tp]++;
            //}
            //方法2 
            for(int i=cnt;i>=0;i--) num1[fa[b[i]]]+=num1[b[i]];
        }
        void solve()
        {
            scanf("%s",c+1); L=strlen(c+1); p=1;
            for(int i=1;i<=L;i++){
                int x=c[i]-'a';
                if(ch[p][x]) p=ch[p][x];
                else {
                    while(p&&!ch[p][x]) p=fa[p];
                    if(!p) p=1;
                    else p=ch[p][x];
                }
                num2[p]++;
            }
            for(int i=cnt;i>=0;i--) num2[fa[b[i]]]+=num2[b[i]];
            for(int i=2;i<=cnt;i++) 
              if(num1[i]==1&&num2[i]==1)
               ans=min(ans,maxlen[fa[i]]+1);
            if(ans==inf) ans=-1; 
        }
    }sam;
    int main()
    {
        scanf("%s",c+1); L=strlen(c+1);
        for(int i=1;i<=L;i++) sam.add(c[i]-'a');
        sam.sort(); sam.solve();
        printf("%d
    ",ans);
        return 0;
    }
  • 相关阅读:
    支持向量机SVM知识梳理和在sklearn库中的应用
    Android P HIDL demo代码编写 (原创)
    Android P HIDL服务绑定模式与直通模式的分析 (原创)
    NFCApplication 启动分析(原创)
    Android native进程间通讯的实例 (原创)
    android 6.0 高通平台sensor 工作机制及流程(原创)
    NFC OMA 访问
    NXP NFC移植及学习笔记(原创)
    自定义View实战
    从0开始学自定义View -1
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8577129.html
Copyright © 2011-2022 走看看