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

每一次转移的复杂度是O(maxlen),maxlen是单词的最长长度。

#include<bits/stdc++.h>
using namespace std; const int maxn = *//4000*98+26*26+26+1
,sigma_size = ; int ch[maxn][sigma_size];
bool vis[maxn];
int nd_cnt; const int LEN = 3e5+, mod = ;
char str[LEN];
int d[LEN]; void init()
{
nd_cnt = ; memset(ch[],,sizeof(ch[]));
} void add(char *s)
{
int u = ;
int n = strlen(s);
for(int i = ; i < n; i++){
int c = word[i]-'a';
if(!ch[u][c]){
memset(ch[nd_cnt],,sizeof(ch[nd_cnt]));
vis[nd_cnt] = false;
ch[u][c] = nd_cnt++;
}
u = ch[u][c];
}
vis[u] = true;
} int main()
{
//freopen("in.txt","r",stdin);
char word[];
int kas = ;
while(gets(str)){
init();
int S; scanf("%d\n",&S);
while(S--){
gets(word);
add(word);
}
int n = strlen(str);
d[n] = ;
for(int cur = n-; cur >= ; cur--){
int u = ; d[cur] = ;
for(int i = cur; i < n; i++){
int c = str[i]-'a';
if(!ch[u][c]) break;
u = ch[u][c];
if(vis[u]) d[cur] += d[i+];
}
d[cur] %= mod;
}
printf("Case %d: %d\n",++kas,d[]);
}
return ;
}

UVALive 3942 Remember The Word (Tire)的更多相关文章

  1. UVALive 3942 Remember the Word(字典树+DP)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

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

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

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

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

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

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

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

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

  6. C#操作Office.word(二)

    在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...

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

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

  8. 最全总结 | 聊聊 Python 办公自动化之 Word(中)

    1. 前言 上一篇文章,对 Word 写入数据的一些常见操作进行了总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 相比写入数据,读取数据同样很实用! 本篇文章,将谈谈如何全面读取 ...

  9. 最全总结 | 聊聊 Python 办公自动化之 Word(下)

    1. 前言 关于 Word 文档的读写,前面两篇文章分别进行了一次全面的总结 最全总结 | 聊聊 Python 办公自动化之 Word(上) 最全总结 | 聊聊 Python 办公自动化之 Word( ...

随机推荐

  1. HDU - 4821 String(窗口移动+map去重+hash优化)

    String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...

  2. css需要注意的地方

    如果一个元素设置了postion:abslute;top:20;botoom:20因为设置了top又设置了botoom所以元素会有高度.

  3. ue4学习资料

    官网中文 https://docs.unrealengine.com/latest/CHN/index.html 官网英文 https://docs.unrealengine.com/latest/I ...

  4. cogs1612. 大话西游

    1612. 大话西游 http://www.cogs.pro/cogs/problem/problem.php?pid=1612 ★★   输入文件:westward.in   输出文件:westwa ...

  5. [Xcode 实际操作]七、文件与数据-(4 )遍历文件夹中的文件

    目录:[Swift]Xcode实际操作 本文将演示如何遍历文件夹下的内容. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class V ...

  6. Java基础--基本规则、语法

    一.关键字.保留字.标识符.常量.变量 1.关键字:使用某种语言赋予特殊含义的单词. 2.保留字:没有赋予特殊含义,但以后可能会使用的单词. 3.标识符:自定义的单词,以数字.字母.下划线以及$符组成 ...

  7. 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错

    调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:

  8. shell常用正则表达式

    转载自: http://www.cnblogs.com/linhaifeng/p/6596660.html 和 http://www.blogjava.net/jasmine214--love/arc ...

  9. C语言实现折半插入算法

    #include <stdio.h> int BInsertSort(int array[],int left,int right){ //接收主函数调用语句中的实参传到这里的形参里 in ...

  10. win7设置管理员权限

    1.在运行中输入:secpol.msc 2.修改设置权限设置 3.在账户中, 将administrator启用并设置密码 将其他用户取消管理原权限,设置为user权限