3942 - Remember the Word
3942 - Remember the Word
思路:字典树+dp
dp[i]前i个字符,能由给的字串组成的方案数,那么dp[i] = sum(dp[i-k]);那么只要只要在字典树中查看是否有字串str[i-k+1,i]就行了;
1 #include<stdio.h>
2 #include<algorithm>
3 #include<stdlib.h>
4 #include<queue>
5 #include<iostream>
6 #include<string.h>
7 #include<math.h>
8 using namespace std;
9 typedef long long LL;
10 struct node
11 {
12 node *p[26];
13 bool flag;
14 node()
15 {
16 memset(p,0,sizeof(p));
17 flag = false;
18 }
19 };
20 char str[300005];
21 char ans[300];
22 char bns[300];
23 node *head;
24 void in(char *c,int l);
25 void ask(char *c,int k,int l);
26 LL dp[300005];
27 const LL mod = 20071027;
28 void fr(node *q);
29 int main(void)
30 {
31 int i,j;
32 int __ca = 0;
33 while(scanf("%s",str)!=EOF)
34 {
35 int l = strlen(str);
36 int n;
37 scanf("%d",&n);
38 head = new node();
39 while(n--)
40 {
41 scanf("%s",ans);
42 int k = strlen(ans);
43 in(ans,k);
44 }
45 memset(dp,0,sizeof(dp));
46 dp[0] = 1;
47 for(i = 0; i < l; i++)
48 {
49 int x = max(0,i-100);
50 int v = 0;
51 for(j = i; j >= x; j--)
52 {
53 bns[v++] = str[j];
54 }
55 bns[v] = '\0';
56 ask(bns,i+1,v);
57 }
58 fr(head);
59 printf("Case %d: ",++__ca);
60 printf("%lld\n",dp[l]);
61 }
62 return 0;
63 }
64 void in(char *c,int l)
65 {
66 int i,j;
67 node *root = head;
68 for(i = l-1; i >=0; i--)
69 {
70 int x = c[i]-'a';
71 if(root->p[x]==NULL)
72 {
73 root->p[x] = new node();
74 }
75 root = root->p[x];
76 }
77 root->flag = true;
78 }
79 void ask(char *c,int k,int l)
80 {
81 int i,j;
82 node *root = head;
83 for(i = 0; i < l; i++)
84 {
85 int x = c[i]-'a';
86 if(root->p[x]==NULL)
87 {
88 return ;
89 }
90 else if(root->p[x]->flag)
91 {
92 dp[k] = dp[k] + dp[k-i-1];
93 dp[k]%=mod;
94 }
95 root = root->p[x];
96 }
97 }
98 void fr(node *q)
99 {
100 int i,j;
101 for(i = 0; i < 26; i++)
102 {
103 if(q->p[i])
104 {
105 fr(q->p[i]);
106 }
107 }
108 free(q);
109 }
3942 - Remember the Word的更多相关文章
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 【暑假】[实用数据结构]UVAlive 3942 Remember the Word
UVAlive 3942 Remember the Word 题目: Remember the Word Time Limit: 3000MS Memory Limit: Unknown ...
- LA 3942 Remember the Word(前缀树&树上DP)
3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...
- UVALive 3942 Remember the Word 字典树+dp
/** 题目:UVALive 3942 Remember the Word 链接:https://vjudge.net/problem/UVALive-3942 题意:给定一个字符串(长度最多3e5) ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
- UVA - 1401 | LA 3942 - Remember the Word(dp+trie)
https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...
- UVAL 3942 Remember the Word(递推+Trie)
Remember the Word [题目链接]Remember the Word [题目类型]递推+Trie &题解: 蓝书P209,参考的别人公开代码 &代码: #include ...
- UVa Live 3942 Remember the Word - Hash - 动态规划
题目传送门 高速路出口I 高速路出口II 题目大意 给定若干种短串,和文本串$S$,问有多少种方式可以将短串拼成长串. 显然,你需要一个动态规划. 用$f[i]$表示拼出串$S$前$i$个字符的方案数 ...
随机推荐
- 基于tp5的免费开源企业官网系统
基于tp5的免费开源企业官网系统 基本功能: 自定义菜单,单页 添加新闻文章前台展示 前台页面自动适配电脑与手机端等.后台模板用的是:AdminLTE 项目放在github上有兴趣开源下载看看 htt ...
- “==” 和 equals()的区别
※ "==" 和 equals()的区别 ※ == :比较. 基本数据类型比较的是值:. 引用类型比较的是地址值. ※ equals(Object o):1)不能比较基本数据类型, ...
- 1.Thmeleaf模板引擎
1.Thmeleaf的基本语法 大部分的Thmeleaf表达式都直接被设置到HTML节点中,作为HTML节点的一个属性存在,这样同一个模板文件既可以使用浏览器直接打开,也可以放到服务器中用来显示数据, ...
- 制作一个有趣的涂鸦物联网小项目(涂鸦模组SDK开发 CBU BK7231N WiFi+蓝牙模组 HSV彩色控制)
实现的功能: l APP控制月球灯 l 本地月球灯控制 l APP控制"大白"颜色,实现各种颜色变身 l 门状态传感器状态APP显示 l 网络状态指示灯,连接服务器长亮, ...
- Lock 锁的实现
锁的种类 自旋锁(spinlock):无法获得锁,就一直循环获取,适合短时间的加锁 睡眠锁(sleeplock):为了防止长时间的循环等待,在获取不到锁时,进程陷入睡眠,当锁释放时对睡眠进程进行唤醒 ...
- 使用plantuml,业务交接就是这么简单
使用plantuml,业务交接就是这么简单 你好,我是轩脉刃. 最近交接了一个业务,原本还是有挺复杂的业务逻辑的,但发现交接过来的项目大有文章,在项目代码中有一个docs文件夹,里面躺着若干个 pum ...
- 安装火狐浏览器报错找不到VCRUNTIME140_1.DLL
产生原因参考及下载地址:https://cn.dll-files.com/vcruntime140_1.dll.html vcruntime140_1.dll 相关的错误可能源于多种不同原因.比如,错 ...
- set env export区别
set env export区别 set,env和export这三个命令都可以用来显示shell变量,区别[root@localhost root]# aaa=bbb[root@localhost r ...
- git 基本命令及idea集成使用
目录 git基本命令使用 设置签名 gitHub 服务配置秘钥 上传代码 更新代码 分支管理 bat脚本更新 idea集成git git基本命令使用 设置签名 签名和秘钥大多数是一起设置的,设置后一般 ...
- CF1154A Restoring Three Numbers 题解
Content 已知有三个正整数 \(a,b,c\),现在给出 \(a+b,a+c,b+c,a+b+c\)(不保证有序)的值,试求出 \(a,b,c\). 数据范围:\(2\leqslant a+b, ...