zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    大数据产品对比
    3人3天3桶水9个人9天用几桶水
    skatebroads
    手机全面屏屏占比93%以上解决方案
    POC
    公司网站 解决方案 案例
    GBT 31000-2015 社会治安综合治理基础数据规范 数据项 编码
    GBT 33200-2016 社会治安综合治理 综治中心建设与管理规范 GBT 31000-2015 社会治安综合治理基础数据规范
    大数据 交警 户外广告设施管理监管系统平台高空坠物智慧社区城市城管警务
    破解爱奇艺腾讯优酷会员限制
  • 原文地址:https://www.cnblogs.com/homura/p/5788991.html
Copyright © 2011-2022 走看看