LightOJ - 1322 - Worst Case Trie(DP)
链接:
https://vjudge.net/problem/LightOJ-1322
题意:
In Computer Science Trie or prefix tree is a data structure which is usually used to store some strings or some numbers. Unlike binary trees, edges contain characters. And a node actually represents a string which is found by taking the characters from the edges, in the path from root to leaf. For example, for {abc, ae, bd, bb, bc, abd} we get the following trie:
Now you are given a set of strings and each string uses one of the K character symbols, and in any string (from the set) a symbol occurs at most once. Your task is to find the number of nodes required if we make a trie with the strings, using the procedure described above. As you don't know the size of the set, your task is to find the worst case result. For example, if you have 2 character symbols, then you need 5 nodes in worst case as in the following trie (let the symbols be {a, b}):
思路:
对i个字符,他的每个分支都是i-1个字符,可以让i-1个字符的根节点变成i中的一个,再加上自己的根节点。
得到Dp[i] = i*Dp[i-1]+1
对于mod 10000,当点数mod 10000为0时答案为1,即答案又从1开始循环,所以直接将i%10000即可。
代码:
// #include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MOD = 1e9+7;
const int MAXN = 1e6+10;
int k;
int a[MAXN];
void Init()
{
a[0] = 1;
for (int i = 1;i < MAXN;i++)
a[i] = (a[i-1]*i+1)%10000;
}
int main()
{
// freopen("test.in", "r", stdin);
Init();
int t, cas = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d", &k);
printf("Case %d:", ++cas);
if (k <= 5)
{
printf(" %d\n", a[k]);
continue;
}
k %= 10000;
printf(" %04d\n", a[k]);
}
return 0;
}
LightOJ - 1322 - Worst Case Trie(DP)的更多相关文章
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA 3942 Remember the Word (Trie+DP)题解
思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...
- AtCoder Regular Contest 094 D Worst Case
Worst Case 思路: 使 a <= b 当 a == b 时 或者 a == b - 1 时,答案显然为 2 * (a - 1) 否则找到最大的 c ,使得 c * c < a * ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- Trie + DP LA 3942 Remember the Word
题目传送门 题意:(训练指南P209) 问长字符串S能由短单词组成的方案数有多少个 分析:书上的做法.递推法,从后往前,保存后缀S[i, len-1]的方案数,那么dp[i] = sum (dp[i+ ...
- UVA 1401 - Remember the Word(Trie+DP)
UVA 1401 - Remember the Word [题目链接] 题意:给定一些单词.和一个长串.问这个长串拆分成已有单词,能拆分成几种方式 思路:Trie,先把单词建成Trie.然后进行dp. ...
- LightOJ 1422 Halloween Costumes 区间dp
题意:给你n天需要穿的衣服的样式,每次可以套着穿衣服,脱掉的衣服就不能再穿了,问至少要带多少条衣服才能参加所有宴会 思路:dp[i][j]代表i-j天最少要带的衣服 从后向前dp 区间从大到小 更新d ...
- LightOJ - 1246 Colorful Board(DP+组合数)
http://lightoj.com/volume_showproblem.php?problem=1246 题意 有个(M+1)*(N+1)的棋盘,用k种颜色给它涂色,要求曼哈顿距离为奇数的格子之间 ...
随机推荐
- Go基础编程实践(一)—— 操作字符串
修剪空格 strings包中的TrimSpace函数用于去掉字符串首尾的空格. package main import ( "fmt" "strings" ) ...
- PB 点击标题行排序和双击打开编辑页面共存不冲突的方法
根据doubleclicked() 事件的参数 row 进行判断 大于0才进入编辑页面(不能用getrow()事件获取行id,双击标题行获取的是1) if row>0 then event ue ...
- idea中的调试按键(f5,f6,f7,f8,f9)
f5: 如果断点处存在方法,f5 则强制进入方法内部,然后一步一步执行方法体, 如果再遇到方法,则继续进入方法体,如此循环,直到执行到断点开始处: f6: 从断点处一步步执行以后的代码,会跳出断点所在 ...
- .net core中的Session以及HttpContext对象使用小结
session用于识别用户并保持用户信息,就是一个会话 ,在浏览器不关闭的前提下,可以保存用户的信息,比如登录的保存用户信息从一个网页跳转到另一个网页,你的用户信息就可以用session. .net ...
- tf.reduce_max的运用
a=np.array([[[[1],[2],[3]],[[4],[25],[6]]],[[[27],[8],[99]],[[10],[11],[12]]],[[[13],[14],[15]],[[16 ...
- elasticsearch原理学习笔记
https://mp.weixin.qq.com/s/dn1n2FGwG9BNQuJUMVmo7w 感谢,透彻的讲解 整理笔记 请说出 唐诗中 包含 前 的诗句 ...... 其实你都会,只是想不起 ...
- Microsoft Project项目管理工具
下载 网址 安装 要注意以前安装的32位的Office或者Visio时这里会检测到,这里也要装32位的.不能根据系统位数来了.下载前先看清你用的Microsoft的软件是什么版本. 使用 新建空白项目 ...
- 【转载】 C#使用Newtonsoft.Json组件来反序列化字符串为对象
在Asp.Net网站开发的过程中,很多时候会遇到对象的序列化和反序列化操作,Newtonsoft.Json组件是专门用来序列化和反序列化操作的一个功能组件,引入这个DLL组件后,就可使用JsonCon ...
- java基本结构
前言 Java文件的运行过程: 1,javac.exe:编译器 2,java.exe:解释器 微软shell下运行实例: C:\Users\Administrator>cd D:\文档\JAVA ...
- JavaWeb 之 三层架构:软件设计架构
界面层(表示层):用户看的得界面.用户可以通过界面上的组件和服务器进行交互. 业务逻辑层:处理业务逻辑的. 数据访问层:操作数据存储文件.