Remember the Word,LA3942(Trie树+DP)
Trie树基础题,记录下代码。
#include <cstdio>
#include <cstring> #define MaxNode 4005*100
#define NodeSize 26
#define MOD 20071027 char givenword[300005];
int ans[300005];
int next[MaxNode][NodeSize];
class Trie{
public:
int val[MaxNode];
int sz;
Trie(){
sz = 1; //初始时有一个根结点
memset(next[0], 0, sizeof(next[0]));
}
int idx(char ch){
return ch - 'a';
}
void insert(char *s, int v){
int len = strlen(s);
int d = 0;
for(int i = 0; i < len; ++i){
int _idx = idx(s[i]);
if(!next[d][_idx]){
memset(next[sz],0,sizeof(next[sz]));
val[sz] = 0;
next[d][_idx] = sz++;
}
d = next[d][_idx];
}
val[d] = v;
}
void query(char *s,int pos){
int len = strlen(s);
int d = 0;
for(int i = 0; i < len; ++i){
int _idx = idx(s[i]);
if(!next[d][_idx]) return;
d = next[d][_idx];
if(val[d]){
ans[pos] += ans[pos+i+1];
if(ans[pos] > MOD) ans[pos] -= MOD;
}
}
return;
}
}; int main()
{
int S;
int Case = 1;
while(scanf("%s",givenword)!=EOF){
int len = strlen(givenword);
memset(ans,0,(len+1)*sizeof(int));
ans[len] = 1;
scanf("%d",&S);
char tstr[105];
Trie trie;
for(int i = 0;i < S;++i){
scanf("%s",tstr);
trie.insert(tstr,1);
}
for(int i = len-1; i >= 0; --i){
trie.query(&givenword[i],i);
}
printf("Case %d: ",Case++);
printf("%d\n",ans[0]);
}
return 0;
}
Remember the Word,LA3942(Trie树+DP)的更多相关文章
- LA-3942(trie树+dp)
题意: 给出一个由多个不同单词组成的字典,和一个长字符串,把这个字符串分解成若干个单词的连接,问有多少种方法; 思路: dp[i]表示s[i,L]的方案数,d[i]=∑d[j];s[i,j-1]是一个 ...
- NBUT 1222 English Game(trie树+DP)
[1222] English Game 时间限制: 1000 ms 内存限制: 131072 K 问题描写叙述 This English game is a simple English words ...
- POJ2004 Mix and build Trie树? dp?
学习Trie树中,所以上网搜一下Trie树的题,找到这个,人家写着是简单dp,那我就想着能学习到什么Trie树上的dp,但最后发现根本好像跟Trie树没有什么联系嘛... 题意就是给你很多个字符串(长 ...
- [POJ 1204]Word Puzzles(Trie树暴搜&AC自己主动机)
Description Word puzzles are usually simple and very entertaining for all ages. They are so entertai ...
- LA 3942 - Remember the Word (字典树 + dp)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- hdu4843(NOI2000) 古城之谜 (trie树+DP)
Description 著名的考古学家石教授在云梦高原上发现了一处古代城市遗址.让教授欣喜的是在这个他称为冰峰城(Ice-Peak City)的城市中有12块巨大石碑,上面刻着用某种文字书写的资料,他 ...
- BZOJ1212[HNOI2004]L语言——trie树+DP
题目描述 标点符号的出现晚于文字的出现,所以以前的语言都是没有标点的.现在你要处理的就是一段没有标点的文章. 一段文章T是由若干小写字母构成.一个单词W也是由若干小写字母构成.一个字典D是若干个单词的 ...
- UVa1401 Remember the Word(DP+Trie树)
题目给定一个字符串集合有几种方式拼成一个字符串. dp[i]表示stri...strlen-1的方案数 dp[len]=1 dp[i]=∑dp[j](stri...strj-1∈SET) 用集合的字符 ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
随机推荐
- 抓取锁的sql语句-第一次修改
CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); CUR_LOCK SYS_REFCURSOR; TYPE TP_LOCK ...
- <blockquote>标签,长文本引用
<blockquote>的作用也是引用别人的文本.但它是对长文本的引用,如在文章中引入大段某知名作家的文字,这时需要这个标签. 等等,上一节<q>标签不是也是对文本的引用吗?不 ...
- openssl提取pfx证书密钥对
刚做银联的项目,对方给了1.pfx和1.cer两个测试文件,总结一下利用这两个文件提取出文本 银联提供两个测试证书 1.pfx 和 1.cer . 其中 pfx证书包含RSA的公钥和密钥;cer证书 ...
- ios 中获得应用程序名称和版本号
IOS程序中的应用名称和版本号在 info.plist 文件中存储着,要想在程序中获得需要使用 NSBundle 对象 下面是示例代码: NSBundle *bundle = [NSBundle ma ...
- 关于一个隐藏和显示物品列表的demo
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- js 判断url的?后参数是否包含某个字符串
function GetQueryString(name){ var reg=eval("/"+name+"/g"); var r = window. ...
- c++builder向c#开发的webservice传递非数字参数
一.引用WebService地址 BCB6.0环境下,File-New-Other-WebService-WSDL Importer.然后手动写完整地址.如:“http://192.168.1.3:1 ...
- Razor引擎的转换数据类型
AsInt() 把字符串转换为整数. if (myString.IsInt()) IsInt() {myInt=myString.AsInt();} AsFloat() 把字符串转换为浮点数. if ...
- JS判断鼠标从哪个方向进入DIV容器
写的不够高大上 , 不要介意哦... Js: //进去 $(".flash").bind("mouseenter",function(e){ /** the w ...
- Day3 set集合、函数和装饰器
set特性:无序,不重复,可嵌套 创建setset_example = set("123", "213", "234", "432 ...