UVA 1401 Remember the Word

时间:2023-02-22 12:46:48

字典树优化DP

                               Remember the Word
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

UVA 1401  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<tex2html_verbatim_mark> , 1UVA 1401  Remember the WordSUVA 1401  Remember the Word4000<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>
#include <string> using namespace std; const int MOD=;
const int maxn=; int m,dp[];
char str[]; struct Trie
{
int tot,root,child[maxn][];
bool flag[maxn];
Trie()
{
memset(child[],,sizeof(child[]));
flag[]=false;
root=tot=;
}
void Init()
{
memset(child[],,sizeof(child[]));
flag[]=false;
root=tot=;
}
void Insert(const char*str)
{
int *cur=&root;
for(const char *p=str;*p;p++)
{
cur=&child[*cur][*p-'a'];
if(*cur==)
{
*cur=++tot;
memset(child[tot],,sizeof(child[tot]));
flag[tot]=false;
}
}
flag[*cur]=true;
}
bool query(const char* str,int i)
{
int *cur=&root;
int l=;
for(const char*p=str;*p&&*cur;p++,l++)
{
cur=&child[*cur][*p-'a'];
if(flag[*cur])
{
dp[i]=(dp[i]+dp[i+l])%MOD;
}
}
return (*cur&&flag[*cur]);
}
}tree; int main()
{
int cas=;
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
scanf("%d",&m);
tree.Init();
while(m--)
{
char dic[];
scanf("%s",dic);
tree.Insert(dic);
}
memset(dp,,sizeof(dp));
dp[len]=;
for(int i=len-;i>=;i--)
{
tree.query(str+i,i);
}
printf("Case %d: %d\n",cas++,dp[]%MOD);
}
return ;
}

UVA 1401 Remember the Word的更多相关文章

  1. UVA 1401 - Remember the Word&lpar;Trie&plus;DP&rpar;

    UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...

  2. UVA 1401 Remember the Word(用Trie加速动态规划)

    Remember the Word Neal is very curious about combinatorial problems, and now here comes a problem ab ...

  3. LA 3942 &amp&semi;&amp&semi; UVa 1401 Remember the Word &lpar;Trie &plus; DP&rpar;

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  4. UVA - 1401 Remember the Word(trie&plus;dp)

    1.给一个串,在给一个单词集合,求用这个单词集合组成串,共有多少种组法. 例如:串 abcd, 单词集合 a, b, cd, ab 组合方式:2种: a,b,cd ab,cd 2.把单词集合建立字典树 ...

  5. UVA - 1401 &vert; LA 3942 - Remember the Word(dp&plus;trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  6. UVa 1401 &lpar;Tire树&rpar; Remember the Word

    d(i)表示从i开始的后缀即S[i, L-1]的分解方法数,字符串为S[0, L-1] 则有d(i) = sum{ d(i+len(x)) | 单词x是S[i, L-1]的前缀 } 递推边界为d(L) ...

  7. uva 1401 dp&plus;Trie

    http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. uva 1401

    Neal is very curious about combinatorial problems, and now here comes a problem about words. Knowing ...

  9. 1401 - Remember the Word

    注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...

随机推荐

  1. 浮动div中的图片垂直居中

    table-cell方法垂直水平居中 <!DOCTYPE html> <html> <head> <meta name="description&q ...

  2. KeepAlived主备模型高可用LVS

    部署前准备: 1.至少4台主机:两个Director(HA1,HA2),两个Real Server(RS1,RS2) 2.Director之间时间必须同步,且关闭各主机的防火墙和Selinux 3.出 ...

  3. python mysql Connect Pool mysql连接池 &lpar;201

     easy_install mysql-connector-python >>>import mysql.connector as conner >>> conn ...

  4. 胜利大逃亡&lbrack;HDU1253&rsqb;

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. 【CSS】Beginner2&colon;Selectors&comma; Properties&comma; and Values

    1.Whereas HTML has tags,CSS has selectors.   2.Selector{ properties:value; properties2:value2; }   3 ...

  6. Linux脚本(二)

    1.for循环以及加法的使用 portStr=`lsof -i:56801 | head -2`count=0for str in `lsof -i:56801 | head -2`do ((coun ...

  7. Android SDK代理服务器解决国内不能更新下载问题(转)

    言:Android SDK代理服务器解决国内Android SDK不能更新下载问题,经常会遇到Fitch fail URL错误,要不就是Nothing was installed.目下Google遭受 ...

  8. mvc的IIS 配置问题 runAllManagedModulesForAllRequests 与 HtmlFileHandler

    runAllManagedModulesForAllRequests 一般设置为false,当为true时所有的资源将进入mvc处理,无疑会给服务器加大压力. 解决办法是时使用HtmlFileHand ...

  9. 【Maven】---Linux搭建Nexus3&period;X私服

    Linux搭建Nexus3.X私服 备注:linux版本: ubuntu 同时已经部署好JDK8环境 一.linux安装nexus 1.创建文件夹并进入该目录 cd /usr/local && ...

  10. 流式大数据计算实践(3)----高可用的Hadoop集群

    一.前言 1.上文中我们已经搭建好了Hadoop和Zookeeper的集群,这一文来将Hadoop集群变得高可用 2.由于Hadoop集群是主从节点的模式,如果集群中的namenode主节点挂掉,那么 ...