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

背单词 Remember the world LA 3942

时间:2015-05-05 21:15:47      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 4000 * 100 + 10;
const int sigma = 26;
const int MAXNn = 300010;
const int mod = 20071027;
char words[ MAXNn ];
int  dp[ MAXNn ];
struct Trie
{
    int ch[MAXN][sigma];
    int value[MAXN];
    int sz;
    Trie() { sz = 1; memset( ch[0] , 0 , sizeof(ch[0]) ); }
    void init() { sz = 1; memset( ch[0] , 0 , sizeof(ch[0]) ); }
    int dix(char c) { return c - a ; }
    void Insert( char *s)
    {
        int len = strlen( s );
        int u = 0;
        int i , cur ;
        for( i = 0 ; i < len ; i++ )
        {
            cur = dix( s[i] );
            if( !ch[u][cur] )
            {
                memset( ch[sz] , 0 , sizeof( ch[sz] ) );
                value[sz] = 0;
                ch[u][cur] = sz++;
            }
            u = ch[u][cur];
        }
        value[ u ] = len;
    }
}tree;

int dfs( int cur , int len )
{
    if( dp[cur] != 0 )
        return dp[cur];
    if( cur == len )
        return 1;
    else if( cur > len )
        return 0;
    int sum = 0 , i , tmp , u = 0 ;
    for( i = cur ; i < len ; i++ )
    {
        tmp = tree.dix( words[i] );
        if( tree.ch[u][tmp] )
        {
            u = tree.ch[u][tmp];
            if( tree.value[u] )
            {
                sum += dfs( i+1 , len ) % mod;
            }
        }
        else
            break;
    }
    dp[cur] = sum % mod;
    return sum;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    char tmp[110];
    int  s , len , i;
    int count = 1;
    while( ~scanf("%s",words) )
    {
        scanf("%d",&s);
        len = strlen( words );
        tree.init();
        for( i = 0 ; i < s ; i++ )
        {
            scanf("%s",tmp );
            tree.Insert( tmp );
        }
        memset( dp , 0 ,sizeof(dp) );
        dfs( 0  , len );
        printf("Case %d: %d\n",count++ , dp[0] % mod);
    }
    return 0;
}

 

背单词 Remember the world LA 3942

标签:

原文地址:http://www.cnblogs.com/zhping/p/4479965.html

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