首页 > 其他 > 详细

大白书 209 remember the word

时间:2015-03-12 22:12:38      阅读:237      评论:0      收藏:0      [点我收藏+]
F - Remember the Word
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Appoint description: 

Description

技术分享
 

Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a photographic memory and this may not trouble him, Neal gives it to Jiejie.

Since Jiejie can‘t remember numbers clearly, he just uses sticks to help himself. Allowing for Jiejie‘s only 20071027 sticks, he can only record the remainders of the numbers divided by total amount of sticks.

The problem is as follows: a word needs to be divided into small pieces in such a way that each piece is from some given set of words. Given a word and the set of words, Jiejie should calculate the number of ways the given word can be divided, using the words in the set.

 

Input 

The input file contains multiple test cases. For each test case: the first line contains the given word whose length is no more than 300 000.

The second line contains an integer S<tex2html_verbatim_mark> , 1技术分享S技术分享4000<tex2html_verbatim_mark> .

Each of the following S<tex2html_verbatim_mark> lines contains one word from the set. Each word will be at most 100 characters long. There will be no two identical words and all letters in the words will be lowercase.

There is a blank line between consecutive test cases.

You should proceed to the end of file.

 

Output 

For each test case, output the number, as described above, from the task description modulo 20071027.

 

Sample Input 

 

abcd 
4 
a 
b 
cd 
ab

 

Sample Output 

 

Case 1: 2
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int ch[500010][32],val[500010];
int sz,d[500010];
int idx(char c)
{
    return c-a;
}
void insert(char *s,int v)
{
    int  u=0,n=strlen(s);
    for(int i=0;i<n;i++)
    {
        int c=idx(s[i]);
        if(!ch[u][c])
        {
            memset(ch[sz],0,sizeof(ch[sz]));
            val[sz]=0;
            ch[u][c]=sz++;
        }
        u=ch[u][c];
    }
    val[u]=v;
}
int main()
{
    char s[500010],a[105];
     int  i,x,t=1,j;
    while(~scanf("%s",s))
     {
         memset(d,0,sizeof(d));
          sz=1;
        memset(ch[0],0,sizeof(ch[0]));
         scanf("%d",&x);
         for(i=1;i<=x;i++)
         {
             scanf("%s",a);
             insert(a,-1);
         }
         int root,i,j,k;
         int len=strlen(s);
          d[len]=1;
       for(i=len-1;i>=0;i--)
        {
             root=0;
          for(j=0,k=i;k<len;j++,k++)
           {
             int c=idx(s[k]);
             if(ch[root][c]==0)
                break;
             else if(val[ch[root][c]]==-1)
             {
               d[i] += d[i+j+1];
              if(d[i] >= 20071027) d[i] %= 20071027;
             }
              root=ch[root][c];
          }

     }
         printf("Case %d: %d\n",t++,d[0]%20071027);
     }
}

 

大白书 209 remember the word

原文:http://www.cnblogs.com/cancangood/p/4333477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!