• SPOJ 7758 Growing Strings


    MGLAR10 - Growing Strings 

    Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters.
    Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle.Gene and Gina have a collection of photos of some strings at different times during their growth.
    The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they
    need your help to find an appropriate sequence of photos.
    Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures,so all strings in the sequence must be different.
    Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.
    Gene and Gina have a particular kind of farm. Instead of growing animals and vegetables, as it is usually the case in regular farms, they grow strings. A string is a sequence of characters. Strings have the particularity that, as they grow, they add characters to the left and/or to the right of themselves, but they never lose characters, nor insert new characters in the middle. 
     Gene and Gina have a collection of photos of some strings at different times during their growth. The problem is that the collection is not annotated, so they forgot to which string each photo belongs to. They want to put together a wall to illustrate strings growing procedures, but they need your help to find an appropriate sequence of photos.
    Each photo illustrates a string. The sequence of photos must be such that if si comes immediately before si+1 in the sequence, then si+1 is a string that may have grown from si (i.e., si appears as a consecutive substring of si+1). Also, they do not want to use repeated pictures, so all strings in the sequence must be different.
    Given a set of strings representing all available photos, your job is to calculate the size of the largest sequence they can produce following the guidelines above.

    Input

    Each test case is given using several lines. The first line contains an integer N representing the number of strings in the set (1 ≤ N ≤ 10^4). Each of the following N lines contains a different non-empty string of at most 1000 lowercase letters of the English alphabet. Within each test case, the sum of the lengths of all strings is at most 10^6.

    The last test case is followed by a line containing one zero.

    Output

    For each test case output a single line with a single integer representing the size of the largest sequence of photos that can be produced.

    Sample

    input
    6 plant ant cant decant deca an 2 supercalifragilisticexpialidocious rag 0

    output
    4 2

    #include<cstdio>
    #include<cstdlib>
    #include<queue>
    #include<cstring>
    using namespace std;
    const int sigma_size=26;
    const int N=1e4+5;
    char s[1005];
    struct Trie
    {
        Trie* next[sigma_size];
        Trie* fail;
        int v,cnt,sum;
        Trie()
        {
            memset(next,0,sizeof(next));
            fail=0;
            cnt=sum=v=0;
        }
    };
    struct AhoCorasickAutomata
    {
        Trie *root;
        AhoCorasickAutomata()
        {
            root=new Trie();
        }
        inline int idx(char c)
        {
            return c-'a';
        }
        void _insert(char *s,int v)
        {
            int len=strlen(s);
            Trie* p=root;
            for(int i=0;i<len;i++)
            {
                int c=idx(s[i]);
                if(p->next[c]==0)
                {
    
                    Trie* q=new Trie();
                    p->next[c]=q;
                }
                p=p->next[c];
            }
            p->v=v,p->cnt++;
        }
        void getfail()
        {
            queue<Trie*>Q;
            root->fail=0;
            Q.push(root);
            while(!Q.empty())
            {
                Trie* u=Q.front();Q.pop();
                for(int i=0;i<sigma_size;i++)
                    if(u->next[i])
                    {
                        Trie* p=u->fail;
                        while(p&&!p->next[i])p=p->fail;
                        u->next[i]->fail=p?p->next[i]:root;
                        u->next[i]->sum=max(u->sum,u->next[i]->fail->sum)+u->next[i]->cnt;
                        Q.push(u->next[i]);
                    }
            }
        }
    };
    int dfs(Trie* u,int ans)
    {
        ans=max(ans,u->sum);
        for(int i=0;i<sigma_size;i++)
            if(u->next[i])
                ans=max(ans,dfs(u->next[i],ans));
        return ans;
    }
    int main()
    {
        int n;
        while(scanf("%d",&n),n)
        {
            AhoCorasickAutomata ac;
            for(int i=1;i<=n;i++)
            {
                scanf("%s",s);
                ac._insert(s,i);
            }
            ac.getfail();
            printf("%d
    ",dfs(ac.root,-1));
        }
        return 0;
    }
  • 相关阅读:
    OpenResty 实现限流
    Java+Selenium——处理Alert弹窗
    openresty+lua做接口调用权限限制
    Nginx反爬虫: 禁止某些User Agent抓取网站
    pycharm Python error: PermissionError: [Errno 13] Permission denied:
    could not build theproxy_headers_hash, you should increase either proxy_headers_hash_max_size: 512or proxy_headers_hash_bucket_size: 64 解决 OpenResty
    js数组中去除重复值的几种方法
    20Jan2022 20:08:58.201 信息 [catalinaexec40] org.apache.coyote.AbstractProcessor.parseHost [${ip}:${port}] 是无效主机
    检测Selenium的js代码例子:
    Nginx教程配置文件详解
  • 原文地址:https://www.cnblogs.com/homura/p/5788991.html
Copyright © 2020-2023  润新知