Remember the Word

Remember the Word

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 , 1 S 4000 .

Each of the following S 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分析:trie+dp;   转移方程为dp[i]=dp[i]+dp[i+len[x]];代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 20071027
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
const int maxn=4e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,m,k,t,ch[26][maxn],sz,cas,lent[4010],num[maxn];
char str[maxn],node[4010][110];
vi tmp;
ll dp[maxn];
void insert(char *p,int len,int id)
{
    int now=0;
    for(int i=0;i<len;i++)
    {
        int x=p[i]-‘a‘;
        if(!ch[x][now])ch[x][now]=++sz;
        now=ch[x][now];
    }
    num[now]=id;
}
void gao(int p)
{
    int now=0;
    for(int i=p;str[i];i++)
    {
        int x=str[i]-‘a‘;
        if(num[now])dp[p]=(dp[p]+dp[p+lent[num[now]]])%mod;
        if(!ch[x][now])return;
        now=ch[x][now];
    }
    if(num[now])dp[p]=(dp[p]+dp[p+lent[num[now]]])%mod;
}
int main()
{
    int i,j;
    while(~scanf("%s",str))
    {
        int len=strlen(str);
        memset(dp,0,sizeof(dp));
        memset(num,0,sizeof(num));
        memset(ch,0,sizeof(ch));
        dp[len]=1;
        sz=0;
        scanf("%d",&n);
        rep(i,1,n){
            scanf("%s",node[i]);
            lent[i]=strlen(node[i]);
            insert(node[i],lent[i],i);
        }
        for(i=len-1;i>=0;i--)gao(i);
        printf("Case %d: %lld\n",++cas,dp[0]);
    }
    //system("Pause");
    return 0;
}
时间: 2024-10-10 19:45:41

Remember the Word的相关文章

Word中 简单宏的使用

 (注意:打开文档时按住 Shift 键可以阻止 AutoOpen 宏运行) 1:Word中能够自动运行的默认宏代码名称及触发条件如下 -------------------------------------------------------- 1.名称:AutoExec 条件:启动Word或加载全局模板 2.名称:AutoNew 条件:每次生成新文档时 3.名称:AutoOpen 条件:每次打开一个已有文档时 4.名称:AutoClose 条件:每次关闭文档时 5.名称:AutoExit

LeetCode58 Length of Last Word

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Qt 向word中插入文字(使用QAxWidget和QAxObject)

pro 文件中要加入 CONFIG += qaxcontainer 2. main.cpp [cpp] view plain copy #include <QApplication> #include <QAxWidget> #include <QAxObject> int main(int argc, char* argv[]) { QApplication app(argc, argv); QAxWidget *word=new QAxWidget("Wo

Atitit.office&#160;word&#160;&#160;excel&#160;&#160;ppt&#160;pdf&#160;的web在线预览方案与html转换方案&#160;attilax&#160;总结

Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求1 1.1. 显示效果要好1 1.2. 可以自定义显示界面1 1.3. 不需要控件,兼容性好1 1.4. 支持编辑操作1 2. 纯html预览解决之道(自由的格式)1 3. 转换swf flash方案2 4. 转换pdf方式..更多的浏览器已经直接支持pdf格式查看2 5. 控件方式2 6. Hyb

Java [Leetcode 58]Length of Last Word

题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档.时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用.感谢这位博友 静下心,认真做,

word Search

Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be us

word在试图打开时遇到错误

最近收邮件的时候碰到,附件打不开.报错如下: 最后百度了下,如下步骤解决 (word 2010 ) 1.点击word -->文件-->  选项 2.在弹出框中选择 信任中心 -->点击信任中心设置 3.在弹出对话框中选择如下设置:(取消受保护的视图前面的勾)

LeetCode:Word Pattern - 字符串模式匹配

1.题目名称 Word Pattern(字符串模式匹配) 2.题目地址 https://leetcode.com/problems/word-pattern/ 3.题目内容 英文:Given a pattern and a string str, find if str follows the same pattern. 中文:给出一组模式(pattern)和一个字符串(str),查看字符串是否与模式匹配 例如: pattern = "abba",str = "dog cat

[Excel]鼠标右键菜单没有新建Word、Excel、PPT怎么办?

很多朋友在安装好Office(2010或2013等)之后,发现右键新建中没有Word.Excel.PowerPoint等项,但是自己的Office却明明安装好了.这个时候该怎么办呢?这里,本文为大家提供一种简单的解决方法. 1.点击开始,找到运行命令行,输入regedit,打开注册表. 2.在左侧找到HKEY_CLASSES_ROOT目录,并展开. 3.首先,我们利用CTRL + F 快捷键,查找“docx”的位置,当然这里也可以自己下拉查找. 4.找到之后,不用展开目录.我们可以看到右侧有一个