zoukankan      html  css  js  c++  java
  • Codeforces Round #106 (Div. 2) E. Martian Strings

    E. Martian Strings
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.

    Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n. When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting of uppercase Latin letters. The string's length is n.

    "Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers abcd, (1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such that a ≤ i ≤ b or c ≤ i ≤ d. After the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.

    Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.

    Input

    The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105). The second line contains an integer m (1 ≤ m ≤ 100) — the number of beautiful words. Next m lines contain the beautiful words pi, consisting of uppercase Latin letters. Their length is from 1 to 1000. All beautiful strings are pairwise different.

    Output

    Print the single integer — the number of different beautiful strings the Martian can see this morning.

    Sample test(s)
    input
    ABCBABA
    2
    BAAB
    ABBA
    output
    1
    Note

    Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 andc = 4, d = 5 or of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.

    题意:给出一个长的字符串str 和m 个小的字符串,然后让你找两个部分 str[a~b],str[c~d] ,a < c ,使得两部分连接起来等于某个给出的字符串,

    问一共可以组成多少个给出的字符串。

    对于每个小的字符串,我们dp[j] 表示后缀j 和长的字符串匹配位置(最后面的)

    这个可以用后缀自动机搞,因为要求最后面的,所以需要对建好的自动机经行拓扑排序。

    调了N久,真累

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    #include<set>
    #include<stack>
    #include<map>
    #include<ctime>
    #include<bitset>
    #define LL long long
    #define ll long long
    #define INF 4365899
    #define maxn 100052
    #define eps 1e-6
    #define mod 1000000007
    using namespace std;
    
    struct SAM
    {
        SAM *pre,*son[26] ;
        int len ,g ,Max;
    }que[maxn*2],*root,*tail,*b[maxn*2];
    int tot ;
    void add(int c ,int l)
    {
        SAM *p = tail,*np=&que[tot++] ;
        np->len=l;tail=np ;
        np->g=l;
        while(p&&p->son[c]==NULL)p->son[c]=np,p=p->pre ;
        if(p==NULL) np->pre = root ;
        else
        {
            SAM *q = p->son[c] ;
            if(p->len+1==q->len)np->pre = q ;
            else
            {
                SAM *nq = &que[tot++] ;
                *nq=*q ;
                nq->len = p->len+1;
                nq->g=l;
                np->pre=q->pre=nq;
                while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;
            }
        }
    }
    void init(int n )
    {
        tot=0;
        for(int i = 0 ; i < n ;i++)
        {
            que[i].g = 0 ;
            que[i].pre=NULL;
            memset(que[i].son,0,sizeof(que[i].son)) ;
        }
        root=tail=&que[tot++] ;
    }
    
    struct SAM1
    {
        SAM1 *pre,*son[26] ;
        int len ,g ,Max;
    }que1[maxn*2],*root1,*tail1,*b1[maxn*2];
    int tot1 ;
    void add1(int c ,int l)
    {
        SAM1 *p = tail1,*np=&que1[tot1++] ;
        np->len=l;tail1=np ;
        np->g=l;
        while(p&&p->son[c]==NULL)p->son[c]=np,p=p->pre ;
        if(p==NULL) np->pre = root1 ;
        else
        {
            SAM1 *q = p->son[c] ;
            if(p->len+1==q->len)np->pre = q ;
            else
            {
                SAM1 *nq = &que1[tot1++] ;
                *nq=*q ;
                nq->len = p->len+1;
                nq->g=l;
                np->pre=q->pre=nq;
                while(p&&p->son[c]==q) p->son[c]=nq,p=p->pre;
            }
        }
    }
    void init1(int n )
    {
        tot1=0;
        for(int i = 0 ; i < n ;i++)
        {
            que1[i].g = 0 ;
            que1[i].pre=NULL;
            memset(que1[i].son,0,sizeof(que1[i].son)) ;
        }
        root1=tail1=&que1[tot1++] ;
    }
    
    struct node
    {
        char a[1010] ;
    }qe[110];
    char a[maxn] ;
    int dp[110][1010],C[maxn];
    bool vi[maxn];
    
    int main()
    {
        int i,n,m,j,k;
        int cnt ,ans,len ;
        while(scanf("%s",a+1) != EOF)
        {
            n = strlen(a+1) ;
            init1(n*2) ;
            for( i = 1 ; i <= n ;i++)
                add1(a[i]-'A',i) ;
            reverse(a+1,a+n+1) ;
            a[n+1]='' ;
            init(n*2) ;
            for( i = 1 ; i <= n ;i++)
                add(a[i]-'A',i) ;
    
            cin >> m ;
            for( i = 1 ; i <= m ;i++)
                scanf("%s",qe[i].a) ;
            memset(dp,-1,sizeof(dp)) ;
            memset(vi,0,sizeof(vi));
            memset(C,0,sizeof(C));
            for( i = 0 ; i < tot ;i++)C[que[i].len]++;
            for( i = 1 ; i <= n ;i++)C[i] += C[i-1] ;
            for( i =0; i < tot ;i++)b[--C[que[i].len]] = &que[i];
            SAM *p;
            for( i =tot-1; i >=0;i--)
            {
                p = b[i];
                if(p->pre != NULL )
                    p->pre->g=min(p->pre->g,p->g);
            }
            ans=0;
            for( i = 1 ; i <= m ;i++)
            {
                k = strlen(qe[i].a) ;
                SAM *p ;
                p=root;
                for( j = k-1 ; j >= 0 ;j--)
                {
                    int c=qe[i].a[j]-'A';
                    if(p->son[c] != NULL)
                    {
                        p = p->son[c] ;
                        dp[i][j+1]=n-p->g+1 ;
                    }
                    else break ;
                }
                if(dp[i][1] != -1&&k>1)ans++,vi[i]=true;
            }
            /*for( i = 1 ; i <= m ;i++)
            {
                k = strlen(qe[i].a) ;
                for( j = 1 ; j <= k ;j++)
                    cout << dp[i][j] << " " ;
                puts("") ;
            }*/
            memset(C,0,sizeof(C));
            for( i = 0 ; i < tot1 ;i++)C[que1[i].len]++;
            for( i = 1 ; i <= n ;i++)C[i] += C[i-1] ;
            for( i =0; i < tot1 ;i++)b1[--C[que1[i].len]] = &que1[i];
            SAM1 *p1;
            for( i = tot1-1; i >=0;i--)
            {
                p1 = b1[i];
                if(p1->pre != NULL )
                    p1->pre->g=min(p1->pre->g,p1->g);
            }
            for( i = 1 ; i <= m ;i++)
            {
                k = strlen(qe[i].a) ;
                if(vi[i]||k==1) continue ;
                SAM1 *p ;
                p=root1;
                for( j = 0 ; j < k ;j++)
                {
                    int c=qe[i].a[j]-'A';
                    if(p->son[c] != NULL)
                    {
                        p = p->son[c] ;
                        if(p->g <dp[i][j+2])
                        {
                            ans++;
                            break ;
                        }
                    }
                    else break ;
                }
            }
            cout << ans << endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【转】什么时候用抽象类,什么时候用接口
    高内聚松耦合在程序设计中如何做到
    如何做高水平的程序项目设计者
    NHibernate条件查询(Criteria Query)
    Oracle学习笔记之表结构修改
    Java集合类和HashMap遍历
    Asp.net中基于Forms验证的角色验证授权
    Springconfig.xml数据库操作Bean配置
    Java 常用排序算法实现快速排序、插入排序、选择、冒泡
    .net消息队列
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4060977.html
Copyright © 2011-2022 走看看