2007 Asia - Nanjing F题,字典树
差点就被这个题目RE疯掉(ノへ ̄、)。
字典树:保存字符串集合。

用一个二维数组ch[i][j] 保存节点i,到标号j的叶子节点是否存在。一般val[i] 表示节点 i 对应的附加权值。
这个题目,给一个字典,一个文本,看文本可以有多少种分解方法。
用DP做,DP方程 dp[i] = sum(dp[i+len[x]]) ; 从后往前。
dp[i] 是从字符 i 开始的后缀数组的分解方案, x 是一个单词,是从 i 以后的单词
我RE的地方是ch数组用的char型,然后我一直查数组范围,LRJ的代码,我比照了好久O(≧口≦)O。
#include<cstring>
#include<vector> using namespace std; const int maxnode = *+;
const int sigma_size = ; struct Trie
{
int ch[maxnode][sigma_size];
int val[maxnode];
int sz; ///节点总数
void clear()
{
sz = ;
memset(ch[],,sizeof(ch[]));
} int idx(char c)
{
return c-'a';
} void insert(const char *s, int v)
{
int u = , n = strlen(s);
for(int i = ; i < n; i++)
{
int c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[sz], , sizeof(ch[sz]));
val[sz] = ;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = v;
} ///找字符串s不超过len的前缀
void find_prefixes(const char *s, int len, vector<int>& ans)
{
int u = ;
for(int i = ; i < len; i++)
{
if(s[i] == '\0') break;
int c = idx(s[i]);
if(!ch[u][c]) break;
u = ch[u][c];
if(val[u] != ) ans.push_back(val[u]); // 找到一个前缀
}
}
}; #include<cstdio>
const int maxl = +;
const int maxw = +;
const int maxwl = +;
const int MOD = ; int d[maxl],len[maxw];
char text[maxl],word[maxwl];
Trie trie; int main()
{
//freopen("in.txt","r",stdin);
int cases = ;
int s;
while(scanf("%s%d",text,&s)==)
{
trie.clear();
for(int i=; i<=s; i++)
{
scanf("%s",word);
len[i] = strlen(word);
trie.insert(word,i);
}
memset(d,,sizeof(d));
int L = strlen(text);
d[L] = ;
for(int i=L-; i>=; i--)
{
vector<int> p;
trie.find_prefixes(text+i,L-i,p);
for(int j=; j<p.size(); j++)
{
d[i] = (d[i]+d[i+len[p[j]]])%MOD;
}
}
printf("Case %d: %d\n",cases++,d[]);
}
return ;
}
2007 Asia - Nanjing F题,字典树的更多相关文章
- C#LeetCode刷题-字典树
字典树篇 # 题名 刷题 通过率 难度 208 实现 Trie (前缀树) 48.6% 中等 211 添加与搜索单词 - 数据结构设计 39.9% 中等 212 单词搜索 II 27.9% ...
- HDU1305 Immediate Decodability(水题字典树)
巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...
- 2010辽宁省赛F(字典树,动态规划)
#include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010] ...
- HDU1671 水题字典树
#include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...
- Good Firewall(字典树 HDU4760)
Good Firewall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU 4757 Tree(可持久化字典树)(2013 ACM/ICPC Asia Regional Nanjing Online)
Problem Description Zero and One are good friends who always have fun with each other. This time, ...
- HDU-4825 Xor Sum,字典树好题!
Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- Hihicoder 题目1 : Trie树(字典树,经典题)
题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...
随机推荐
- 示sudo: cd: command not found
执行sudo cd 时出现 sudo: cd: command not found 原因shell shell是一个命令解析器 所谓shell是一个交互式的应用程序. shell执行外部命令的 时候, ...
- linux 缺少动态连接库.so--cannot open shared object file: No such file or directory
error while loading shared libraries的解決方法 执行行程式時,如此遇到像下列這種錯誤: ./tests: error while loading shared l ...
- UITableView中的headerView改变颜色
UITableView中的headerView 默认颜色是灰色的 如果自定义headerView必须在headerview上加一个view作为添加的颜色 或者直接 -(UIView *)tableVi ...
- [转]-Dmaven.multiModuleProjectDirectory system propery is not set. 解决方案 适用于myeclipes 和 eclipes
eclipse中使用maven插件的时候,运行run as maven build的时候报错 -Dmaven.multiModuleProjectDirectory system propery is ...
- C++: getline函数
转自http://blog.sina.com.cn/s/blog_60263c1c0101ck25.html 学习C++的同学可能都会遇到一个getline()函数,譬如在C++premer中,标准s ...
- << 链式重载
#include <iostream> using namespace std; class Complex { private: int a, b; public: Complex(in ...
- 关于设置oracle中系统编号SYSID自动编号的问题;
http://liye9801.blog.163.com/blog/static/601970320086210039591/ 如何在oracle里设置自动编号列 2008-07-21 12:00:3 ...
- 取客户的银行帐号SQL
SELECT ibybanks.bank_name, --银行 ibybanks.bank_branch_name, --分行 ibybanks.bank_account_num_electronic ...
- C#语言基础2016/3/6
一. 基础知识 输入输出 Console.Write();//输出语句,自动换行 Console.WriteLine();//输出语句 Console.WriteLine();输入语句 Consol ...
- [php] PHP Fatal error: Class 'AMQPConnection' not found
When using rabbitmq, $this->conn = new AMQPConnection($conn_args); $this->conn->connect(); ...