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. 日常Java 2021/10/24

    Java ArrrayList ArrayList类是一个可以动态修改的数组,没有固定大小的限制,可以在任何时候添加或者删除元素 ArrayList类在java.util包中使用之前需要引用 E:泛型 ...

  2. 在 Qualys SSL Labs SSL 测试中获得 A+ 评级的秘技 2021 版

    本系列文章将阐述主流应用交付控制器和主流 Web 服务器如何运行 HTTP/2 和 TLSv1.3 协议,以及如何在 SSL Test 中获得 A+ 评级. 请访问原文链接:https://sysin ...

  3. Oracle中的job(定时任务)

    oracle中的job(定时任务)由dbms_job包下的函数实现.关于job的理论知识可参考https://blog.csdn.net/apextrace/article/details/77675 ...

  4. java poi导出多sheet页

    /** * @Title: exportExcel * @Description: 导出Excel的方法 * @param workbook * @param sheetNum (sheet的位置,0 ...

  5. docker创建tomcat容器无法正常访问

    记一次创建tomcat docker容器后访问是404,进入到tomcat docker容器后发现webapps是空的 1.挂载 docker run -v localConfigFile:/cont ...

  6. tomcat 之 httpd session stiky

    # 注释中心主机 [root@nginx ~]# vim /etc/httpd/conf/httpd.conf #DocumentRoot "/var/www/html" #:配置 ...

  7. 关于requests.exceptions.ConnectionError: HTTPSConnectionPool的问题

    错误如下: raise ConnectionError(e, request=request)requests.exceptions.ConnectionError: HTTPSConnectionP ...

  8. 【MySQL】亲测可用的教程筛选:安装与卸载

    windows版本的 安装看这篇,非常详细:https://www.cnblogs.com/winton-nfs/p/11524007.html 彻底清除:https://www.pianshen.c ...

  9. js--生成器总结

    前言 生成器gengrator是es6 新增的函数功能,它允许你定义一个包含自有迭代算法的函数, 同时它可以自动维护自己的状态. 本文来总结一下JavaScript 中生成器的相关知识点. 正文 1. ...

  10. 特定场景下的PLC 远程控制和数据读取

    最近有位博友提出了一种应用场景,根据工作中实际遇到的类似的产品应用场景,记录下自己的解决方案. 场景: 需要在云端控制和采集各个站点的PLC数据.各个站点是分散的,每个站点有公网访问能力,但是分散站点 ...