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. linux下vim的安装与配置(centos)

    1.vim的安装 #yum search vim   //查看vim相关软件信息 #yum install -y vim*  //在线安装vim 2.vim的配置 (1)~/.viminfo 在vim ...

  2. LearnPython_week4

    1.装饰器2.生成器3.迭代器4.内置方法5.可序列化6.项目规范化 1.装饰器 # -*- coding:utf-8 -*- # Author:Wong Du ### 原代码 def home(): ...

  3. C#集合Dictionary中按值的排序

    C#集合Dictionary中按值的降序排列 static void Main(string[] args) {             Dictionary<string, int> d ...

  4. 全网最详细的ReentrantReadWriteLock源码剖析(万字长文)

    碎碎念) 花了两天时间,终于把ReentrantReadWriteLock(读写锁)解析做完了.之前钻研过AQS(AbstractQueuedSynchronizer)的源码,弄懂读写锁也没有想象中那 ...

  5. Identity Server 4 从入门到落地(七)—— 控制台客户端

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  6. 谈谈你对volatile的理解

    1.volatile是Java虚拟机提供的轻量级的同步机制 1.1保证可见性1.2不保证原子性1.3禁止指令重排 JMM(Java内存模型Java Memory Model,简称JMM)本身是一种抽象 ...

  7. 最长公共子序列问题(LCS) 洛谷 P1439

    题目:P1439 [模板]最长公共子序列 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 关于LCS问题,可以通过离散化转换为LIS问题,于是就可以使用STL二分的方法O(nlogn ...

  8. 100个Shell脚本——【脚本7】批量建立用户

    [脚本7]批量建立用户 编写shell脚本,批量建立用户user_00, user_01, ... user_100并且所有用户同属于users组. 一.脚本 #!/bin/bash group=`c ...

  9. 集合类——Map集合、Properties属性文件操作

    1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...

  10. Java文件操作(求各专业第一名的学生)

    两个文件:info.txt 存放学生基本信息 学号 学院 专业 姓名 1001 计算机学院 软件工程 刘月 1002 生物工程 服装设计 孙丽 score.txt存放分数信息 学号 学科 成绩 100 ...