http://acm.ecnu.edu.cn/problem/3261/

分词

Time limit per test: 1.0 seconds

Time limit all tests: 1.0 seconds

Memory limit: 256 megabytes

有一句句子因为粘贴的时候出现了一点问题空格全部丢失了。现在给一本字典,每个词都对应这个词出现的频率(每十亿)。根据这个频率,我们可以根据下面的公式算出这个词带来的收益 P(word)

P(word)=len2(word)⋅ln(frequency(word))

其中 frequency 就是上面所提到的频率。len 指的是单词的长度。

特别的,对于字典中没有出现过的词,P(word)=0

请对句子进行适当的分割,使得分割得到的所有词收益之和最大。同一个词可以重复出现,收益算作多次。

Input

先给出一本词典,词典的第一行是词条数(词条数约为 40 000 ),下面每行分别是单词和对应出现频率,用空格隔开。单词中只会出现英文字母大小写,不会有多余的空格。每个单词只会出现一次。频率是一个正实数

所有单词长度之和不超过 3⋅105 ,最长单词长度不超过 30

接下来一行一个整数 T (T≤10) ,表示有 T 个查询。

下面 T 行,每行一个句子,句子长度不超过 5 000 。句子中保证只含有英文字母大小写。注意单词匹配时,不区分大小写

词典数据来源于 Wikipedia Project Gutenberg(可能需要代理),其中 1-10000 词汇。

查询数据来源于 IELTS Test。

Output

对于每组数据,输出两行。

第一行是一个实数,表示最大能达到的收益。输出和答案相差不超过 10−3 即可认为正确。

第二行输出一连串单词,单词和单词之间用空格隔开。满足:

  • 把这些单词依次串联起来可以得到原句子;
  • 所有单词的收益值相加得到第一行的实数。

Examples

Input
5
ano 10
ther 30
another 10
an 300
other 20
1
another
Output
112.826670
another
Input
5
ano 10.0
ther 30.0
another 10.0
an 300.0
other 2000.0
1
another
Output
212.837691
an other

Note

样例给出的词典与测试数据有所不同。

Source

2017 华东师范大学网赛

 
可以把所有字典都放进去字典树中,因为匹配的时候无视大小写,所以我们可以压小写进去就行了。
当然:
1、这样的话ABC和abc一样,取个价值大的就好。
2、价值公式:len * len * log(val),这个log,会变成负数,这样还不如不要,所以要娶个max(0, val)
 
TLE  hack:
注意题目,我一直TLE, 队友带飞。
长度大于30的直接是0了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 4e5 + ;
const int N = ;
char str[maxn];
struct Node {
double val;
struct Node * pNext[N];
} tree[maxn];
int t;
struct Node * create() {
struct Node *p = &tree[t++];
p->val = ;
return p;
}
void toInsert(struct Node **T, char str[], double val, int lenstr) {
struct Node *p = *T;
if (!p) p = *T = create();
for (int i = ; str[i]; ++i) {
int id = str[i] - 'a';
if (!p->pNext[id]) p->pNext[id] = create();
p = p->pNext[id];
}
p->val = max(p->val, val);
}
double ask(struct Node *T, char str[], int be, int en) {
if (en - be + > ) return ;
struct Node *p = T;
if (!p) return ;
for (int i = be; i <= en; ++i) {
int id = str[i] - 'a';
if (!p->pNext[id]) return ;
p = p->pNext[id];
}
return p->val;
}
struct ddpp {
double val;
int pre;
} dp[maxn];
int del[maxn], DFN;
bool isok(char ch) {
return ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z';
} void toChange(char str[], int lenstr) {
for (int i = ; i <= lenstr; ++i) {
if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] += ;
}
}
}
char sub[maxn];
void work() {
int n;
scanf("%d", &n);
struct Node *T = NULL;
for (int i = ; i <= n; ++i) {
scanf("%s", str + );
double val;
scanf("%lf", &val);
int lenstr = strlen(str + );
toChange(str, lenstr);
toInsert(&T, str, lenstr * lenstr * log(val), lenstr);
}
int q;
scanf("%d", &q);
while (q--) {
scanf("%s", str + );
int lenstr = strlen(str + );
strcpy(sub + , str + );
toChange(str, lenstr);
for (int i = ; i <= lenstr; ++i) {
dp[i].val = dp[i].pre = ;
for (int j = ; j <= i; ++j) {
double res = ask(T, str, j, i);
if (dp[i].val < dp[j - ].val + res) {
dp[i].val = dp[j - ].val + res;
dp[i].pre = j - ;
}
}
}
++DFN;
int pre = dp[lenstr].pre;
while (pre > ) {
del[pre] = DFN;
pre = dp[pre].pre;
}
printf("%0.10f\n", dp[lenstr].val);
for (int i = ; i <= lenstr; ++i) {
if (del[i - ] == DFN) {
printf(" ");
}
printf("%c", sub[i]);
}
printf("\n");
}
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
#endif
work();
return ;
}
 

EOJ Problem #3261 分词 trie + dp + 小剪枝的更多相关文章

  1. EOJ Problem #3249 状态压缩+循环周期+反向递推

    限量供应 Time limit per test: 4.0 seconds Time limit all tests: 4.0 seconds Memory limit: 256 megabytes ...

  2. UVA 3942 Remember the Word (Trie+DP)题解

    思路: 大白里Trie的例题,开篇就是一句很容易推出....orz 这里需要Trie+DP解决. 仔细想想我们可以得到dp[i]=sum(dp[i+len[x]]). 这里需要解释一下:dp是从最后一 ...

  3. EOJ 3261 分词

    字典树,$dp$. 记录$dp[i]$为以$i$为结尾获得的最大价值.枚举结尾一段是哪个单词,更新最大值.可以将字典中单词倒着建一棵字典树. 这题数据有点不严谨. 下面这组数据答案应该是负的. 3 a ...

  4. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  5. hdu 4540 威威猫系列故事——打地鼠 dp小水题

    威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total ...

  6. 四维dp 或者 剪枝 + dfs Codeforces Beta Round #6 (Div. 2 Only) D

    http://codeforces.com/contest/6/problem/D 题目大意:有一队人,排成一列,每个人都有生命值,你每次可以攻击2~n位置的一个的人,假设每次攻击的位置为pos,那么 ...

  7. EOJ Monthly 2018.4 (E.小迷妹在哪儿(贪心&排序&背包)

    ultmaster 男神和小迷妹们玩起了捉迷藏的游戏. 小迷妹们都希望自己被 ultmaster 男神发现,因此她们都把自己位置告诉了 ultmaster 男神,因此 ultmaster 男神知道了自 ...

  8. LightOJ - 1322 - Worst Case Trie(DP)

    链接: https://vjudge.net/problem/LightOJ-1322 题意: In Computer Science Trie or prefix tree is a data st ...

  9. zoj3777 Problem Arrangement(状压dp,思路赞)

    The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...

随机推荐

  1. L90

    On Motes and Beams 微尘与栋梁 It is curious that our own offenses should seem so much less heinous than t ...

  2. COGS 2581 无聊的会议V2

    传送 题目大意 给定一个长为\(n\)的序列,定义\(y\)在三元对\((x,y,z)\)中成为中心轴当且仅当同时满足:\(a_x = a_y = a_z,y-x=z-y,x<y<z\)对 ...

  3. MOVE降低高水位 HWM

    MOVE降低高水位 HWM --创建实验表空间SQL> create tablespace andy03 datafile '/home/oracle/app/oradata/orcl/andy ...

  4. CentOS7的安装以及GPT和MBR

    讲到GPT(GUID partition Table)和MBR(Master Boot Record)首先要将一下EFI(Extension Firmware Interface)和BIOS(Basi ...

  5. bzoj 4275 Badania naukowe —— DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4275 枚举 \( C \) 在 \( A \) 和 \( B \) 中的位置,然后取它前后的 ...

  6. docker基于宿主机系统版本创建镜像

    这里讲如何定制自己centos镜像,仅供测试docker使用. A) 安装软件 yum -y install febootstrap B)下载镜像febootstrap -i bash -i wget ...

  7. 手把手教你上传文件到GitHub并发布到pod

    第一步:定位到要上传到GitHub的文件夹, 第二步:GitHub中建立一个仓库,用于存放项目. 第三步:建立podspec文件, pod spec create openinstall 然后修改里面 ...

  8. python的logging模块详细使用demo

    import logging import os from logging import handlers from datetime import datetime class MyLog(): d ...

  9. function multi-versioning in GCC

    https://lwn.net/Articles/691932/ https://gcc.gnu.org/wiki/FunctionMultiVersioning why multi-versioni ...

  10. linux下libphenom的测试代码

    使用说明:测试使用libphenom库的字符串追加函数,效率是strcat的60多倍.所以在进行大量的字符串累加的时候可以考虑使用libphenom库  依赖库: ck-.tar.gz cmake-. ...