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


题意##

给定一个由S个单词组成的字典和一个字符串,求将这个字符串分解为若干单词的连接有多少种分法。


想法一##

进行dp

dp[i]表示字符串从第i位往后有多少种分法

转移方程: dp[i]=sum { dp[j+1] | s[i~j]为一个单词 }

枚举每一个单词

复杂度 每一组数据Θ(len(s)·S)

略微有那么一点高

想法二##

还是dp,转移方程同上

一个个枚举单词太慢了,我们发现对某一个i所有可以的单词前缀都应相同

运用数据结构trie

从s[i]开始在trie上找,找到某一个单词的结束就相当于找到了一个j

复杂度 每一组数据Θ(maxlen(字典中的单词)·S)

这样就可以过啦


代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring> using namespace std; const int N = 300005;
const int P = 20071027; struct trie{
trie *ch[26];
int flag;
void clear(){
flag=0;
for(int i=0;i<26;i++) ch[i]=NULL;
}
}pool[105*4000],*root;
int cnt; int d[N];
void add(){
char s[105];
scanf("%s",s);
int len=strlen(s),id;
trie *p=root;
for(int i=0;i<len;i++){
id=s[i]-'a';
if(!p->ch[id]){
pool[++cnt].clear();
p->ch[id]=&pool[cnt];
}
p=p->ch[id];
}
p->flag++;
} char sh[N]; int main()
{
int len,n,i,j,kase=0;
trie *p;
root=&pool[++cnt];
while(scanf("%s",sh)!=EOF){
len=strlen(sh);
scanf("%d",&n);
pool[1].clear(); cnt=1;
for(i=0;i<n;i++) add();
d[len]=1;
for(i=len-1;i>=0;i--){
p=root;
d[i]=0;
for(j=i;j<len;j++){
if(p->ch[sh[j]-'a']) {
p=p->ch[sh[j]-'a'];
if(p->flag)
d[i]=(d[i]+d[j+1])%P;
}
else break;
}
}
printf("Case %d: %d\n",++kase,d[0]);
} return 0;
}

最新文章

  1. Nodejs 饭店
  2. Write Your software base on plugin(C/C++ ABI)
  3. CSS设置DIV背景色渐变
  4. 解析导航栏的url--selnium,beautifulsoup实战
  5. AngularJs自定义指令详解(2) - template
  6. JQuery的$(document).ready(function(){})与JS的window.onload 的各自优势!
  7. JavaScript设计模式与开发实践 - 单例模式
  8. 鸟哥的linux私房菜勘误表
  9. Android ScaleAnimation类:尺寸变化动画类
  10. 一致性hash应用到redis
  11. http协议中的Content-Type
  12. 用Java实现非阻塞通信
  13. java 多线程访问同一个对象数据保护的问题
  14. 2、for 循环
  15. C# Levenshtein计算字符串的相似度
  16. 左倾堆C++实现
  17. VC6 LINK : fatal error LNK1168: cannot open Debug/Test.exe for writing
  18. forever start app.js 启动node时,服务访问一次后第二次就不能访问了
  19. Docker容器学习梳理 - 容器间网络通信设置(Pipework和Open vSwitch)
  20. (Go rails)使用Rescue_from(ActiveSupport:Rescuable::ClassMethods)来解决404(ActiveRecord::RecordNotFound)❌

热门文章

  1. H3C根桥的选举
  2. H3C VLAN基本配置
  3. 【42.49%】【hdu 1542】Atlantis(线段树扫描线简析)
  4. 【Jenkins】pipeline-hello-world项目
  5. SQL常见命令
  6. C语言图形界面常用函数集锦
  7. myeclipse上进行tomcat远程调试
  8. 洛谷p1119--灾难后重建(Floyd不仅仅是板子)
  9. APP数据采集--基础配置
  10. 学习python库:elasticsearch-py