题目链接: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. Keepalived 安装与配置

    下载:http://www.keepalived.org/ what is keepalived? Keepalived is a routing software written in C. The ...

  2. ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析

    配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...

  3. 异常:Struts:org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find BasicDataSource

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.apache.common ...

  4. struts.xml什么时候加载

    http://www.cnblogs.com/jiemoxiaodi/p/5621697.html

  5. android05

    #ANR(application not response) 应用程序无响应原因: >主线程里面执行了耗时的操作.就会导致应用程序无响应. <自己动手写操作系统> 带界面的应用程序都 ...

  6. linux中编译git时提示找不到ssl.h头文件

    在centos中的解决方案是安装一个叫 openssl-devel 的包.

  7. 【crunch bang】tint2配置2

    # Tint2 config file # Background definitions # ID 1 rounded = 0 border_width = 0 background_color = ...

  8. SqlServer:此数据库处于单用户模式,导致数据库无法删除的处理

    此数据库处理单用户模式,尚在连接当中,无法删除(既使将SQLServer停止后再启动也是如此) USE [master] GO /****** Object: StoredProcedure [dbo ...

  9. 似是而非的k=sqrt(n)

    //题目:输入一个大于3的整数n,判定它是否为素数(prime,又称质数)#include <stdio.h>#include <math.h>int main(){int n ...

  10. javascript 正则表达式(二)

    /* 正则表达式方法:test(),exec(),String对象方法:match(),search(),replace(),split() 1.test()方法: 用法:  regexp对象实例.t ...