题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=282&page=show_problem&problem=1943

差点就被这个题目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题,字典树的更多相关文章

  1. C#LeetCode刷题-字典树

    字典树篇 # 题名 刷题 通过率 难度 208 实现 Trie (前缀树)   48.6% 中等 211 添加与搜索单词 - 数据结构设计   39.9% 中等 212 单词搜索 II   27.9% ...

  2. HDU1305 Immediate Decodability(水题字典树)

    巧了,昨天刚刚写了个字典树,手到擒来,233. Problem Description An encoding of a set of symbols is said to be immediatel ...

  3. 2010辽宁省赛F(字典树,动态规划)

    #include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010] ...

  4. HDU1671 水题字典树

    #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring> #inc ...

  5. Good Firewall(字典树 HDU4760)

    Good Firewall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 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, ...

  7. HDU-4825 Xor Sum,字典树好题!

    Xor Sum 一遍A了之后大呼一声好(keng)题!debug了两小时~~~~百度之星资格赛,可以. 题意:给你一个n个元素的数组,m次查询,每次输入一个数k要求从数组中找到一个数与k异或值最大,输 ...

  8. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  9. Hihicoder 题目1 : Trie树(字典树,经典题)

    题目1 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编 ...

随机推荐

  1. IOS 加载Xib 后 如何 动态修改xib中的控件frame

    看看xib里view是不是设置了自动布局 use auto layout.取消掉就可以了.

  2. Groupon面经:Find paths in a binary tree summing to a target value

    You are given a binary tree (not necessarily BST) in which each node contains a value. Design an alg ...

  3. 配置suse自动化安装

    配置suse自动化安装 作者:尹正杰   版权声明:原创作品,谢绝转载!否则将追究法律责任.       前言:不知道你习惯用那款虚拟器,我用的是VMware Workstation,别问我为什么,因 ...

  4. Python之urllib2

    urllib2 - extensible library for opening URLs Note The urllib2 module has been split across several ...

  5. MAC开发NDK非常的简单

    转自:http://www.cnblogs.com/jarrah/archive/2013/03/15/2961892.html 附带CDT的下载:http://www.eclipse.org/cdt ...

  6. 卸载了mysql之后,mysql服务仍在,显示读取描述失败,错误代码2

    卸载了mysql之后,mysql服务仍在,显示读取描述失败,错误代码2 用360软件管家,卸载mysql5.5,卸载了mysql之后,再依次删除 mysql的安装目录.c盘下的隐藏文件夹Program ...

  7. linux第4天 shell socket

    $[ ] 表示形式告诉shell对方括号中的表达式求值 echo $[3+9] 赋值运算符 =,+=,-=,*=,/=,%=,&=,^=.|=,<<=,>>= let ...

  8. II7下配置SSAS通过HTTP 远程链接访问

    IIS7下配置SSAS通过HTTP远程连接 安装环境操作系统:Windows7.Windows Server2008IIS版本:7.5 IIS7下配置SSAS通过HTTP远程连接详细的步骤如下:1.首 ...

  9. 夺命雷公狗---DEDECMS----22dedecms让A标签进入对应的内容页

    我们的模版里的超链接都是写死的,这都是不符合实际网站的需求的,我们要将他让他边活的,而并非死的.. 我们首先要将前端给我们的内容页面的模版放到目标目录里面,但是我们的内容页的模版名叫啥呢?我们可以来查 ...

  10. 夺命雷公狗jquery---3普通选择器

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...