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

LA 3942 Remember the Word (Trie树)

时间:2016-05-13 23:20:06      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

——刘汝佳的白皮书里面介绍的题目。

/*
Problem:
Status :
By WF,
*/
#include "algorithm"
#include "iostream"
#include "cstring"
#include "cstdio"
#include "string"
#include "stack"
#include "cmath"
#include "queue"
#include "set"
#include "map"

#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e6+5;
const int mod=20071027;

char str[maxn];
char word[maxn];
int dp[maxn];
int n;



struct Trie
{
    int sz;
    int ch[maxn][26];
    int val[maxn];


    void Init()
    {
        sz=1;
        val[0]=0;
        memset(ch[0],0,sizeof ch[0]);

    }

    void Insert(char *s)
    {
        int n = strlen(s);
        int u = 0;
        for(int i=0;i<n;++i)
        {
            if( ch[u][ s[i]-‘a‘ ] ==0 )
            {
                memset(ch[sz],0,sizeof ch[sz]);//!!
                val[sz]=0;
                ch[u][ s[i]-‘a‘ ] = sz;
                sz++;
            }
            u = ch[u][ s[i]-‘a‘ ];
        }
        val[u]=n;
        //printf("insert:%s\n",s);
    }

    void Search(char * s,int k,int l)
    {
        int u = 0;
        for(int i=0;i<l;++i)
        {
            if( ch[u][ s[i]-‘a‘ ]==0 )return;
            u = ch[u][ s[i]-‘a‘ ];
            if( val[u] )
            {
                /*
                for(int j=0;j<val[u];++j)
                {
                    printf("%c",s[j]);
                }
                printf("\n");
                */
                dp[k] = ( dp[k] + dp[k+val[u] ] )%mod;
                //printf("dp[%d]=%d   %d\n",k,dp[k],val[u]);
            }
        }
    }
}trie;

int main()
{
    //freopen("in.txt","r",stdin);
    int cs=1;
    while( scanf("%s",&str)!=EOF )
    {
        trie.Init();

        scanf("%d",&n);
        for(int i=0;i<n;++i)
        {
            scanf("%s",&word);
            trie.Insert(word);
        }
        int len = strlen(str);
        dp[len]=1;
        for(int i=len-1;i>=0;--i)
        {
            dp[i]=0;
            trie.Search(str+i,i,len-i);
        }
        printf("Case %d: %d\n",cs++,dp[0]);
    }

    return 0;
}

  

LA 3942 Remember the Word (Trie树)

标签:

原文地址:http://www.cnblogs.com/bruce27/p/5491391.html

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