题目链接: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. 推荐几个sql server牛人的博客

    Aaron Bertrand http://sqlblog.com/blogs/aaron_bertrand/ Brent Ozar www.brentozar.com/ Buck Woody htt ...

  2. Lintcode: Maximum Subarray III

    Given an array of integers and a number k, find k non-overlapping subarrays which have the largest s ...

  3. 点的双联通+二分图的判定(poj2942)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 10804   Acce ...

  4. android ANR问题

    一.什么是ANR ANR:Application Not Responding: 具体请参考:http://blog.csdn.net/dadoneo/article/details/8270107

  5. java装饰者模式理解

    java 装饰者模式其实就是扩展子类的功能,和继承是一个性质. 但继承是在编译时就固定扩展了父类的一些功能,而装饰者模式是在运行过程中动态绑定对象,实现一个子类可以随时扩展功能. 将方法排列组合,也可 ...

  6. paper 78:sniff抓包程序片段

    #define INTERFACE "eth0"#define MAX_SIZE 65535 int init_raw_socket();int open_promisc(char ...

  7. 【RoR win32】新rails运行后0.0.0.0:3000不能访问

    在浏览器中使用127.0.0.1:3000来访问

  8. empty()、html("")和text("")

    empty().html("")和text("")在删除匹配元素内内容时是一样的.jQuery源码中实现有所不同,但效果相同.可以测试一下源码: <!DO ...

  9. Visual studio 中编译错误SQL71006: Only one statement is allowed per batch. A batch separator, such as 'GO', might be required between statements.

    把写好的sql脚本,并在mssqlmanager里面编译成功的存储过程脚本复制到vs项目下,出现错误信息如下:SQL71006: Only one statement is allowed per b ...

  10. 数据结构之,线性表去除等于x的元素

    问题看起来很简单,但是这里有个限制,就是算法的时间复杂度位O(n),空间复杂度为O(1),下面上代码 #include <iostream> #include <string.h&g ...