input

字符串s   1<=len(s)<=300000

n    1<=n<=4000

word1

word2

...

wordn

1<=len(wordi)<=100

output

由一个或多个word拼成s的种数%20071027.

做法1:dp:单词长度最多为100,d[i]表示到第i个字符结束的种数,则如果d[j]到d[i]这段字符能从trie中找到,d[i]+=d[j],i-100<j<=i

 //1225ms
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
#define mod 20071027
struct trie
{
int ch[*MAX][];
bool val[*MAX];
int sz;
trie()
{
sz=;
memset(ch[],,sizeof(ch[]));
}
int idx(char &c) { return c-'a';}
char* insert(char*s)
{
int u=;
for(;*s;s++)
{
int c=idx(*s);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=;
return s;
}
int find(char*s,int n)
{
int u=;
for(int i=;i<n;i++)
{
int c=idx(s[i]);
if(!ch[u][c]) return ;
u=ch[u][c];
}
return val[u];
}
};
trie t;
char s[*MAX+],word[];
int n,maxl,d[*MAX+],cas=;
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
while(scanf("%s",s)==)
{
memset(t.ch[],,sizeof(t.ch[]));
t.sz=;
scanf("%d",&n);
maxl=;
while(n--)
{
scanf("%s",word);
int l=t.insert(word)-word;
maxl=std::max(maxl,l);
}
n=strlen(s);
memset(d,,sizeof(d));
int u=,work=;
for(int i=;i<maxl;i++)
{
int c=t.idx(s[i]);
if(!t.ch[u][c]) break;
u=t.ch[u][c];
if(t.val[u])
{
work=;
d[i]=;
}
}
// for(int i=0;i<=n;i++) printf("%d ",d[i]);printf("\n");
if(work)
{
for(int i=;i<n;i++)
{
for(int j=;j<=maxl;j++)
{
if(i-j>=&&t.find(s+i-j+,j)) d[i]=(d[i]+d[i-j])%mod;
}
}
}
// for(int i=0;i<=n;i++) printf("%d ",d[i]);printf("\n");
printf("Case %d: %d\n",cas++,d[n-]);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

dp

做法2:直接模拟:一开始只有一条路找,每个单词结束时分出一条路从头开始

 //79ms
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#define MAX 100000
#define LL long long
#define mod 20071027
struct trie
{
int ch[*MAX][];
bool val[*MAX];
int sz;
trie()
{
sz=;
memset(ch[],,sizeof(ch[]));
}
int idx(char &c) { return c-'a';}
void insert(char*s)
{
int u=;
for(;*s;s++)
{
int c=idx(*s);
if(!ch[u][c])
{
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
}
val[u]=;
}
};
trie t;
char s[*MAX+],word[];
int n,cas=,head[][],count[][],num[];
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
while(scanf("%s",s)==)
{
memset(t.ch[],,sizeof(t.ch[]));
t.sz=;
scanf("%d",&n);
while(n--)
{
scanf("%s",word);
t.insert(word);
}
int d=;
num[d]=;
head[d][]=;
count[d][]=;
for(char*p=s;*p;p++,d^=)
{
// printf("%d %d\n",count[d][0],d);
int &idx=num[d^]=;
int c=t.idx(*p);
head[d^][]=;
count[d^][]=;
for(int i=;i<num[d];i++)
{
int &f=head[d][i];
if(t.ch[f][c])//继续走到下一个字母
{
if(t.val[t.ch[f][c]])//走完一个单词,从头开始,总是head[d][0]
{
count[d^][]+=count[d][i];
count[d^][]%=mod;
}
head[d^][idx]=t.ch[f][c];
count[d^][idx++]=count[d][i];
}
}
}
// printf("%d %d\n",count[d][0],d);
printf("Case %d: %d\n",cas++,count[d][]);
}
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return ;
}

模拟

UVALive - 3942 Remember the Word的更多相关文章

  1. UVALive - 3942:Remember the Word

    发现字典里面的单词数目多且长度短,可以用字典树保存 f[i]表示s[i~L]的分割方式,则有f[i]=∑f[i+len(word[j])]   其中word[j]为s[i~L]的前缀 注意字典树又叫前 ...

  2. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. 【暑假】[实用数据结构]UVAlive 3942 Remember the Word

    UVAlive 3942 Remember the Word 题目: Remember the Word   Time Limit: 3000MS   Memory Limit: Unknown   ...

  5. UVALive 3942 Remember the Word 字典树+dp

    /** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...

  6. UVALive 3942 Remember the Word

    题意:给出一个由S个不同单词组成的字典和一个长字符串.把这个字符串分解成若干个单词的连接(单词可以重复 使用),有多少种方法? Sample Input abcd 4 a b cd ab Sample ...

  7. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

  8. UVALive 3942 Remember The Word (Tire)

    状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...

  9. UVALive - 3942 Remember the Word (Trie + DP)

    题意: 给定一篇长度为L的小写字母文章, 然后给定n个字母, 问有多少种方法用这些字母组成文章. 思路: 用dp[i]来表达[i , L]的方法数, 那么dp[i] 就可以从dp[len(x) + i ...

随机推荐

  1. 【Python&Sort】QuickSort

    Python版的快排,使用递归. 1.设置递归终止条件,当元素个数<1时 2.从列表中pop出一个元素pv 3.列表中的剩余值与pv进行对比,大的放入smaller列表,小的放入larger列表 ...

  2. visual studio2013 改变匹配括号的颜色

    改变匹配括号的颜色实现如下效果

  3. 2016弱校联盟十一专场10.2——Floyd-Warshall

    题目链接:Floyd-Warshall 题意: 给你n个点,m条边,100>m-n>0,现在有q个询问,问你任意两点的最短距离,题目保证每条边都被连接,每条边的距离为1 题解: 首先我们可 ...

  4. Struts2配置文件动态传参

    两个Action动态传递参数: 1.从一个Action传递到另一个Action如果带有参数那么result中的type属性需要使用redirect 第一个Action代码如下: private Str ...

  5. 用 rebar 来构建、编译、测试、发布 Erlang 应用程序

    转自:http://dhq.me/build-compile-eunit-release-erlang-application-with-rebar rebar 是一个遵循 Erlang/OTP 原则 ...

  6. CCleaner(著名清理软件) 5.21.5700 中文免费版(著名清理软件) 5.21.5700 中文免费版

    软件名称: CCleaner(著名清理软件) 5.21.5700 中文免费版著名清理软件(CCleaner)软件语言: 多国语言授权方式: 免费软件运行环境: Win 32位/64位软件大小: 5.6 ...

  7. SEO优化技巧总结

    SEO优化技巧总结 一:了解搜索引擎 基础知识 搜索引擎:由蜘蛛程序沿着链接爬行和抓取网上的大量页面,存进数据库,经过预处理,用户在搜索框 输入关键词后,搜索引擎排序从数据库中挑选出符合搜索关键词要求 ...

  8. iOS学习笔记(01) - 泛型

    决定新开一坑,在不断学习的同时分享自己的学习历程给大家,既是对自己学习的记录,又希望能对大家提供些微的帮助. 这一篇文章主要来介绍泛型的意义.使用与声明方法等. 1.泛型:限制类型 1.1.泛型使用场 ...

  9. 在Dll中创建对话框并调用

    1.第一步创建一“MFC AppWizard (dll)”工程,接下来选择“Regular Dll using shared MFC DLL”,点击“Finish”. 2.添加一对话框资源到工程中,从 ...

  10. 只能在执行 Render() 的过程中调用 RegisterForEventValidation

    当用GridView导出Execl的时候,会发生只能在执行 Render() 的过程中调用 RegisterForEventValidation的错误提示.有两种方法可以解决以上问题: 1.修改web ...