大白书 209 remember the word

F - Remember the Word

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 1401

Appoint description: 
System Crawler  (2015-03-07)

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> , 1S4000<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);
     }
}
时间: 2024-12-17 04:42:10

大白书 209 remember the word的相关文章

大白书

UVA 11292 (简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 思路: 贪心,均从小到大排序.为每一条龙找一个恰好能杀他的骑士.简单贪心. UVA 11729 (经典贪心问题) 题意: n个任务,需要交代B分钟,执行J分钟,让你合理选择交代任务的次序,求得n个任务完成的最小总时长. 思路: 经典贪心,通过比较俩俩的关系,得到整个序列的贪心排序方法.这个

瞎搞大白书----(1)

/************************************************************************* > File Name: uva11300.cpp > Author: Baiyan > 题意: n个人分别有一些钱,要平均: 但是,每次只能给左右两边的人一些钱,n个人坐成环: 问最少移动多少钱: 解法: 大白书上P6页说的已经很详细了: 转化表示,使得问题转化成单变量最值问题, 然后问题转化成了数轴上到一些点距离和最小值: 那么就是中位

大白书第一章

UVA 11292 The Dragon of Loowater(简单贪心) 题意: n条恶龙,m个勇士,用勇士来杀恶龙.一个勇士只能杀一个恶龙.而且勇士只能杀直径不超过自己能力值的恶龙.每个勇士需要支付能力值一样的金币.问杀掉所有恶龙需要的最少金币. 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 20000 + 5; 4 int n, m; 5 int night[maxn], dragon[maxn

大白书伸展树学习笔记

void splay(Node * &o,int k) { int d=o->cmp(k);//cmp函数比较的是k和o->ch[0]->s+1的大小 if(d==1) k-=o->ch[0]->s+1; if(d!=-1) { Node *p=o->ch[d]; int d2=p->cmp(k); int k2=(d2==0?k:k-p->ch[0]->s-1); if(d2!=-1) { splay(p->ch[d2],k2); if

wenbao与数论(大白书)

最大公约数之和 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=26&problem=2421&mosmsg=Submission+received+with+ID+18788108 输入n 输出gcd(i,j) 1<=i<j<=n总和 1 #include <iostream> 2 usin

UVa 1401 Remember the Word

Trie+DP 大白书上的字典树训练题. 题意是说一个字符串可能有多少种小串组成. 例如 abcd 4 a b cd ab abcd=a+b+cd:abcd=ab+cd: 递推为:从最后一位往前,dp[i]=dp[i]+dp[i+ len[x]]  x为输入时的顺序,附加到节点中.是 i~strlen(S)的前缀.S[1,2,3,-,i,-len] 构建Trie树时,把顺序也附加到节点中. 最后search的时候 找出为多少个字符串的前缀,并把这些前缀都加起来. #include<cstdio>

UVALive - 3942 - Remember the Word (Trie树)

UVALive - 3942 Remember the Word Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing that Ray has a p

C#操作Word的超详细总结

本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置图片宽高以及给图片添加标题: 插入表格,格式化表格,往表格中插入数据: 保存Word,打印Word: 重新打开Word等. Visual studio版本:Visual Studio 2012(2010应该也可以) 准备工作: /* 1. 添加引用COM里面的 Microsoft Word 12.0

la3523 白书例题 圆桌骑士 双联通分量+二分图

具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include <string.h> #include <stack> #include <cstdio> using namespace std; struct Edge{int u,v;}; const int maxn = 1000+10; int pre[maxn],iscut[ma