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的更多相关文章

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

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

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

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

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

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

  4. LA 3942 Remember the Word(前缀树&树上DP)

    3942 - Remember the Word Neal is very curious about combinatorial problems, and now here comes a pro ...

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

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

  6. Trie + DP LA 3942 Remember the Word

    题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...

  7. UVA - 1401 | LA 3942 - Remember the Word(dp+trie)

    https://vjudge.net/problem/UVA-1401 题意 给出S个不同的单词作为字典,还有一个长度最长为3e5的字符串.求有多少种方案可以把这个字符串分解为字典中的单词. 分析 首 ...

  8. UVAL 3942 Remember the Word(递推+Trie)

    Remember the Word [题目链接]Remember the Word [题目类型]递推+Trie &题解: 蓝书P209,参考的别人公开代码 &代码: #include ...

  9. UVa Live 3942 Remember the Word - Hash - 动态规划

    题目传送门 高速路出口I 高速路出口II 题目大意 给定若干种短串,和文本串$S$,问有多少种方式可以将短串拼成长串. 显然,你需要一个动态规划. 用$f[i]$表示拼出串$S$前$i$个字符的方案数 ...

随机推荐

  1. MIT6.824 分布式系统实验

    LAB1 mapreduce mapreduce中包含了两个角色,coordinator和worker,其中,前者掌管任务的分发和回收,后者执行任务.mapreduce分为两个阶段,map阶段和red ...

  2. Zookeeper之创建组,加入组,列出组成员和删除组

    public class CreateGroup implements Watcher { private static final int SESSION_TIMEOUT=5000; //ZooKe ...

  3. int是几位;short是几位;long是几位 负数怎么表示

    其实可以直接通过stm32的仿真看到结果:(这里是我用keil进行的测试,不知道这种方法是否准确) 从上面看, char是8位  short是4*4=16位  int是8*4=32位  long是8* ...

  4. keepalived 高可用lvs的dr模型(vip与dip不在同一网段)

    现在rs1和rs2上面安装httpd并准备测试页 [root@rs1 ~]# yum install httpd -y [root@rs1 ~]# echo "this is r1" ...

  5. shell脚本实现网站日志分析统计

    如何用shell脚本分析与统计每天的访问日志,并发送到电子邮箱,以方便每天了解网站情况.今天脚本小编为大家介绍一款不错的shell脚本,可以实现如上功能. 本脚本统计了:1.总访问量2.总带宽3.独立 ...

  6. 【力扣】剑指 Offer 25. 合并两个排序的链表

    输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的. 示例1: 输入:1->2->4, 1->3->4输出:1->1->2->3-> ...

  7. Python绘制柱状图

    1.1Python绘制柱状图对应代码如下所示 import matplotlib.pyplot as plt import numpy as np from pylab import mpl mpl. ...

  8. NSData NSDate NSString NSArray NSDictionary 相互转化

    //    NSData  NSDate NSString NSArray NSDictionary json NSString *string = @"hello word"; ...

  9. STM32F103ZET6 核心板制作指引

    学点啥系列之 --STM32F103ZET6 核心板制作指引 原创资料,转载请联系 作者的话:会画stm32F103ZET6的话,rct6啥的简直不要太简单 一.电路总览 图1:电路整体 二.单片机部 ...

  10. ciscn_2019_c_1 1

    步骤: 先checksec,看一下开启了什么保护 可以看到开启了nx保护,然后把程序放入ida里面,观察程序代码 先shift+f12观察是否有system和binsh函数 发现没有system和bi ...